Skip to content

Commit

Permalink
Move Slice to use MemorySegment
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo committed Jul 2, 2024
1 parent b04abb3 commit 93866d7
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 569 deletions.
10 changes: 5 additions & 5 deletions src/main/java/io/airlift/slice/InputStreamSliceInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public boolean readBoolean()
public byte readByte()
{
ensureAvailable(SIZE_OF_BYTE);
byte v = slice.getByteUnchecked(bufferPosition);
byte v = slice.getByte(bufferPosition);
bufferPosition += SIZE_OF_BYTE;
return v;
}
Expand All @@ -125,7 +125,7 @@ public int readUnsignedByte()
public short readShort()
{
ensureAvailable(SIZE_OF_SHORT);
short v = slice.getShortUnchecked(bufferPosition);
short v = slice.getShort(bufferPosition);
bufferPosition += SIZE_OF_SHORT;
return v;
}
Expand All @@ -140,7 +140,7 @@ public int readUnsignedShort()
public int readInt()
{
ensureAvailable(SIZE_OF_INT);
int v = slice.getIntUnchecked(bufferPosition);
int v = slice.getInt(bufferPosition);
bufferPosition += SIZE_OF_INT;
return v;
}
Expand All @@ -149,7 +149,7 @@ public int readInt()
public long readLong()
{
ensureAvailable(SIZE_OF_LONG);
long v = slice.getLongUnchecked(bufferPosition);
long v = slice.getLong(bufferPosition);
bufferPosition += SIZE_OF_LONG;
return v;
}
Expand All @@ -174,7 +174,7 @@ public int read()
}

verify(availableBytes() > 0);
int v = slice.getByteUnchecked(bufferPosition) & 0xFF;
int v = slice.getByte(bufferPosition) & 0xFF;
bufferPosition += SIZE_OF_BYTE;
return v;
}
Expand Down
82 changes: 0 additions & 82 deletions src/main/java/io/airlift/slice/JvmUtils.java

This file was deleted.

8 changes: 4 additions & 4 deletions src/main/java/io/airlift/slice/OutputStreamSliceOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,31 +133,31 @@ public boolean isWritable()
public void writeByte(int value)
{
ensureWritableBytes(SIZE_OF_BYTE);
slice.setByteUnchecked(bufferPosition, value);
slice.setByte(bufferPosition, value);
bufferPosition += SIZE_OF_BYTE;
}

@Override
public void writeShort(int value)
{
ensureWritableBytes(SIZE_OF_SHORT);
slice.setShortUnchecked(bufferPosition, value);
slice.setShort(bufferPosition, value);
bufferPosition += SIZE_OF_SHORT;
}

@Override
public void writeInt(int value)
{
ensureWritableBytes(SIZE_OF_INT);
slice.setIntUnchecked(bufferPosition, value);
slice.setInt(bufferPosition, value);
bufferPosition += SIZE_OF_INT;
}

@Override
public void writeLong(long value)
{
ensureWritableBytes(SIZE_OF_LONG);
slice.setLongUnchecked(bufferPosition, value);
slice.setLong(bufferPosition, value);
bufferPosition += SIZE_OF_LONG;
}

Expand Down
25 changes: 25 additions & 0 deletions src/main/java/io/airlift/slice/SizeOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openjdk.jol.vm.VM;
import org.openjdk.jol.vm.VirtualMachine;

import java.lang.foreign.MemorySegment;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -79,6 +80,30 @@ public final class SizeOf

private static final int SIMPLE_ENTRY_INSTANCE_SIZE = instanceSize(AbstractMap.SimpleEntry.class);

public static long sizeOf(MemorySegment segment)
{
// final long length;
// final boolean readOnly;
// final MemorySessionImpl scope (int state);
long size = instanceSize(segment.getClass());
if (segment.isNative()) {
return size;
}

return size
+ segment.heapBase() // base
.map(value -> switch (value) {
case byte[] byteArray -> sizeOf(byteArray);
case short[] shortArray -> sizeOf(shortArray);
case int[] intArray -> sizeOf(intArray);
case long[] longArray -> sizeOf(longArray);
case float[] floatArray -> sizeOf(floatArray);
case double[] doubleArray -> sizeOf(doubleArray);
default -> throw new UnsupportedOperationException("Unsupported heap type: " + value.getClass());
})
.orElseThrow();
}

public static long sizeOf(boolean[] array)
{
return (array == null) ? 0 : sizeOfBooleanArray(array.length);
Expand Down
Loading

0 comments on commit 93866d7

Please sign in to comment.