This is an automated email from the ASF dual-hosted git repository.

jermy pushed a commit to branch checkArgument-avoid-boxing
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git

commit f1d95ea7f83f32d2c037ff9bb5a9457eba2af6a2
Author: Jermy Li <[email protected]>
AuthorDate: Sun Aug 31 00:50:47 2025 +0800

    BytesBuffer: E.checkArgument avoid Bytes.toHex and boxing long
    
    Change-Id: Ib92f99f7a24480f44b8b400f49baefd21735fd20
---
 .../hugegraph/backend/serializer/BytesBuffer.java  | 109 ++++++++++++++-------
 1 file changed, 76 insertions(+), 33 deletions(-)

diff --git 
a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java
 
b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java
index 2c890ebe4..fb5234767 100644
--- 
a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java
+++ 
b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/serializer/BytesBuffer.java
@@ -91,8 +91,11 @@ public final class BytesBuffer extends OutputStream {
     }
 
     public BytesBuffer(int capacity) {
-        E.checkArgument(capacity <= MAX_BUFFER_CAPACITY,
-                        "Capacity exceeds max buffer capacity: %s", 
MAX_BUFFER_CAPACITY);
+        if (capacity > MAX_BUFFER_CAPACITY) {
+            E.checkArgument(false,
+                            "Capacity %s exceeds max buffer capacity: %s",
+                            capacity, MAX_BUFFER_CAPACITY);
+        }
         this.buffer = ByteBuffer.allocate(capacity);
         this.resize = true;
     }
@@ -162,16 +165,21 @@ public final class BytesBuffer extends OutputStream {
 
     private void require(int size) {
         // Does need to resize?
-        if (this.buffer.limit() - this.buffer.position() >= size) {
+        if (this.buffer.remaining() >= size) {
             return;
         }
         // Can't resize for wrapped buffer since will change the origin ref
-        E.checkState(this.resize, "Can't resize for wrapped buffer");
+        if (!this.resize) {
+            E.checkState(false, "Can't resize for wrapped buffer");
+        }
 
         // Extra capacity as buffer
         int newCapacity = size + this.buffer.limit() + DEFAULT_CAPACITY;
-        E.checkArgument(newCapacity <= MAX_BUFFER_CAPACITY,
-                        "Capacity exceeds max buffer capacity: %s", 
MAX_BUFFER_CAPACITY);
+        if (newCapacity > MAX_BUFFER_CAPACITY) {
+            E.checkArgument(false,
+                            "Capacity %s exceeds max buffer capacity: %s",
+                            newCapacity, MAX_BUFFER_CAPACITY);
+        }
         ByteBuffer newBuffer = ByteBuffer.allocate(newCapacity);
         this.buffer.flip();
         newBuffer.put(this.buffer);
@@ -249,7 +257,7 @@ public final class BytesBuffer extends OutputStream {
     }
 
     public byte peekLast() {
-        return this.buffer.get(this.buffer.capacity() - 1);
+        return this.buffer.get(this.buffer.limit() - 1);
     }
 
     public byte read() {
@@ -291,8 +299,11 @@ public final class BytesBuffer extends OutputStream {
     }
 
     public BytesBuffer writeBytes(byte[] bytes) {
-        E.checkArgument(bytes.length <= BYTES_LEN_MAX, "The max length of 
bytes is %s, but got %s",
-                        BYTES_LEN_MAX, bytes.length);
+        if (bytes.length > BYTES_LEN_MAX) {
+            E.checkArgument(false,
+                            "The max length of bytes is %s, but got %s",
+                            BYTES_LEN_MAX, bytes.length);
+        }
         require(BYTES_LEN + bytes.length);
         this.writeVInt(bytes.length);
         this.write(bytes);
@@ -306,9 +317,12 @@ public final class BytesBuffer extends OutputStream {
     }
 
     public BytesBuffer writeBigBytes(byte[] bytes) {
-        // TODO: note the max blob size should be 128MB (due to 
MAX_BUFFER_CAPACITY)
-        E.checkArgument(bytes.length <= BLOB_LEN_MAX, "The max length of bytes 
is %s, but got %s",
-                        BLOB_LEN_MAX, bytes.length);
+        if (bytes.length > BLOB_LEN_MAX) {
+            // TODO: note the max blob size should be 128MB (due to 
MAX_BUFFER_CAPACITY)
+            E.checkArgument(false,
+                            "The max length of bytes is %s, but got %s",
+                            BLOB_LEN_MAX, bytes.length);
+        }
         require(BLOB_LEN + bytes.length);
         this.writeVInt(bytes.length);
         this.write(bytes);
@@ -430,8 +444,11 @@ public final class BytesBuffer extends OutputStream {
 
     public int readVInt() {
         byte leading = this.read();
-        E.checkArgument(leading != 0x80, "Unexpected varint with leading byte 
'0x%s'",
-                        Bytes.toHex(leading));
+        if (leading == 0x80) {
+            E.checkArgument(false,
+                            "Unexpected varint with leading byte '0x%s'",
+                            Bytes.toHex(leading));
+        }
         int value = leading & 0x7f;
         if (leading >= 0) {
             assert (leading & 0x80) == 0;
@@ -449,11 +466,16 @@ public final class BytesBuffer extends OutputStream {
             }
         }
 
-        E.checkArgument(i < 5, "Unexpected varint %s with too many bytes(%s)",
-                        value, i + 1);
-        E.checkArgument(i < 4 || (leading & 0x70) == 0,
-                        "Unexpected varint %s with leading byte '0x%s'",
-                        value, Bytes.toHex(leading));
+        if (i >= 5) {
+            E.checkArgument(false,
+                            "Unexpected varint %s with too many bytes(%s)",
+                            value, i + 1);
+        }
+        if (i >= 4 && (leading & 0x70) != 0) {
+            E.checkArgument(false,
+                            "Unexpected varint %s with leading byte '0x%s'",
+                            value, Bytes.toHex(leading));
+        }
         return value;
     }
 
@@ -492,8 +514,11 @@ public final class BytesBuffer extends OutputStream {
 
     public long readVLong() {
         byte leading = this.read();
-        E.checkArgument(leading != 0x80, "Unexpected varlong with leading byte 
'0x%s'",
-                        Bytes.toHex(leading));
+        if (leading == 0x80) {
+            E.checkArgument(false,
+                            "Unexpected varlong with leading byte '0x%s'",
+                            Bytes.toHex(leading));
+        }
         long value = leading & 0x7fL;
         if (leading >= 0) {
             assert (leading & 0x80) == 0;
@@ -511,11 +536,16 @@ public final class BytesBuffer extends OutputStream {
             }
         }
 
-        E.checkArgument(i < 10, "Unexpected varlong %s with too many 
bytes(%s)",
-                        value, i + 1);
-        E.checkArgument(i < 9 || (leading & 0x7e) == 0,
-                        "Unexpected varlong %s with leading byte '0x%s'",
-                        value, Bytes.toHex(leading));
+        if (i >= 10) {
+            E.checkArgument(false,
+                            "Unexpected varlong %s with too many bytes(%s)",
+                            value, i + 1);
+        }
+        if (i >= 9 && (leading & 0x7e) != 0) {
+            E.checkArgument(false,
+                            "Unexpected varlong %s with leading byte '0x%s'",
+                            value, Bytes.toHex(leading));
+        }
         return value;
     }
 
@@ -648,9 +678,13 @@ public final class BytesBuffer extends OutputStream {
                 // String Id (VertexID)
                 bytes = id.asBytes();
                 int len = bytes.length;
-                E.checkArgument(len > 0, "Can't write empty id");
-                E.checkArgument(len <= ID_LEN_MAX, "Big id max length is %s, 
but got %s {%s}",
-                                ID_LEN_MAX, len, id);
+                if (len <= 0) {
+                    E.checkArgument(false, "Can't write empty id");
+                }
+                if (len > ID_LEN_MAX) {
+                    E.checkArgument(false, "Big id max length is %s, but got 
%s {%s}",
+                                    ID_LEN_MAX, len, id);
+                }
                 len -= 1; // mapping [1, 16384] to [0, 16383]
                 if (len <= 0x3f) {
                     // If length is <= 63, use a single byte with the highest 
bit set to 1
@@ -721,7 +755,9 @@ public final class BytesBuffer extends OutputStream {
     public BytesBuffer writeIndexId(Id id, HugeType type, boolean withEnding) {
         byte[] bytes = id.asBytes();
         int len = bytes.length;
-        E.checkArgument(len > 0, "Can't write empty id");
+        if (len == 0) {
+            E.checkArgument(false, "Can't write empty id");
+        }
 
         this.write(bytes);
         if (type.isStringIndex()) {
@@ -873,8 +909,11 @@ public final class BytesBuffer extends OutputStream {
     }
 
     private long readNumber(byte b) {
-        E.checkArgument((b & 0x80) == 0, "Not a number type with prefix byte 
'0x%s'",
-                        Bytes.toHex(b));
+        if ((b & 0x80) != 0) {
+            E.checkArgument(false,
+                            "Not a number type with prefix byte '0x%s'",
+                            Bytes.toHex(b));
+        }
         // Parse the kind from byte 0kkksxxx
         int kind = b >>> 4;
         boolean positive = (b & 0x08) > 0;
@@ -930,7 +969,11 @@ public final class BytesBuffer extends OutputStream {
                 break;
             }
         }
-        E.checkArgument(foundEnding, "Not found ending '0x%s'", 
Bytes.toHex(STRING_ENDING_BYTE));
+        if (!foundEnding) {
+            E.checkArgument(false,
+                            "Not found ending '0x%s'",
+                            Bytes.toHex(STRING_ENDING_BYTE));
+        }
         int end = this.buffer.position() - 1;
         int len = end - start;
         if (len <= 0) {

Reply via email to