From a3f6ae6be895ee0272eb9b9e8aa3bcfc8234f46c Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Thu, 26 Nov 2020 11:31:23 +0100 Subject: [PATCH] Make Buf.send() faster MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When send() a confined buffer, we had to first turn it into a shard buffer, so that it could be claimed by an arbitrary recipient thread. As we've learned, however, closing shared segments is expensive. We can speed up the send() call by simply leaving the segment shared. This weakens the confinement of the received segment, though. Currently no tests fails on that, but in the future we should re-implement confinement checking inside the Buf implementations themselves any, because pooled buffers also violate the confinement restriction, and we have a guiding principle that all buffers, regardless of implementation, should always behave the same. The results of this change can be observed in the MemorySegmentClosedByCleanerBenchmark, with the heavy workload. Explicitly closed segments now run the workload twice as fast, and the cleaner based closing is now 3 times faster. Before: Benchmark (workload) Mode Cnt Score Error Units MemorySegmentClosedByCleanerBenchmark.cleanerClose heavy avgt 150 42,221 ± 0,943 us/op MemorySegmentClosedByCleanerBenchmark.explicitClose heavy avgt 150 65,215 ± 0,761 us/op After: Benchmark (workload) Mode Cnt Score Error Units MemorySegmentClosedByCleanerBenchmark.cleanerClose heavy avgt 150 13,871 ± 0,544 us/op MemorySegmentClosedByCleanerBenchmark.explicitClose heavy avgt 150 37,516 ± 0,426 us/op --- src/main/java/io/netty/buffer/api/memseg/MemSegBuf.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/io/netty/buffer/api/memseg/MemSegBuf.java b/src/main/java/io/netty/buffer/api/memseg/MemSegBuf.java index c7cea45..3367c57 100644 --- a/src/main/java/io/netty/buffer/api/memseg/MemSegBuf.java +++ b/src/main/java/io/netty/buffer/api/memseg/MemSegBuf.java @@ -712,8 +712,7 @@ class MemSegBuf extends RcSupport implements Buf { return new Owned() { @Override public MemSegBuf transferOwnership(Drop drop) { - var newSegment = isConfined? transferSegment.handoff(Thread.currentThread()) : transferSegment; - MemSegBuf copy = new MemSegBuf(newSegment, drop, alloc); + MemSegBuf copy = new MemSegBuf(transferSegment, drop, alloc); copy.order = outer.order; copy.roff = outer.roff; copy.woff = outer.woff;