This is an automated email from the ASF dual-hosted git repository.
vgalaxies pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-hugegraph.git
The following commit(s) were added to refs/heads/master by this push:
new f296a20b2 perf(core): optimize perf by avoid boxing long (#2861)
f296a20b2 is described below
commit f296a20b27d032960b6c107cbeebf45c9d4c23a9
Author: Jermy Li <[email protected]>
AuthorDate: Tue Sep 16 10:22:56 2025 +0800
perf(core): optimize perf by avoid boxing long (#2861)
* Query: E.checkArgument avoid boxing long
Change-Id: I6e08bd07b7c20c4025ed2a4fc91aaa3cbbd059e0
* BytesBuffer: E.checkArgument avoid Bytes.toHex and boxing long
Change-Id: Ib92f99f7a24480f44b8b400f49baefd21735fd20
* define const RANGE_TYPES to avoid construct every time
Change-Id: Idb95ad9136c3ce7b1ff2729b712b315a052ac441
---
.../apache/hugegraph/backend/query/Condition.java | 14 +--
.../org/apache/hugegraph/backend/query/Query.java | 65 ++++++++----
.../hugegraph/backend/serializer/BytesBuffer.java | 109 ++++++++++++++-------
3 files changed, 128 insertions(+), 60 deletions(-)
diff --git
a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Condition.java
b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Condition.java
index 3ef9cd5fa..09e223e4c 100644
---
a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Condition.java
+++
b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Condition.java
@@ -137,6 +137,12 @@ public abstract class Condition {
return true;
});
+ private static final Set<RelationType> RANGE_TYPES =
+ ImmutableSet.of(GT, GTE, LT, LTE);
+
+ private static final Set<RelationType> UNFLATTEN_TYPES =
+ ImmutableSet.of(IN, NOT_IN, TEXT_CONTAINS_ANY);
+
private final String operator;
private final BiFunction<Object, Object, Boolean> tester;
private final Class<?> v1Class;
@@ -261,7 +267,7 @@ public abstract class Condition {
}
public boolean isRangeType() {
- return ImmutableSet.of(GT, GTE, LT, LTE).contains(this);
+ return RANGE_TYPES.contains(this);
}
public boolean isSearchType() {
@@ -628,10 +634,6 @@ public abstract class Condition {
// The value serialized(code/string) by backend store.
protected Object serialValue;
- protected static final Set<RelationType> UNFLATTEN_RELATION_TYPES =
- ImmutableSet.of(RelationType.IN, RelationType.NOT_IN,
- RelationType.TEXT_CONTAINS_ANY);
-
@Override
public ConditionType type() {
return ConditionType.RELATION;
@@ -672,7 +674,7 @@ public abstract class Condition {
@Override
public boolean isFlattened() {
- return !UNFLATTEN_RELATION_TYPES.contains(this.relation);
+ return !RelationType.UNFLATTEN_TYPES.contains(this.relation);
}
@Override
diff --git
a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Query.java
b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Query.java
index 3a024283f..1816bff23 100644
---
a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Query.java
+++
b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/query/Query.java
@@ -180,7 +180,9 @@ public class Query implements Cloneable {
}
public void offset(long offset) {
- E.checkArgument(offset >= 0L, "Invalid offset %s", offset);
+ if (offset < 0L) {
+ E.checkArgument(false, "Invalid offset %s", offset);
+ }
this.offset = offset;
}
@@ -202,7 +204,9 @@ public class Query implements Cloneable {
}
public long goOffset(long offset) {
- E.checkArgument(offset >= 0L, "Invalid offset value: %s", offset);
+ if (offset < 0L) {
+ E.checkArgument(false, "Invalid offset value: %s", offset);
+ }
if (this.originQuery != null) {
this.goParentOffset(offset);
}
@@ -250,9 +254,11 @@ public class Query implements Cloneable {
} else if (fromIndex > 0L) {
this.goOffset(fromIndex);
}
- E.checkArgument(fromIndex <= Integer.MAX_VALUE,
- "Offset must be <= 0x7fffffff, but got '%s'",
- fromIndex);
+ if (fromIndex > Integer.MAX_VALUE) {
+ E.checkArgument(false,
+ "Offset must be <= 0x7fffffff, but got '%s'",
+ fromIndex);
+ }
if (fromIndex >= elems.size()) {
return ImmutableSet.of();
@@ -287,17 +293,27 @@ public class Query implements Cloneable {
public long limit() {
if (this.capacity != NO_CAPACITY) {
- E.checkArgument(this.limit == Query.NO_LIMIT ||
- this.limit <= this.capacity,
- "Invalid limit %s, must be <= capacity(%s)",
- this.limit, this.capacity);
+ if (this.limit == NO_LIMIT) {
+ /*
+ * TODO: may need to return this.capacity here.
+ * In most cases, capacity == DEFAULT_CAPACITY, then limit()
+ * will return NO_LIMIT, thus rely on Query.checkCapacity()
+ * to check in the next step.
+ */
+ }
+ if (this.limit != NO_LIMIT && this.limit > this.capacity) {
+ E.checkArgument(false,
+ "Invalid limit %s, must be <= capacity(%s)",
+ this.limit, this.capacity);
+ }
}
return this.limit;
}
public void limit(long limit) {
- E.checkArgument(limit >= 0L || limit == NO_LIMIT,
- "Invalid limit %s", limit);
+ if (limit != NO_LIMIT && limit < 0L) {
+ E.checkArgument(false, "Invalid limit %s", limit);
+ }
this.limit = limit;
}
@@ -337,8 +353,9 @@ public class Query implements Cloneable {
} else {
assert end < Query.NO_LIMIT;
}
- E.checkArgument(end >= start,
- "Invalid range: [%s, %s)", start, end);
+ if (end < start) {
+ E.checkArgument(false, "Invalid range: [%s, %s)", start, end);
+ }
this.limit(end - start);
} else {
// Keep the origin limit
@@ -349,11 +366,15 @@ public class Query implements Cloneable {
public String page() {
if (this.page != null) {
- E.checkState(this.limit() != 0L,
- "Can't set limit=0 when using paging");
- E.checkState(this.offset() == 0L,
- "Can't set offset when using paging, but got '%s'",
- this.offset());
+ if (this.limit() == 0L) {
+ E.checkState(false,
+ "Can't set limit=0 when using paging");
+ }
+ if (this.offset() != 0L) {
+ E.checkState(false,
+ "Can't set offset when using paging, but got
'%s'",
+ this.offset());
+ }
}
return this.page;
}
@@ -409,7 +430,7 @@ public class Query implements Cloneable {
public void checkCapacity(long count) throws LimitExceedException {
// Throw LimitExceedException if reach capacity
- if (this.capacity != Query.NO_CAPACITY && count > this.capacity) {
+ if (this.capacity != NO_CAPACITY && count > this.capacity) {
final int MAX_CHARS = 256;
String query = this.toString();
if (query.length() > MAX_CHARS) {
@@ -426,8 +447,10 @@ public class Query implements Cloneable {
}
public Aggregate aggregateNotNull() {
- E.checkArgument(this.aggregate != null,
- "The aggregate must be set for number query");
+ if (this.aggregate == null) {
+ E.checkArgument(false,
+ "The aggregate must be set for number query");
+ }
return this.aggregate;
}
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) {