Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Slice to use MemorySegment #173

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: ['17', '21']
java: ['22', '23-ea']
steps:
- uses: actions/checkout@v4
- name: Setup JDK ${{ matrix.java }}
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<properties>
<air.javadoc.lint>-missing</air.javadoc.lint>
<air.test.jvmsize>3584m</air.test.jvmsize>
<project.build.targetJdk>17</project.build.targetJdk>
<air.java.version>17</air.java.version>
<project.build.targetJdk>22</project.build.targetJdk>
<air.java.version>22</air.java.version>
<air.modernizer.java-version>8</air.modernizer.java-version>
<air.check.skip-spotbugs>true</air.check.skip-spotbugs>
<air.check.skip-pmd>true</air.check.skip-pmd>
Expand All @@ -48,7 +48,7 @@
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.16</version>
<version>0.17</version>
</dependency>

<dependency>
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/io/airlift/slice/BasicSliceInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,49 @@ public double readDouble()
return v;
}

public double getDouble(int offset)
{
return slice.getDouble(position + offset);
}

public float getFloat(int offset)
{
return slice.getFloat(position + offset);
}

public float getShort(int offset)
{
return slice.getShort(position + offset);
}

public int getInt(int offset)
{
return slice.getInt(position + offset);
}

public byte getByte(int offset)
{
return slice.getByte(position + offset);
}

public long getLong(int offset)
{
return slice.getLong(position + offset);
}

public byte[] getByteArray()
{
return slice.byteArray();
}

/**
* Returns the start index the content of this slice within the byte array wrapped by this slice.
*/
public int getByteArrayOffset()
{
return position + slice.byteArrayOffset();
}

/**
* Returned slice is a view over {@code slice}
*/
Expand All @@ -164,6 +207,13 @@ public Slice readSlice(int length)
return newSlice;
}

public byte[] readBytes()
{
byte[] bytes = slice.getBytes();
position = slice.length();
return bytes;
}

@Override
public int read(byte[] destination, int destinationIndex, int length)
{
Expand Down
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
20 changes: 20 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 @@ -76,9 +77,28 @@ public final class SizeOf
public static final int OPTIONAL_DOUBLE_INSTANCE_SIZE = instanceSize(OptionalDouble.class);

public static final int STRING_INSTANCE_SIZE = instanceSize(String.class);
public static final int MEMORY_SEGMENT_INSTANCE_SIZE = instanceSize(MemorySegment.ofArray(new byte[0]).getClass());

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

public static long sizeOf(MemorySegment segment)
{
if (segment.isNative()) {
return MEMORY_SEGMENT_INSTANCE_SIZE;
}

return MEMORY_SEGMENT_INSTANCE_SIZE + switch (segment.heapBase().orElseThrow()) {
case byte[] byteArray -> sizeOf(byteArray);
case short[] shortArray -> sizeOf(shortArray);
case char[] charArray -> sizeOf(charArray);
case int[] intArray -> sizeOf(intArray);
case float[] floatArray -> sizeOf(floatArray);
case long[] longArray -> sizeOf(longArray);
case double[] doubleArray -> sizeOf(doubleArray);
default -> throw new AssertionError("Cannot get here");
};
}

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