From 1706df49b8361ee808ed768b0e9d1ecc57cb3416 Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Fri, 20 Nov 2020 12:52:31 +0100 Subject: [PATCH 1/8] Add a GitHub workflow for building PRs --- .github/workflows/ci-workflow.yml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 .github/workflows/ci-workflow.yml diff --git a/.github/workflows/ci-workflow.yml b/.github/workflows/ci-workflow.yml new file mode 100644 index 0000000..e934d69 --- /dev/null +++ b/.github/workflows/ci-workflow.yml @@ -0,0 +1,30 @@ +# This is a basic workflow to help you get started with Actions + +name: CI Build + +# Controls when the action will run. +on: + # Triggers the workflow on push or pull request events but only for the main branch + push: + branches: [ main ] + pull_request: + branches: [ main ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + + # Run the make script + - name: Make build + run: make build From 023bb64a2580ed766793625046b634a073f5108e Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Fri, 20 Nov 2020 14:01:14 +0100 Subject: [PATCH 2/8] Update MemSegBuf with the latest panama-foreign API changes --- .../io/netty/buffer/api/memseg/MemSegBuf.java | 332 +++++++----------- 1 file changed, 119 insertions(+), 213 deletions(-) diff --git a/src/main/java/io/netty/buffer/api/memseg/MemSegBuf.java b/src/main/java/io/netty/buffer/api/memseg/MemSegBuf.java index 7d0085d..c7cea45 100644 --- a/src/main/java/io/netty/buffer/api/memseg/MemSegBuf.java +++ b/src/main/java/io/netty/buffer/api/memseg/MemSegBuf.java @@ -28,41 +28,27 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.NoSuchElementException; -import static jdk.incubator.foreign.MemoryAccess.getByteAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.getByteAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.getCharAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.getCharAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.getDoubleAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.getDoubleAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.getFloatAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.getFloatAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.getIntAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.getIntAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.getLongAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.getLongAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.getShortAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.getShortAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.setByteAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.setByteAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.setCharAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.setCharAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.setDoubleAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.setDoubleAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.setFloatAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.setFloatAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.setIntAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.setIntAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.setLongAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.setLongAtOffset_LE; -import static jdk.incubator.foreign.MemoryAccess.setShortAtOffset_BE; -import static jdk.incubator.foreign.MemoryAccess.setShortAtOffset_LE; +import static jdk.incubator.foreign.MemoryAccess.getByteAtOffset; +import static jdk.incubator.foreign.MemoryAccess.getCharAtOffset; +import static jdk.incubator.foreign.MemoryAccess.getDoubleAtOffset; +import static jdk.incubator.foreign.MemoryAccess.getFloatAtOffset; +import static jdk.incubator.foreign.MemoryAccess.getIntAtOffset; +import static jdk.incubator.foreign.MemoryAccess.getLongAtOffset; +import static jdk.incubator.foreign.MemoryAccess.getShortAtOffset; +import static jdk.incubator.foreign.MemoryAccess.setByteAtOffset; +import static jdk.incubator.foreign.MemoryAccess.setCharAtOffset; +import static jdk.incubator.foreign.MemoryAccess.setDoubleAtOffset; +import static jdk.incubator.foreign.MemoryAccess.setFloatAtOffset; +import static jdk.incubator.foreign.MemoryAccess.setIntAtOffset; +import static jdk.incubator.foreign.MemoryAccess.setLongAtOffset; +import static jdk.incubator.foreign.MemoryAccess.setShortAtOffset; class MemSegBuf extends RcSupport implements Buf { static final Drop SEGMENT_CLOSE = buf -> buf.seg.close(); private final AllocatorControl alloc; private final boolean isSendable; private MemorySegment seg; - private boolean isBigEndian; + private ByteOrder order; private int roff; private int woff; @@ -75,7 +61,7 @@ class MemSegBuf extends RcSupport implements Buf { this.alloc = alloc; seg = segment; this.isSendable = isSendable; - isBigEndian = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN; + order = ByteOrder.nativeOrder(); } @Override @@ -85,13 +71,13 @@ class MemSegBuf extends RcSupport implements Buf { @Override public Buf order(ByteOrder order) { - isBigEndian = order == ByteOrder.BIG_ENDIAN; + this.order = order; return this; } @Override public ByteOrder order() { - return isBigEndian? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN; + return order; } @Override @@ -217,7 +203,7 @@ class MemSegBuf extends RcSupport implements Buf { if (!hasNextLong()) { throw new NoSuchElementException("No 'long' value at offet " + currentOffset() + '.'); } - long val = getLongAtOffset_BE(segment, index); + long val = getLongAtOffset(segment, index, ByteOrder.BIG_ENDIAN); index += Long.BYTES; return val; } @@ -232,7 +218,7 @@ class MemSegBuf extends RcSupport implements Buf { if (!hasNextByte()) { throw new NoSuchElementException("No 'byte' value at offet " + currentOffset() + '.'); } - byte val = getByteAtOffset_BE(segment, index); + byte val = getByteAtOffset(segment, index); index++; return val; } @@ -280,7 +266,7 @@ class MemSegBuf extends RcSupport implements Buf { throw new NoSuchElementException("No 'long' value at offet " + currentOffset() + '.'); } index -= 7; - long val = getLongAtOffset_LE(segment, index); + long val = getLongAtOffset(segment, index, ByteOrder.LITTLE_ENDIAN); index--; return val; } @@ -295,7 +281,7 @@ class MemSegBuf extends RcSupport implements Buf { if (!hasNextByte()) { throw new NoSuchElementException("No 'byte' value at offet " + currentOffset() + '.'); } - byte val = getByteAtOffset_LE(segment, index); + byte val = getByteAtOffset(segment, index); index--; return val; } @@ -336,7 +322,7 @@ class MemSegBuf extends RcSupport implements Buf { @Override public byte readByte() { checkRead(roff, Byte.BYTES); - byte value = isBigEndian? getByteAtOffset_BE(seg, roff) : getByteAtOffset_LE(seg, roff); + byte value = getByteAtOffset(seg, roff); roff += Byte.BYTES; return value; } @@ -344,13 +330,13 @@ class MemSegBuf extends RcSupport implements Buf { @Override public byte getByte(int roff) { checkRead(roff, Byte.BYTES); - return isBigEndian? getByteAtOffset_BE(seg, roff) : getByteAtOffset_LE(seg, roff); + return getByteAtOffset(seg, roff); } @Override public int readUnsignedByte() { checkRead(roff, Byte.BYTES); - int value = (isBigEndian? getByteAtOffset_BE(seg, roff) : getByteAtOffset_LE(seg, roff)) & 0xFF; + int value = getByteAtOffset(seg, roff) & 0xFF; roff += Byte.BYTES; return value; } @@ -358,55 +344,39 @@ class MemSegBuf extends RcSupport implements Buf { @Override public int getUnsignedByte(int roff) { checkRead(roff, Byte.BYTES); - return (isBigEndian? getByteAtOffset_BE(seg, roff) : getByteAtOffset_LE(seg, roff)) & 0xFF; + return getByteAtOffset(seg, roff) & 0xFF; } @Override public Buf writeByte(byte value) { - if (isBigEndian) { - setByteAtOffset_BE(seg, woff, value); - } else { - setByteAtOffset_LE(seg, woff, value); - } + setByteAtOffset(seg, woff, value); woff += Byte.BYTES; return this; } @Override public Buf setByte(int woff, byte value) { - if (isBigEndian) { - setByteAtOffset_BE(seg, woff, value); - } else { - setByteAtOffset_LE(seg, woff, value); - } + setByteAtOffset(seg, woff, value); return this; } @Override public Buf writeUnsignedByte(int value) { - if (isBigEndian) { - setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); - } else { - setByteAtOffset_LE(seg, woff, (byte) (value & 0xFF)); - } + setByteAtOffset(seg, woff, (byte) (value & 0xFF)); woff += Byte.BYTES; return this; } @Override public Buf setUnsignedByte(int woff, int value) { - if (isBigEndian) { - setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); - } else { - setByteAtOffset_LE(seg, woff, (byte) (value & 0xFF)); - } + setByteAtOffset(seg, woff, (byte) (value & 0xFF)); return this; } @Override public char readChar() { checkRead(roff, 2); - char value = isBigEndian? getCharAtOffset_BE(seg, roff) : getCharAtOffset_LE(seg, roff); + char value = getCharAtOffset(seg, roff, order); roff += 2; return value; } @@ -414,34 +384,26 @@ class MemSegBuf extends RcSupport implements Buf { @Override public char getChar(int roff) { checkRead(roff, 2); - return isBigEndian? getCharAtOffset_BE(seg, roff) : getCharAtOffset_LE(seg, roff); + return getCharAtOffset(seg, roff, order); } @Override public Buf writeChar(char value) { - if (isBigEndian) { - setCharAtOffset_BE(seg, woff, value); - } else { - setCharAtOffset_LE(seg, woff, value); - } + setCharAtOffset(seg, woff, order, value); woff += 2; return this; } @Override public Buf setChar(int woff, char value) { - if (isBigEndian) { - setCharAtOffset_BE(seg, woff, value); - } else { - setCharAtOffset_LE(seg, woff, value); - } + setCharAtOffset(seg, woff, order, value); return this; } @Override public short readShort() { checkRead(roff, Short.BYTES); - short value = isBigEndian? getShortAtOffset_BE(seg, roff) : getShortAtOffset_LE(seg, roff); + short value = getShortAtOffset(seg, roff, order); roff += Short.BYTES; return value; } @@ -449,13 +411,13 @@ class MemSegBuf extends RcSupport implements Buf { @Override public short getShort(int roff) { checkRead(roff, Short.BYTES); - return isBigEndian? getShortAtOffset_BE(seg, roff) : getShortAtOffset_LE(seg, roff); + return getShortAtOffset(seg, roff, order); } @Override public int readUnsignedShort() { checkRead(roff, Short.BYTES); - int value = (isBigEndian? getShortAtOffset_BE(seg, roff) : getShortAtOffset_LE(seg, roff)) & 0xFFFF; + int value = getShortAtOffset(seg, roff, order) & 0xFFFF; roff += Short.BYTES; return value; } @@ -463,61 +425,45 @@ class MemSegBuf extends RcSupport implements Buf { @Override public int getUnsignedShort(int roff) { checkRead(roff, Short.BYTES); - return (isBigEndian? getShortAtOffset_BE(seg, roff) : getShortAtOffset_LE(seg, roff)) & 0xFFFF; + return getShortAtOffset(seg, roff, order) & 0xFFFF; } @Override public Buf writeShort(short value) { - if (isBigEndian) { - setShortAtOffset_BE(seg, woff, value); - } else { - setShortAtOffset_LE(seg, woff, value); - } + setShortAtOffset(seg, woff, order, value); woff += Short.BYTES; return this; } @Override public Buf setShort(int woff, short value) { - if (isBigEndian) { - setShortAtOffset_BE(seg, woff, value); - } else { - setShortAtOffset_LE(seg, woff, value); - } + setShortAtOffset(seg, woff, order, value); return this; } @Override public Buf writeUnsignedShort(int value) { - if (isBigEndian) { - setShortAtOffset_BE(seg, woff, (short) (value & 0xFFFF)); - } else { - setShortAtOffset_LE(seg, woff, (short) (value & 0xFFFF)); - } + setShortAtOffset(seg, woff, order, (short) (value & 0xFFFF)); woff += Short.BYTES; return this; } @Override public Buf setUnsignedShort(int woff, int value) { - if (isBigEndian) { - setShortAtOffset_BE(seg, woff, (short) (value & 0xFFFF)); - } else { - setShortAtOffset_LE(seg, woff, (short) (value & 0xFFFF)); - } + setShortAtOffset(seg, woff, order, (short) (value & 0xFFFF)); return this; } @Override public int readMedium() { checkRead(roff, 3); - int value = isBigEndian? - getByteAtOffset_BE(seg, roff) << 16 | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) & 0xFF : - getByteAtOffset_BE(seg, roff) & 0xFF | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) << 16; + int value = order == ByteOrder.BIG_ENDIAN? + getByteAtOffset(seg, roff) << 16 | + (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset(seg, roff + 2) & 0xFF : + getByteAtOffset(seg, roff) & 0xFF | + (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset(seg, roff + 2) << 16; roff += 3; return value; } @@ -525,25 +471,25 @@ class MemSegBuf extends RcSupport implements Buf { @Override public int getMedium(int roff) { checkRead(roff, 3); - return isBigEndian? - getByteAtOffset_BE(seg, roff) << 16 | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) & 0xFF : - getByteAtOffset_BE(seg, roff) & 0xFF | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) << 16; + return order == ByteOrder.BIG_ENDIAN? + getByteAtOffset(seg, roff) << 16 | + (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset(seg, roff + 2) & 0xFF : + getByteAtOffset(seg, roff) & 0xFF | + (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset(seg, roff + 2) << 16; } @Override public int readUnsignedMedium() { checkRead(roff, 3); - int value = isBigEndian? - (getByteAtOffset_BE(seg, roff) << 16 | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) & 0xFF) & 0xFFFFFF : - (getByteAtOffset_BE(seg, roff) & 0xFF | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) << 16) & 0xFFFFFF; + int value = order == ByteOrder.BIG_ENDIAN? + (getByteAtOffset(seg, roff) << 16 | + (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset(seg, roff + 2) & 0xFF) & 0xFFFFFF : + (getByteAtOffset(seg, roff) & 0xFF | + (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset(seg, roff + 2) << 16) & 0xFFFFFF; roff += 3; return value; } @@ -551,26 +497,26 @@ class MemSegBuf extends RcSupport implements Buf { @Override public int getUnsignedMedium(int roff) { checkRead(roff, 3); - return isBigEndian? - (getByteAtOffset_BE(seg, roff) << 16 | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) & 0xFF) & 0xFFFFFF : - (getByteAtOffset_BE(seg, roff) & 0xFF | - (getByteAtOffset_BE(seg, roff + 1) & 0xFF) << 8 | - getByteAtOffset_BE(seg, roff + 2) << 16) & 0xFFFFFF; + return order == ByteOrder.BIG_ENDIAN? + (getByteAtOffset(seg, roff) << 16 | + (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset(seg, roff + 2) & 0xFF) & 0xFFFFFF : + (getByteAtOffset(seg, roff) & 0xFF | + (getByteAtOffset(seg, roff + 1) & 0xFF) << 8 | + getByteAtOffset(seg, roff + 2) << 16) & 0xFFFFFF; } @Override public Buf writeMedium(int value) { checkWrite(woff, 3); - if (isBigEndian) { - setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); - setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); - setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + if (order == ByteOrder.BIG_ENDIAN) { + setByteAtOffset(seg, woff, (byte) (value >> 16)); + setByteAtOffset(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset(seg, woff + 2, (byte) (value & 0xFF)); } else { - setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); - setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); - setByteAtOffset_BE(seg, woff + 2, (byte) (value >> 16 & 0xFF)); + setByteAtOffset(seg, woff, (byte) (value & 0xFF)); + setByteAtOffset(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset(seg, woff + 2, (byte) (value >> 16 & 0xFF)); } woff += 3; return this; @@ -579,14 +525,14 @@ class MemSegBuf extends RcSupport implements Buf { @Override public Buf setMedium(int woff, int value) { checkWrite(woff, 3); - if (isBigEndian) { - setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); - setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); - setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + if (order == ByteOrder.BIG_ENDIAN) { + setByteAtOffset(seg, woff, (byte) (value >> 16)); + setByteAtOffset(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset(seg, woff + 2, (byte) (value & 0xFF)); } else { - setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); - setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); - setByteAtOffset_BE(seg, woff + 2, (byte) (value >> 16 & 0xFF)); + setByteAtOffset(seg, woff, (byte) (value & 0xFF)); + setByteAtOffset(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset(seg, woff + 2, (byte) (value >> 16 & 0xFF)); } return this; } @@ -594,14 +540,14 @@ class MemSegBuf extends RcSupport implements Buf { @Override public Buf writeUnsignedMedium(int value) { checkWrite(woff, 3); - if (isBigEndian) { - setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); - setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); - setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + if (order == ByteOrder.BIG_ENDIAN) { + setByteAtOffset(seg, woff, (byte) (value >> 16)); + setByteAtOffset(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset(seg, woff + 2, (byte) (value & 0xFF)); } else { - setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); - setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); - setByteAtOffset_BE(seg, woff + 2, (byte) (value >> 16 & 0xFF)); + setByteAtOffset(seg, woff, (byte) (value & 0xFF)); + setByteAtOffset(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset(seg, woff + 2, (byte) (value >> 16 & 0xFF)); } woff += 3; return this; @@ -610,14 +556,14 @@ class MemSegBuf extends RcSupport implements Buf { @Override public Buf setUnsignedMedium(int woff, int value) { checkWrite(woff, 3); - if (isBigEndian) { - setByteAtOffset_BE(seg, woff, (byte) (value >> 16)); - setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); - setByteAtOffset_BE(seg, woff + 2, (byte) (value & 0xFF)); + if (order == ByteOrder.BIG_ENDIAN) { + setByteAtOffset(seg, woff, (byte) (value >> 16)); + setByteAtOffset(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset(seg, woff + 2, (byte) (value & 0xFF)); } else { - setByteAtOffset_BE(seg, woff, (byte) (value & 0xFF)); - setByteAtOffset_BE(seg, woff + 1, (byte) (value >> 8 & 0xFF)); - setByteAtOffset_BE(seg, woff + 2, (byte) (value >> 16 & 0xFF)); + setByteAtOffset(seg, woff, (byte) (value & 0xFF)); + setByteAtOffset(seg, woff + 1, (byte) (value >> 8 & 0xFF)); + setByteAtOffset(seg, woff + 2, (byte) (value >> 16 & 0xFF)); } return this; } @@ -625,7 +571,7 @@ class MemSegBuf extends RcSupport implements Buf { @Override public int readInt() { checkRead(roff, Integer.BYTES); - int value = isBigEndian? getIntAtOffset_BE(seg, roff) : getIntAtOffset_LE(seg, roff); + int value = getIntAtOffset(seg, roff, order); roff += Integer.BYTES; return value; } @@ -633,13 +579,13 @@ class MemSegBuf extends RcSupport implements Buf { @Override public int getInt(int roff) { checkRead(roff, Integer.BYTES); - return isBigEndian? getIntAtOffset_BE(seg, roff) : getIntAtOffset_LE(seg, roff); + return getIntAtOffset(seg, roff, order); } @Override public long readUnsignedInt() { checkRead(roff, Integer.BYTES); - long value = (isBigEndian? getIntAtOffset_BE(seg, roff) : getIntAtOffset_LE(seg, roff)) & 0xFFFFFFFFL; + long value = getIntAtOffset(seg, roff, order) & 0xFFFFFFFFL; roff += Integer.BYTES; return value; } @@ -647,55 +593,39 @@ class MemSegBuf extends RcSupport implements Buf { @Override public long getUnsignedInt(int roff) { checkRead(roff, Integer.BYTES); - return (isBigEndian? getIntAtOffset_BE(seg, roff) : getIntAtOffset_LE(seg, roff)) & 0xFFFFFFFFL; + return getIntAtOffset(seg, roff, order) & 0xFFFFFFFFL; } @Override public Buf writeInt(int value) { - if (isBigEndian) { - setIntAtOffset_BE(seg, woff, value); - } else { - setIntAtOffset_LE(seg, woff, value); - } + setIntAtOffset(seg, woff, order, value); woff += Integer.BYTES; return this; } @Override public Buf setInt(int woff, int value) { - if (isBigEndian) { - setIntAtOffset_BE(seg, woff, value); - } else { - setIntAtOffset_LE(seg, woff, value); - } + setIntAtOffset(seg, woff, order, value); return this; } @Override public Buf writeUnsignedInt(long value) { - if (isBigEndian) { - setIntAtOffset_BE(seg, woff, (int) (value & 0xFFFFFFFFL)); - } else { - setIntAtOffset_LE(seg, woff, (int) (value & 0xFFFFFFFFL)); - } + setIntAtOffset(seg, woff, order, (int) (value & 0xFFFFFFFFL)); woff += Integer.BYTES; return this; } @Override public Buf setUnsignedInt(int woff, long value) { - if (isBigEndian) { - setIntAtOffset_BE(seg, woff, (int) (value & 0xFFFFFFFFL)); - } else { - setIntAtOffset_LE(seg, woff, (int) (value & 0xFFFFFFFFL)); - } + setIntAtOffset(seg, woff, order, (int) (value & 0xFFFFFFFFL)); return this; } @Override public float readFloat() { checkRead(roff, Float.BYTES); - float value = isBigEndian? getFloatAtOffset_BE(seg, roff) : getFloatAtOffset_LE(seg, roff); + float value = getFloatAtOffset(seg, roff, order); roff += Float.BYTES; return value; } @@ -703,34 +633,26 @@ class MemSegBuf extends RcSupport implements Buf { @Override public float getFloat(int roff) { checkRead(roff, Float.BYTES); - return isBigEndian? getFloatAtOffset_BE(seg, roff) : getFloatAtOffset_LE(seg, roff); + return getFloatAtOffset(seg, roff, order); } @Override public Buf writeFloat(float value) { - if (isBigEndian) { - setFloatAtOffset_BE(seg, woff, value); - } else { - setFloatAtOffset_LE(seg, woff, value); - } + setFloatAtOffset(seg, woff, order, value); woff += Float.BYTES; return this; } @Override public Buf setFloat(int woff, float value) { - if (isBigEndian) { - setFloatAtOffset_BE(seg, woff, value); - } else { - setFloatAtOffset_LE(seg, woff, value); - } + setFloatAtOffset(seg, woff, order, value); return this; } @Override public long readLong() { checkRead(roff, Long.BYTES); - long value = isBigEndian? getLongAtOffset_BE(seg, roff) : getLongAtOffset_LE(seg, roff); + long value = getLongAtOffset(seg, roff, order); roff += Long.BYTES; return value; } @@ -738,34 +660,26 @@ class MemSegBuf extends RcSupport implements Buf { @Override public long getLong(int roff) { checkRead(roff, Long.BYTES); - return isBigEndian? getLongAtOffset_BE(seg, roff) : getLongAtOffset_LE(seg, roff); + return getLongAtOffset(seg, roff, order); } @Override public Buf writeLong(long value) { - if (isBigEndian) { - setLongAtOffset_BE(seg, woff, value); - } else { - setLongAtOffset_LE(seg, woff, value); - } + setLongAtOffset(seg, woff, order, value); woff += Long.BYTES; return this; } @Override public Buf setLong(int woff, long value) { - if (isBigEndian) { - setLongAtOffset_BE(seg, woff, value); - } else { - setLongAtOffset_LE(seg, woff, value); - } + setLongAtOffset(seg, woff, order, value); return this; } @Override public double readDouble() { checkRead(roff, Double.BYTES); - double value = isBigEndian? getDoubleAtOffset_BE(seg, roff) : getDoubleAtOffset_LE(seg, roff); + double value = getDoubleAtOffset(seg, roff, order); roff += Double.BYTES; return value; } @@ -773,27 +687,19 @@ class MemSegBuf extends RcSupport implements Buf { @Override public double getDouble(int roff) { checkRead(roff, Double.BYTES); - return isBigEndian? getDoubleAtOffset_BE(seg, roff) : getDoubleAtOffset_LE(seg, roff); + return getDoubleAtOffset(seg, roff, order); } @Override public Buf writeDouble(double value) { - if (isBigEndian) { - setDoubleAtOffset_BE(seg, woff, value); - } else { - setDoubleAtOffset_LE(seg, woff, value); - } + setDoubleAtOffset(seg, woff, order, value); woff += Double.BYTES; return this; } @Override public Buf setDouble(int woff, double value) { - if (isBigEndian) { - setDoubleAtOffset_BE(seg, woff, value); - } else { - setDoubleAtOffset_LE(seg, woff, value); - } + setDoubleAtOffset(seg, woff, order, value); return this; } // @@ -808,7 +714,7 @@ class MemSegBuf extends RcSupport implements Buf { public MemSegBuf transferOwnership(Drop drop) { var newSegment = isConfined? transferSegment.handoff(Thread.currentThread()) : transferSegment; MemSegBuf copy = new MemSegBuf(newSegment, drop, alloc); - copy.isBigEndian = outer.isBigEndian; + copy.order = outer.order; copy.roff = outer.roff; copy.woff = outer.woff; return copy; From 72eb5d3bcb2d1deed136ebea9303130654ef9b6e Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Fri, 20 Nov 2020 14:33:06 +0100 Subject: [PATCH 3/8] Try adding a build cache that uses githubs package repo as a cache Inspired by https://dev.to/dtinth/caching-docker-builds-in-github-actions-which-approach-is-the-fastest-a-research-18ei --- .github/workflows/ci-workflow.yml | 11 +++++++++++ Makefile | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci-workflow.yml b/.github/workflows/ci-workflow.yml index e934d69..8e85c50 100644 --- a/.github/workflows/ci-workflow.yml +++ b/.github/workflows/ci-workflow.yml @@ -24,7 +24,18 @@ jobs: steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 + - name: Prepare docker build cache + run: + echo ${{ secrets.GITHUB_TOKEN }} | docker login docker.pkg.github.com -u $GITHUB_ACTOR --password-stdin + docker pull docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache || true + export DOCKER_BUILD_OPTS="--cache-from=docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache" # Run the make script - name: Make build run: make build + + # Cache build outputs + - name: Save docker build cache + run: + docker tag netty-incubator-buffer:build docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache + docker push docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache || true diff --git a/Makefile b/Makefile index 4e662c5..0e92cd9 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ .DEFAULT_GOAL := build image: - docker build --tag netty-incubator-buffer:build . + docker build $(DOCKER_BUILD_OPTS) --tag netty-incubator-buffer:build . test: image docker run --rm --name build-container netty-incubator-buffer:build From 308b4df3b61aeb802fef9694d13b6df45daa7343 Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Fri, 20 Nov 2020 15:28:02 +0100 Subject: [PATCH 4/8] Try fixing multi-line workflow commands --- .github/workflows/ci-workflow.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci-workflow.yml b/.github/workflows/ci-workflow.yml index 8e85c50..e7b59ff 100644 --- a/.github/workflows/ci-workflow.yml +++ b/.github/workflows/ci-workflow.yml @@ -25,7 +25,7 @@ jobs: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 - name: Prepare docker build cache - run: + run: | echo ${{ secrets.GITHUB_TOKEN }} | docker login docker.pkg.github.com -u $GITHUB_ACTOR --password-stdin docker pull docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache || true export DOCKER_BUILD_OPTS="--cache-from=docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache" @@ -36,6 +36,6 @@ jobs: # Cache build outputs - name: Save docker build cache - run: + run: | docker tag netty-incubator-buffer:build docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache docker push docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache || true From f3e494bce345bce834bf4836a1dbfd78e2342db0 Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Fri, 20 Nov 2020 15:24:26 +0100 Subject: [PATCH 5/8] Add first example on how to use the new buffer API --- .../buffer/api/examples/FileCopyExample.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/test/java/io/netty/buffer/api/examples/FileCopyExample.java diff --git a/src/test/java/io/netty/buffer/api/examples/FileCopyExample.java b/src/test/java/io/netty/buffer/api/examples/FileCopyExample.java new file mode 100644 index 0000000..ba075a9 --- /dev/null +++ b/src/test/java/io/netty/buffer/api/examples/FileCopyExample.java @@ -0,0 +1,92 @@ +/* + * Copyright 2020 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.buffer.api.examples; + +import io.netty.buffer.api.Allocator; +import io.netty.buffer.api.Buf; +import io.netty.buffer.api.Send; + +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Path; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static java.nio.file.StandardOpenOption.CREATE; +import static java.nio.file.StandardOpenOption.READ; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; +import static java.nio.file.StandardOpenOption.WRITE; + +public final class FileCopyExample { + public static void main(String[] args) throws Exception { + ExecutorService executor = Executors.newFixedThreadPool(2); + ArrayBlockingQueue queue = new ArrayBlockingQueue<>(8); + Object done = new Object(); + try (Allocator allocator = Allocator.pooledDirect(); + var input = FileChannel.open(Path.of("/dev/urandom"), READ); + var output = FileChannel.open(Path.of("random.bin"), CREATE, TRUNCATE_EXISTING, WRITE)) { + + var reader = executor.submit(() -> { + var buf = ByteBuffer.allocateDirect(1024); + for (int i = 0; i < 1024; i++) { + buf.clear(); + while (buf.hasRemaining()) { + int r = input.read(buf); + System.out.println("r = " + r); + System.out.println("buf = " + buf); + } + buf.flip(); + try (Buf in = allocator.allocate(1024)) { + System.out.println("in = " + in); + while (buf.hasRemaining()) { + in.writeByte(buf.get()); + } + System.out.println("Sending " + in.readableBytes() + " bytes."); + queue.put(in.send()); + } + } + queue.put(done); + return null; + }); + + var writer = executor.submit(() -> { + var buf = ByteBuffer.allocateDirect(1024); + Object msg; + while ((msg = queue.take()) != done) { + buf.clear(); + @SuppressWarnings("unchecked") + Send send = (Send) msg; + try (Buf out = send.receive()) { + System.out.println("Received " + out.readableBytes() + " bytes."); + out.copyInto(0, buf, 0, out.readableBytes()); + buf.position(0).limit(out.readableBytes()); + } + while (buf.hasRemaining()) { + output.write(buf); + } + } + output.force(true); + return null; + }); + + reader.get(); + writer.get(); + } finally { + executor.shutdown(); + } + } +} From 1f9ab72a4416406358a31019178e7d7605c0d4a3 Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Fri, 20 Nov 2020 22:22:01 +0100 Subject: [PATCH 6/8] Add more examples --- .../buffer/api/examples/AsyncExample.java | 47 ++++++++++++++++ .../examples/ComposingAndSlicingExample.java | 54 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/test/java/io/netty/buffer/api/examples/AsyncExample.java create mode 100644 src/test/java/io/netty/buffer/api/examples/ComposingAndSlicingExample.java diff --git a/src/test/java/io/netty/buffer/api/examples/AsyncExample.java b/src/test/java/io/netty/buffer/api/examples/AsyncExample.java new file mode 100644 index 0000000..f4d7717 --- /dev/null +++ b/src/test/java/io/netty/buffer/api/examples/AsyncExample.java @@ -0,0 +1,47 @@ +/* + * Copyright 2020 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.buffer.api.examples; + +import io.netty.buffer.api.Allocator; +import io.netty.buffer.api.Buf; + +import static java.util.concurrent.CompletableFuture.completedFuture; + +public final class AsyncExample { + public static void main(String[] args) throws Exception { + try (Allocator allocator = Allocator.pooledDirectWithCleaner(); + Buf startBuf = allocator.allocate(16)) { + startBuf.writeLong(threadId()); + + completedFuture(startBuf.send()).thenApplyAsync(send -> { + try (Buf buf = send.receive()) { + buf.writeLong(threadId()); + return buf.send(); + } + }).thenAcceptAsync(send -> { + try (Buf buf = send.receive()) { + System.out.println("First thread id was " + buf.readLong()); + System.out.println("Then sent to " + buf.readLong()); + System.out.println("And now in thread " + threadId()); + } + }).get(); + } + } + + private static long threadId() { + return Thread.currentThread().getId(); + } +} diff --git a/src/test/java/io/netty/buffer/api/examples/ComposingAndSlicingExample.java b/src/test/java/io/netty/buffer/api/examples/ComposingAndSlicingExample.java new file mode 100644 index 0000000..1c970aa --- /dev/null +++ b/src/test/java/io/netty/buffer/api/examples/ComposingAndSlicingExample.java @@ -0,0 +1,54 @@ +/* + * Copyright 2020 The Netty Project + * + * The Netty Project licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ +package io.netty.buffer.api.examples; + +import io.netty.buffer.api.Allocator; +import io.netty.buffer.api.Buf; +import io.netty.buffer.api.Scope; + +import java.util.concurrent.ThreadLocalRandom; + +public final class ComposingAndSlicingExample { + public static void main(String[] args) { + try (Allocator allocator = Allocator.pooledDirect(); + Buf buf = createBigBuffer(allocator)) { + + ThreadLocalRandom tlr = ThreadLocalRandom.current(); + for (int i = 0; i < tlr.nextInt(4, 200); i++) { + buf.writeByte((byte) tlr.nextInt()); + } + + try (Buf slice = buf.slice()) { + slice.send(); + System.out.println("buf.capacity() = " + buf.capacity()); + System.out.println("buf.readableBytes() = " + buf.readableBytes()); + System.out.println("---"); + System.out.println("slice.capacity() = " + slice.capacity()); + System.out.println("slice.readableBytes() = " + slice.readableBytes()); + } + } + } + + private static Buf createBigBuffer(Allocator allocator) { + try (Scope scope = new Scope()) { + return allocator.compose( + scope.add(allocator.allocate(64)), + scope.add(allocator.allocate(64)), + scope.add(allocator.allocate(64)), + scope.add(allocator.allocate(64))); + } + } +} From 87d23f52db510621b7276fe9066d9fd14e7a63ec Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Sat, 21 Nov 2020 11:34:08 +0100 Subject: [PATCH 7/8] Try a different caching mechanism --- .github/workflows/ci-workflow.yml | 34 +++++++++++++++++++++---------- Dockerfile | 2 +- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci-workflow.yml b/.github/workflows/ci-workflow.yml index e7b59ff..7f4b135 100644 --- a/.github/workflows/ci-workflow.yml +++ b/.github/workflows/ci-workflow.yml @@ -17,6 +17,9 @@ on: jobs: # This workflow contains a single job called "build" build: + env: + DOCKER_BUILDKIT: '1' + DOCKER_BUILD_OPTS: "--cache-from=localhost:5000/build-cache --build-arg BUILDKIT_INLINE_CACHE=1" # The type of runner that the job will run on runs-on: ubuntu-latest @@ -24,18 +27,27 @@ jobs: steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 - - name: Prepare docker build cache - run: | - echo ${{ secrets.GITHUB_TOKEN }} | docker login docker.pkg.github.com -u $GITHUB_ACTOR --password-stdin - docker pull docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache || true - export DOCKER_BUILD_OPTS="--cache-from=docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache" + - id: cache-docker + uses: actions/cache@v1 + with: + path: /tmp/docker-registry + key: docker-registry-buildkit-${{ hashFiles('Dockerfile', 'Makefile', 'pom.xml') }} + - run: | + docker run -d -p 5000:5000 --restart=always --name registry -v /tmp/docker-registry:/var/lib/registry registry:2 && npx wait-on tcp:5000 + make build + docker tag netty-incubator-buffer:build localhost:5000/build-cache && docker push localhost:5000/build-cache || true +# - name: Prepare docker build cache +# run: | +# echo ${{ secrets.GITHUB_TOKEN }} | docker login docker.pkg.github.com -u $GITHUB_ACTOR --password-stdin +# docker pull docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache || true +# export DOCKER_BUILD_OPTS="--cache-from=docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache" # Run the make script - - name: Make build - run: make build +# - name: Make build +# run: make build # Cache build outputs - - name: Save docker build cache - run: | - docker tag netty-incubator-buffer:build docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache - docker push docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache || true +# - name: Save docker build cache +# run: | +# docker tag netty-incubator-buffer:build docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache +# docker push docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache || true diff --git a/Dockerfile b/Dockerfile index 8449620..90d11c5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Prepare environment -FROM fedora:latest +FROM fedora:33 RUN dnf -y install file findutils unzip zip libXtst-devel libXt-devel libXrender-devel libXrandr-devel \ libXi-devel cups-devel fontconfig-devel alsa-lib-devel make autoconf diffutils git clang \ java-latest-openjdk-devel From b171449de9fee3c5b1b89e919e9ea0777ccc004b Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Sat, 21 Nov 2020 17:00:30 +0100 Subject: [PATCH 8/8] Try yet another different caching mechanism --- .github/workflows/ci-workflow.yml | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci-workflow.yml b/.github/workflows/ci-workflow.yml index 7f4b135..096213e 100644 --- a/.github/workflows/ci-workflow.yml +++ b/.github/workflows/ci-workflow.yml @@ -17,9 +17,9 @@ on: jobs: # This workflow contains a single job called "build" build: - env: - DOCKER_BUILDKIT: '1' - DOCKER_BUILD_OPTS: "--cache-from=localhost:5000/build-cache --build-arg BUILDKIT_INLINE_CACHE=1" +# env: +# DOCKER_BUILDKIT: '1' +# DOCKER_BUILD_OPTS: "--cache-from=localhost:5000/build-cache --build-arg BUILDKIT_INLINE_CACHE=1" # The type of runner that the job will run on runs-on: ubuntu-latest @@ -27,15 +27,17 @@ jobs: steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 - - id: cache-docker - uses: actions/cache@v1 - with: - path: /tmp/docker-registry - key: docker-registry-buildkit-${{ hashFiles('Dockerfile', 'Makefile', 'pom.xml') }} - - run: | - docker run -d -p 5000:5000 --restart=always --name registry -v /tmp/docker-registry:/var/lib/registry registry:2 && npx wait-on tcp:5000 - make build - docker tag netty-incubator-buffer:build localhost:5000/build-cache && docker push localhost:5000/build-cache || true + - uses: satackey/action-docker-layer-caching@v0.0.8 + continue-on-error: true +# - id: cache-docker +# uses: actions/cache@v1 +# with: +# path: /tmp/docker-registry +# key: docker-registry-buildkit-${{ hashFiles('Dockerfile', 'Makefile', 'pom.xml') }} +# - run: | +# docker run -d -p 5000:5000 --restart=always --name registry -v /tmp/docker-registry:/var/lib/registry registry:2 && npx wait-on tcp:5000 +# make build +# docker tag netty-incubator-buffer:build localhost:5000/build-cache && docker push localhost:5000/build-cache || true # - name: Prepare docker build cache # run: | # echo ${{ secrets.GITHUB_TOKEN }} | docker login docker.pkg.github.com -u $GITHUB_ACTOR --password-stdin @@ -43,8 +45,8 @@ jobs: # export DOCKER_BUILD_OPTS="--cache-from=docker.pkg.github.com/$GITHUB_REPOSITORY/build-cache" # Run the make script -# - name: Make build -# run: make build + - name: Make build + run: make build # Cache build outputs # - name: Save docker build cache