Skip to content

Commit 02eb336

Browse files
daschlMichael Nitschinger
authored and
Michael Nitschinger
committed
JVMCBC-867: Do not grab ByteBuf slice when parsing server latency
During profiling with JFR I found that most of the allocations (when doing gets in a tight loop) on one of the IO threads is actually allocating a slice from the buffer pool for each server duration decoding. Since all we need is an offset into the response we can do the checking without allocating a slice. This reduces the allocations in a 6 minute run by 30% in the cb-io-kv-* thread where the op is performed. Your mileage will vary but overall this is another small step to improve efficiency. Change-Id: I1601297eb4604e77a1f3c6ef9baf277e94e2648e Reviewed-on: http://review.couchbase.org/c/couchbase-jvm-clients/+/132305 Tested-by: Build Bot <[email protected]> Reviewed-by: Graham Pople <[email protected]>
1 parent 34578bc commit 02eb336

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

core-io/src/main/java/com/couchbase/client/core/io/netty/kv/MemcacheProtocol.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -515,22 +515,24 @@ public static ByteBuf flexibleSyncReplication(final ByteBufAllocator alloc, fina
515515
* @return the extracted duration, 0 if not found.
516516
*/
517517
public static long parseServerDurationFromResponse(final ByteBuf response) {
518-
Optional<ByteBuf> frames = flexibleExtras(response);
519-
if (frames.isPresent()) {
520-
ByteBuf frame = frames.get();
521-
while (frame.readableBytes() > 0) {
522-
byte control = frame.readByte();
518+
int flexibleExtrasLength = response.getByte(0) == Magic.FLEXIBLE_RESPONSE.magic()
519+
? response.getByte(2)
520+
: 0;
521+
522+
if (flexibleExtrasLength > 0) {
523+
for (int offset = 0; offset < flexibleExtrasLength; offset++) {
524+
byte control = response.getByte(MemcacheProtocol.HEADER_SIZE + offset);
523525
byte id = (byte) (control & 0xF0);
524526
byte len = (byte) (control & 0x0F);
525527
if (id == FRAMING_EXTRAS_TRACING) {
526-
return Math.round(Math.pow(frame.readUnsignedShort(), 1.74) / 2);
527-
} else {
528-
frame.skipBytes(len);
528+
return Math.round(
529+
Math.pow(response.getUnsignedShort(MemcacheProtocol.HEADER_SIZE + offset + 1), 1.74) / 2
530+
);
529531
}
532+
offset += len;
530533
}
531-
} else {
532-
return 0;
533534
}
535+
534536
return 0;
535537
}
536538

0 commit comments

Comments
 (0)