Best practices with reading/writing to a Memory Mapped File

Jorn Vernee jorn.vernee at oracle.com
Tue Jun 30 18:32:48 UTC 2020


One thing that looks suspicious to me when eye-balling the code is that 
you are creating your access VarHandles locally in MemoryMappedFileWriter.

Not only can the creation take a good amount of time, but the resulting 
VarHandle will not be a constant, and a lot of JIT optimization will 
therefore be turned off.

As a rule of thumb, you should always store VarHandles and MethodHandles 
in `static final` field, and then reference that field directly when 
doing the get or set call, so that the call can be optimized away by the 
JIT.

Jorn

On 27/06/2020 12:50, Johannes Lichtenberger wrote:
> Hi,
>
> I've fixed my Memory Mapped file implementation using your Foreign Memory
> API.
>
> https://github.com/sirixdb/sirix/tree/master/bundles/sirix-core/src/main/java/org/sirix/io/memorymapped
>
> Running my tests (mostly simple integration tests, which test if the stuff
> I'm storing can be retrieved again or the result of queries are what I
> expect), I can't see a clear performance difference between the
> RandomAccessFile implementation
>
> https://github.com/sirixdb/sirix/tree/master/bundles/sirix-core/src/main/java/org/sirix/io/file
>
> and the new memorymapped implementation.
>
> So far, I have to create a new mapping everytime I'm appending to the
> memory mapped segment of the underlying file I guess (otherwise the bounds
> checks will obviously fail):
>
> https://github.com/sirixdb/sirix/blob/627fa5a57a302b04d7165aad75a780d74e14c2e9/bundles/sirix-core/src/main/java/org/sirix/io/memorymapped/MemoryMappedFileWriter.java#L141
>
> I'm only ever appending data when writing or reading randomly based on
> offsets.
>
> I haven't done any microbenchmarks as of now and did not check bigger files
> ranging from 1Gb to much more nor did I use a profiler to check what's
> going on. However, maybe creating the mapping often times is costly and
> maybe you can simply spot a performance issue. Or it's IntelliJ and my
> rather small flles for testing as of now.
>
> Will next check if importing a 3,8 Gb JSON file is faster or iterating
> through the whole imported file with around 400_000_000 nodes :-)
>
> If anyone wants to check it it's simply changing
>
> private static final StorageType STORAGE = StorageType.FILE;
>
> to
>
> private static final StorageType STORAGE = StorageType.MEMORY_MAPPED;
>
> in the class: org.sirix.access.ResourceConfiguration
>
> Thanks for all the suggestions and hints so far
> Johannes


More information about the panama-dev mailing list