Merge pull request #4564 from SerCeMan/jetty-9.4.x

Improve performance of small websocket binary messages
This commit is contained in:
Lachlan 2020-02-11 00:40:27 +11:00 committed by GitHub
commit 636c6047ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View File

@ -51,7 +51,7 @@ public class BinaryWholeMessage extends SimpleBinaryMessage
{ {
super.finished = true; super.finished = true;
byte[] data = out.toByteArray(); byte[] data = (out == null) ? new byte[]{} : out.toByteArray();
DecoderFactory.Wrapper decoder = msgWrapper.getDecoder(); DecoderFactory.Wrapper decoder = msgWrapper.getDecoder();
Decoder.Binary<Object> binaryDecoder = (Binary<Object>)decoder.getDecoder(); Decoder.Binary<Object> binaryDecoder = (Binary<Object>)decoder.getDecoder();

View File

@ -18,25 +18,24 @@
package org.eclipse.jetty.websocket.common.message; package org.eclipse.jetty.websocket.common.message;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.ByteArrayOutputStream2;
import org.eclipse.jetty.websocket.common.events.EventDriver; import org.eclipse.jetty.websocket.common.events.EventDriver;
public class SimpleBinaryMessage implements MessageAppender public class SimpleBinaryMessage implements MessageAppender
{ {
private static final int BUFFER_SIZE = 65535; private static final int BUFFER_SIZE = 65535;
private final EventDriver onEvent; private final EventDriver onEvent;
protected final ByteArrayOutputStream out; protected ByteArrayOutputStream2 out;
private int size; private int size;
protected boolean finished; protected boolean finished;
public SimpleBinaryMessage(EventDriver onEvent) public SimpleBinaryMessage(EventDriver onEvent)
{ {
this.onEvent = onEvent; this.onEvent = onEvent;
this.out = new ByteArrayOutputStream(BUFFER_SIZE);
finished = false; finished = false;
} }
@ -57,6 +56,8 @@ public class SimpleBinaryMessage implements MessageAppender
onEvent.getPolicy().assertValidBinaryMessageSize(size + payload.remaining()); onEvent.getPolicy().assertValidBinaryMessageSize(size + payload.remaining());
size += payload.remaining(); size += payload.remaining();
if (out == null)
out = isLast ? new ByteArrayOutputStream2(size) : new ByteArrayOutputStream2(BUFFER_SIZE);
BufferUtil.writeTo(payload, out); BufferUtil.writeTo(payload, out);
} }
@ -64,7 +65,13 @@ public class SimpleBinaryMessage implements MessageAppender
public void messageComplete() public void messageComplete()
{ {
finished = true; finished = true;
byte[] data = out.toByteArray(); byte[] data;
if (out == null)
data = new byte[]{};
else if (out.getCount() == out.getBuf().length)
data = out.getBuf();
else
data = out.toByteArray();
onEvent.onBinaryMessage(data); onEvent.onBinaryMessage(data);
} }
} }