Skip to content

Commit 3d8be7f

Browse files
authored
perf(core): StringId hold bytes to avoid decode/encode (#2862)
1 parent f296a20 commit 3d8be7f

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.hugegraph.backend.id.Id.IdType;
2424
import org.apache.hugegraph.backend.serializer.BytesBuffer;
2525
import org.apache.hugegraph.structure.HugeVertex;
26+
import org.apache.hugegraph.util.Bytes;
2627
import org.apache.hugegraph.util.E;
2728
import org.apache.hugegraph.util.LongEncoding;
2829
import org.apache.hugegraph.util.NumericUtil;
@@ -128,14 +129,20 @@ public static int compareType(Id id1, Id id2) {
128129
public static class StringId implements Id {
129130

130131
protected String id;
132+
protected byte[] bytes;
131133

132134
public StringId(String id) {
133-
E.checkArgument(!id.isEmpty(), "The id can't be empty");
135+
E.checkArgument(id != null && !id.isEmpty(),
136+
"The id can't be null or empty");
134137
this.id = id;
138+
this.bytes = null;
135139
}
136140

137141
public StringId(byte[] bytes) {
138-
this.id = StringEncoding.decode(bytes);
142+
E.checkArgument(bytes != null && bytes.length > 0,
143+
"The id bytes can't be null or empty");
144+
this.bytes = bytes;
145+
this.id = null;
139146
}
140147

141148
@Override
@@ -145,27 +152,35 @@ public IdType type() {
145152

146153
@Override
147154
public Object asObject() {
148-
return this.id;
155+
return this.asString();
149156
}
150157

151158
@Override
152159
public String asString() {
160+
if (this.id == null) {
161+
assert this.bytes != null;
162+
this.id = StringEncoding.decode(this.bytes);
163+
}
153164
return this.id;
154165
}
155166

156167
@Override
157168
public long asLong() {
158-
return Long.parseLong(this.id);
169+
return Long.parseLong(this.asString());
159170
}
160171

161172
@Override
162173
public byte[] asBytes() {
163-
return StringEncoding.encode(this.id);
174+
if (this.bytes == null) {
175+
assert this.id != null;
176+
this.bytes = StringEncoding.encode(this.id);
177+
}
178+
return this.bytes;
164179
}
165180

166181
@Override
167182
public int length() {
168-
return this.id.length();
183+
return this.asString().length();
169184
}
170185

171186
@Override
@@ -174,25 +189,37 @@ public int compareTo(Id other) {
174189
if (cmp != 0) {
175190
return cmp;
176191
}
177-
return this.id.compareTo(other.asString());
192+
if (this.id != null) {
193+
return this.id.compareTo(other.asString());
194+
} else {
195+
return Bytes.compare(this.bytes, other.asBytes());
196+
}
178197
}
179198

180199
@Override
181200
public int hashCode() {
182-
return this.id.hashCode();
201+
return this.asString().hashCode();
183202
}
184203

185204
@Override
186-
public boolean equals(Object other) {
187-
if (!(other instanceof StringId)) {
205+
public boolean equals(Object obj) {
206+
if (!(obj instanceof StringId)) {
188207
return false;
189208
}
190-
return this.id.equals(((StringId) other).id);
209+
StringId other = (StringId) obj;
210+
if (this.id != null) {
211+
return this.id.equals(other.asString());
212+
} else if (other.bytes == null) {
213+
return this.asString().equals(other.asString());
214+
} else {
215+
assert this.bytes != null;
216+
return Bytes.equals(this.bytes, other.asBytes());
217+
}
191218
}
192219

193220
@Override
194221
public String toString() {
195-
return this.id;
222+
return this.asString();
196223
}
197224
}
198225

hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/memory/consumer/impl/id/StringIdOffHeap.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ public Object zeroCopyReadFromByteBuf() {
6161

6262
@Override
6363
public void serializeSelfToByteBuf(MemoryPool memoryPool) {
64-
byte[] stringBytes = id.getBytes((StandardCharsets.UTF_8));
64+
byte[] stringBytes;
65+
if (this.bytes != null) {
66+
stringBytes = this.bytes;
67+
} else {
68+
stringBytes = this.id.getBytes((StandardCharsets.UTF_8));
69+
}
6570
this.idOffHeap = (ByteBuf) memoryPool.requireMemory(stringBytes.length, memoryPool);
6671
this.idOffHeap.markReaderIndex();
6772
this.idOffHeap.writeBytes(stringBytes);
@@ -70,6 +75,7 @@ public void serializeSelfToByteBuf(MemoryPool memoryPool) {
7075
@Override
7176
public void releaseOriginalVarsOnHeap() {
7277
this.id = null;
78+
this.bytes = null;
7379
}
7480

7581
@Override

0 commit comments

Comments
 (0)