From b9e507c9cc4ae7105f22c7297fe40a322e4ed92f Mon Sep 17 00:00:00 2001 From: Matthias Einwag Date: Fri, 3 Oct 2014 23:35:46 +0200 Subject: [PATCH] Avoid vectored writes for small websocket messages Motivation: The WebSocket08FrameEncoder contains an optimization path for small messages which copies the message content into the header buffer to avoid vectored writes. However this path is in the current implementation never taken because the target buffer is preallocated only for exactly the size of the header. Modification: For messages below a certain treshold allocate the buffer so that the message can be directly copied. Thereby the optimized path is taken. Result: A speedup of about 25% for 100byte messages. Declines with bigger message sizes. I have currently set the treshold to 1kB which is a point where I could still see a few percent speedup, but we should also avoid burning too many CPU cycles. --- .../http/websocketx/WebSocket08FrameEncoder.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameEncoder.java index 2bcc87442d..efc072034c 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocket08FrameEncoder.java @@ -81,6 +81,14 @@ public class WebSocket08FrameEncoder extends MessageToMessageEncoder + * Masked messages will always be sent at once. + */ + private static final int GATHERING_WRITE_TRESHOLD = 1024; + private final boolean maskPayload; /** @@ -140,7 +148,7 @@ public class WebSocket08FrameEncoder extends MessageToMessageEncoder