From 7202d54c34d906fd62a8e79b3ad68ae6856ebe4b Mon Sep 17 00:00:00 2001 From: Jermy Li Date: Sun, 31 Aug 2025 01:19:20 +0800 Subject: [PATCH 1/2] perf(core): StringId hold bytes to avoid decode/encode Change-Id: I1ff93a2ee31f142fb7ab5cfe0e038ef2da2f982d --- .../hugegraph/backend/id/IdGenerator.java | 43 ++++++++++++++----- .../consumer/impl/id/StringIdOffHeap.java | 1 + 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java index 17cc11684c..385555bdf9 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java @@ -23,6 +23,7 @@ import org.apache.hugegraph.backend.id.Id.IdType; import org.apache.hugegraph.backend.serializer.BytesBuffer; import org.apache.hugegraph.structure.HugeVertex; +import org.apache.hugegraph.util.Bytes; import org.apache.hugegraph.util.E; import org.apache.hugegraph.util.LongEncoding; import org.apache.hugegraph.util.NumericUtil; @@ -128,14 +129,19 @@ public static int compareType(Id id1, Id id2) { public static class StringId implements Id { protected String id; + protected byte[] bytes; public StringId(String id) { E.checkArgument(!id.isEmpty(), "The id can't be empty"); this.id = id; + this.bytes = null; } public StringId(byte[] bytes) { - this.id = StringEncoding.decode(bytes); + E.checkArgument(bytes != null && bytes.length > 0, + "The id bytes can't be null or empty"); + this.bytes = bytes; + this.id = null; } @Override @@ -145,27 +151,33 @@ public IdType type() { @Override public Object asObject() { - return this.id; + return this.asString(); } @Override public String asString() { + if (this.id == null) { + this.id = StringEncoding.decode(this.bytes); + } return this.id; } @Override public long asLong() { - return Long.parseLong(this.id); + return Long.parseLong(this.asString()); } @Override public byte[] asBytes() { - return StringEncoding.encode(this.id); + if (this.bytes == null) { + this.bytes = StringEncoding.encode(this.id); + } + return this.bytes; } @Override public int length() { - return this.id.length(); + return this.asString().length(); } @Override @@ -174,25 +186,34 @@ public int compareTo(Id other) { if (cmp != 0) { return cmp; } - return this.id.compareTo(other.asString()); + if (this.id != null) { + return this.id.compareTo(other.asString()); + } else { + return Bytes.compare(this.bytes, other.asBytes()); + } } @Override public int hashCode() { - return this.id.hashCode(); + return this.asString().hashCode(); } @Override - public boolean equals(Object other) { - if (!(other instanceof StringId)) { + public boolean equals(Object obj) { + if (!(obj instanceof StringId)) { return false; } - return this.id.equals(((StringId) other).id); + StringId other = (StringId) obj; + if (this.id != null) { + return this.id.equals(other.asString()); + } else { + return Bytes.equals(this.bytes, other.asBytes()); + } } @Override public String toString() { - return this.id; + return this.asString(); } } diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/consumer/impl/id/StringIdOffHeap.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/consumer/impl/id/StringIdOffHeap.java index be96c2c963..98bdc25ab5 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/consumer/impl/id/StringIdOffHeap.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/consumer/impl/id/StringIdOffHeap.java @@ -70,6 +70,7 @@ public void serializeSelfToByteBuf(MemoryPool memoryPool) { @Override public void releaseOriginalVarsOnHeap() { this.id = null; + this.bytes = null; } @Override From d705eacc538bebb8462b6d02b9c6d6bc95d0897b Mon Sep 17 00:00:00 2001 From: Jermy Li Date: Sun, 7 Sep 2025 21:13:22 +0800 Subject: [PATCH 2/2] fix comments Change-Id: I436858dbad61ecc2e83861b05a6620a46b6df245 --- .../java/org/apache/hugegraph/backend/id/IdGenerator.java | 8 +++++++- .../memory/consumer/impl/id/StringIdOffHeap.java | 7 ++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java index 385555bdf9..9f1bcc56fc 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java @@ -132,7 +132,8 @@ public static class StringId implements Id { protected byte[] bytes; public StringId(String id) { - E.checkArgument(!id.isEmpty(), "The id can't be empty"); + E.checkArgument(id != null && !id.isEmpty(), + "The id can't be null or empty"); this.id = id; this.bytes = null; } @@ -157,6 +158,7 @@ public Object asObject() { @Override public String asString() { if (this.id == null) { + assert this.bytes != null; this.id = StringEncoding.decode(this.bytes); } return this.id; @@ -170,6 +172,7 @@ public long asLong() { @Override public byte[] asBytes() { if (this.bytes == null) { + assert this.id != null; this.bytes = StringEncoding.encode(this.id); } return this.bytes; @@ -206,7 +209,10 @@ public boolean equals(Object obj) { StringId other = (StringId) obj; if (this.id != null) { return this.id.equals(other.asString()); + } else if (other.bytes == null) { + return this.asString().equals(other.asString()); } else { + assert this.bytes != null; return Bytes.equals(this.bytes, other.asBytes()); } } diff --git a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/consumer/impl/id/StringIdOffHeap.java b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/consumer/impl/id/StringIdOffHeap.java index 98bdc25ab5..d026583821 100644 --- a/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/consumer/impl/id/StringIdOffHeap.java +++ b/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/consumer/impl/id/StringIdOffHeap.java @@ -61,7 +61,12 @@ public Object zeroCopyReadFromByteBuf() { @Override public void serializeSelfToByteBuf(MemoryPool memoryPool) { - byte[] stringBytes = id.getBytes((StandardCharsets.UTF_8)); + byte[] stringBytes; + if (this.bytes != null) { + stringBytes = this.bytes; + } else { + stringBytes = this.id.getBytes((StandardCharsets.UTF_8)); + } this.idOffHeap = (ByteBuf) memoryPool.requireMemory(stringBytes.length, memoryPool); this.idOffHeap.markReaderIndex(); this.idOffHeap.writeBytes(stringBytes);