Repository navigation
Unsychronized MMapDataAccess - #2889
Conversation
|
We could also switch to a MethodHandle while we're at it: static final MethodHandle CLEANER;
static {
MethodHandle handle;
try {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(unsafeClass, MethodHandles.lookup());
MethodType type = MethodType.methodType(void.class, ByteBuffer.class);
handle = lookup.findVirtual(unsafeClass, "invokeCleaner", type);
VarHandle unsafeField = lookup.findStaticVarHandle(unsafeClass, "theUnsafe", unsafeClass);
Object unsafe = unsafeField.get();
handle = handle.bindTo(unsafe);
} catch (Exception e) {
LOGGER.error("Failed to obtain method handle for ByteBuffer cleaner.", e);
handle = null;
}
CLEANER = handle;
}
// ....
public static void cleanMappedByteBuffer(final ByteBuffer buffer) {
if (CLEANER == null) {
throw new RuntimeException("Cleaner is not available");
}
try {
CLEANER.invokeExact(buffer);
} catch (Throwable e) {
throw new RuntimeException(e);
}
} |
|
I'm really unsure how to test MMAP properly as with synchronized the problem was concurrent users and for MMAP also the randomness of the routes might be a problem (as it has to load the data into RAM). The best or most realistic would be probably always the same N routes from our logs. But the following test with static 2 points shows at least a reproducible difference. I use graphhopper (with a coverage of sachsen, -Xmx6g, 32g physical RAM) and drop the caches everytime I start a new load test with 200 concurrent users, i.e.:
The new MMAP config comes closer to RAM speed (avg response time in ms):
For real world setups, where the full RAM can be used the following config might be used: Which preloads everything into RAM. Then it is even a bit better:
So we shouldn't draw quick conclusions as I probably did not test it correctly, but the new settings seems to do fine in general :) |
|
Nice! |
Since #2827 we can now use getBytes with absolute indices and so the MMapDataAccess should be read-thread-safe even without
synchronized. Note, that writing is still not thread-safe (but this is the same for RAMDataAccess) as e.g. a value change for an EncodedValue might still involve several method calls, i.e. the synchronization for write-thread-safety would be required on a level above the DataAccess.