diff options
author | Håvard Pettersen <havardpe@oath.com> | 2021-06-15 11:47:17 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@oath.com> | 2021-06-15 11:47:57 +0000 |
commit | 44974c98740ab358e4157c8c4e0358312873ebb3 (patch) | |
tree | 657733046c5fd72cf8356fae64c6d2c08f4adeab /jrt/src | |
parent | 062427cbeacac0f2850558976da56a7789983b62 (diff) |
try to use less memory for buffers
- fix default 64k max sizes
- start with empty buffers
- simplify Buffer class
Diffstat (limited to 'jrt/src')
-rw-r--r-- | jrt/src/com/yahoo/jrt/Buffer.java | 19 | ||||
-rw-r--r-- | jrt/src/com/yahoo/jrt/Connection.java | 40 | ||||
-rw-r--r-- | jrt/src/com/yahoo/jrt/Supervisor.java | 13 |
3 files changed, 22 insertions, 50 deletions
diff --git a/jrt/src/com/yahoo/jrt/Buffer.java b/jrt/src/com/yahoo/jrt/Buffer.java index 937666a28ae..06a658740e5 100644 --- a/jrt/src/com/yahoo/jrt/Buffer.java +++ b/jrt/src/com/yahoo/jrt/Buffer.java @@ -6,9 +6,6 @@ import java.nio.ByteBuffer; class Buffer { - - static final int MAX_IO = 65000; - private ByteBuffer buf; private int readPos; private int writePos; @@ -111,20 +108,4 @@ class Buffer { ensureFree(minFree); return buf; } - - public ByteBuffer getChannelReadable() { - ByteBuffer bb = getReadable(); - if (bb.remaining() > MAX_IO) { - bb.limit(bb.position() + MAX_IO); - } - return bb; - } - - public ByteBuffer getChannelWritable(int minFree) { - ByteBuffer bb = getWritable(minFree); - if (bb.remaining() > MAX_IO) { - bb.limit(bb.position() + MAX_IO); - } - return bb; - } } diff --git a/jrt/src/com/yahoo/jrt/Connection.java b/jrt/src/com/yahoo/jrt/Connection.java index 891558684ed..6158576348a 100644 --- a/jrt/src/com/yahoo/jrt/Connection.java +++ b/jrt/src/com/yahoo/jrt/Connection.java @@ -19,9 +19,9 @@ class Connection extends Target { private static final Logger log = Logger.getLogger(Connection.class.getName()); - private static final int READ_SIZE = 32768; + private static final int READ_SIZE = 16*1024; private static final int READ_REDO = 10; - private static final int WRITE_SIZE = 32768; + private static final int WRITE_SIZE = 16*1024; private static final int WRITE_REDO = 10; private static final int INITIAL = 0; @@ -32,11 +32,11 @@ class Connection extends Target { private int state = INITIAL; private final Queue queue = new Queue(); private final Queue myQueue = new Queue(); - private final Buffer input = new Buffer(0x1000); // Start off with small buffer. - private final Buffer output = new Buffer(0x1000); // Start off with small buffer. - private int maxInputSize = 64*1024; - private int maxOutputSize = 64*1024; - private boolean dropEmptyBuffers = false; + private final Buffer input = new Buffer(0); // Start off with empty buffer. + private final Buffer output = new Buffer(0); // Start off with empty buffer. + private final int maxInputSize; + private final int maxOutputSize; + private final boolean dropEmptyBuffers; private final boolean tcpNoDelay; private final Map<Integer, ReplyHandler> replyMap = new HashMap<>(); private final Map<TargetWatcher, TargetWatcher> watchers = new IdentityHashMap<>(); @@ -98,6 +98,9 @@ class Connection extends Target { this.socket = parent.transport().createServerCryptoSocket(channel); this.spec = null; this.tcpNoDelay = tcpNoDelay; + maxInputSize = owner.getMaxInputBufferSize(); + maxOutputSize = owner.getMaxOutputBufferSize(); + dropEmptyBuffers = owner.getDropEmptyBuffers(); server = true; owner.sessionInit(this); } @@ -108,22 +111,13 @@ class Connection extends Target { this.owner = owner; this.spec = spec; this.tcpNoDelay = tcpNoDelay; + maxInputSize = owner.getMaxInputBufferSize(); + maxOutputSize = owner.getMaxOutputBufferSize(); + dropEmptyBuffers = owner.getDropEmptyBuffers(); server = false; owner.sessionInit(this); } - public void setMaxInputSize(int bytes) { - maxInputSize = bytes; - } - - public void setMaxOutputSize(int bytes) { - maxOutputSize = bytes; - } - - public void setDropEmptyBuffers(boolean value) { - dropEmptyBuffers = value; - } - public TransportThread transportThread() { return parent; } @@ -235,7 +229,7 @@ class Connection extends Target { readSize = socket.getMinimumReadBufferSize(); } setState(CONNECTED); - while (socket.drain(input.getChannelWritable(readSize)) > 0) { + while (socket.drain(input.getWritable(readSize)) > 0) { handlePackets(); } break; @@ -302,14 +296,14 @@ class Connection extends Target { private void read() throws IOException { boolean doneRead = false; for (int i = 0; !doneRead && i < READ_REDO; i++) { - ByteBuffer wb = input.getChannelWritable(readSize); + ByteBuffer wb = input.getWritable(readSize); if (socket.read(wb) == -1) { throw new IOException("jrt: Connection closed by peer"); } doneRead = (wb.remaining() > 0); handlePackets(); } - while (socket.drain(input.getChannelWritable(readSize)) > 0) { + while (socket.drain(input.getWritable(readSize)) > 0) { handlePackets(); } if (dropEmptyBuffers) { @@ -346,7 +340,7 @@ class Connection extends Target { owner.writePacket(info); info.encodePacket(packet, wb); } - ByteBuffer rb = output.getChannelReadable(); + ByteBuffer rb = output.getReadable(); if (rb.remaining() == 0) { break; } diff --git a/jrt/src/com/yahoo/jrt/Supervisor.java b/jrt/src/com/yahoo/jrt/Supervisor.java index 5975e191a5b..b82664b2f56 100644 --- a/jrt/src/com/yahoo/jrt/Supervisor.java +++ b/jrt/src/com/yahoo/jrt/Supervisor.java @@ -21,8 +21,8 @@ public class Supervisor { private SessionHandler sessionHandler = null; private final Object methodMapLock = new Object(); private final AtomicReference<HashMap<String, Method>> methodMap = new AtomicReference<>(new HashMap<>()); - private int maxInputBufferSize = 0; - private int maxOutputBufferSize = 0; + private int maxInputBufferSize = 64*1024; + private int maxOutputBufferSize = 64*1024; private boolean dropEmptyBuffers = false; /** @@ -47,6 +47,7 @@ public class Supervisor { dropEmptyBuffers = value; return this; } + boolean getDropEmptyBuffers() { return dropEmptyBuffers; } /** * Set maximum input buffer size. This value will only affect @@ -61,6 +62,7 @@ public class Supervisor { public void setMaxInputBufferSize(int bytes) { maxInputBufferSize = bytes; } + int getMaxInputBufferSize() { return maxInputBufferSize; } /** * Set maximum output buffer size. This value will only affect @@ -75,6 +77,7 @@ public class Supervisor { public void setMaxOutputBufferSize(int bytes) { maxOutputBufferSize = bytes; } + int getMaxOutputBufferSize() { return maxOutputBufferSize; } /** * Obtain the method map for this Supervisor @@ -192,12 +195,6 @@ public class Supervisor { * @param target the target **/ void sessionInit(Target target) { - if (target instanceof Connection) { - Connection conn = (Connection) target; - conn.setMaxInputSize(maxInputBufferSize); - conn.setMaxOutputSize(maxOutputBufferSize); - conn.setDropEmptyBuffers(dropEmptyBuffers); - } SessionHandler handler = sessionHandler; if (handler != null) { handler.handleSessionInit(target); |