Skip to content

Commit

Permalink
[Java] Optimize sli long read/write (#981)
Browse files Browse the repository at this point in the history
optimize sli long read/write
  • Loading branch information
chaokunyang authored Oct 6, 2023
1 parent cfa074f commit f802e5d
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions java/fury-core/src/main/java/io/fury/memory/MemoryBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1830,14 +1830,15 @@ public int writeSliLong(long value) {
return unsafeWriteSliLong(value);
}

private static final int HALF_MAX_INT_VALUE = Integer.MAX_VALUE / 2;
private static final int HALF_MIN_INT_VALUE = Integer.MIN_VALUE / 2;
private static final long HALF_MAX_INT_VALUE = Integer.MAX_VALUE / 2;
private static final long HALF_MIN_INT_VALUE = Integer.MIN_VALUE / 2;
private static final byte BIG_LONG_FLAG = 0b1; // bit 0 set, means big long.

/** Write long using fury SLI(Small Long as Int) encoding. */
public int unsafeWriteSliLong(long value) {
final int writerIndex = this.writerIndex;
final long pos = address + writerIndex;
final byte[] heapMemory = this.heapMemory;
if (value >= HALF_MIN_INT_VALUE && value <= HALF_MAX_INT_VALUE) {
// write:
// 00xxx -> 0xxx
Expand All @@ -1851,7 +1852,7 @@ public int unsafeWriteSliLong(long value) {
} else {
UNSAFE.putInt(heapMemory, pos, Integer.reverseBytes(v));
}
this.writerIndex += 4;
this.writerIndex = writerIndex + 4;
return 4;
} else {
UNSAFE.putByte(heapMemory, pos, BIG_LONG_FLAG);
Expand All @@ -1860,16 +1861,17 @@ public int unsafeWriteSliLong(long value) {
} else {
UNSAFE.putLong(heapMemory, pos + 1, Long.reverseBytes(value));
}
this.writerIndex += 9;
this.writerIndex = writerIndex + 9;
return 9;
}
}

/** Read fury SLI(Small Long as Int) encoded long. */
public long readSliLong() {
int readIdx = readerIndex;
final int readIdx = readerIndex;
final long pos = address + readIdx;
int size = this.size;
final int size = this.size;
final byte[] heapMemory = this.heapMemory;
if (BoundsChecking.BOUNDS_CHECKING_ENABLED && readIdx > size - 4) {
throwIndexOutOfBoundsException(readIdx, size, 4);
}
Expand Down

0 comments on commit f802e5d

Please sign in to comment.