JCTools icon indicating copy to clipboard operation
JCTools copied to clipboard

[Channels] NOTE: Channels unaligned memory access considerations

Open nitsanw opened this issue 9 years ago • 3 comments

The SPSC fixed sized ring buffer is using an int marker for publication:

private boolean isMessageReady(long offset) {
    return UNSAFE.getIntVolatile(null, offset) == READY_MESSAGE_INDICATOR;
}

private void busyIndicator(long offset) {
    UNSAFE.putOrderedInt(null, offset, BUSY_MESSAGE_INDICATOR);
}

This is potentially an issue if offset is not integer aligned. We use int because there's no putOrderedByte. The int access may not be atomic, but this is not really an issue because we only set the value to 0 or 1 so there's only one actual byte in use. I am aware that for JDK9 Unsafe gains new methods for unaligned access which we may need to use here and elsewhere in the channel code. I am also aware that current packing/layout of data in each message is NOT aligned, which is something we may want to address in future.

nitsanw avatar Mar 12 '15 08:03 nitsanw

Can't you just use a long indicator and align the int write into it?

akarnokd avatar Apr 15 '15 17:04 akarnokd

in theory, yes. In practice I think I'll go with rounding the 'frame' size to int.

nitsanw avatar Apr 16 '15 07:04 nitsanw

@nitsanw Both have advantages: having a long indicator and frame size rounded to long will give you frame start aligned to long for free...

franz1981 avatar May 25 '18 09:05 franz1981