You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On the read path in SSLConnection.readDataFromNetwork we try to avoid the number of reallocations we perform by using ByteBuffer.writeWithUnsafeMutableBytes(minimumWritableBytes:). We pass SSL_MAX_RECORD_SIZE (16kB) to the minimum size, ensuring we will always be able to write a complete record.
This is reasonable in theory, but it behaves awkwardly when we combine it with the fact that NIOSSLHandler.plaintextReadBuffer is always allocated with an initial size of SSL_MAX_RECORD_SIZE. The reason this is awkward is that we call SSLConnection.readDataFromNetwork in a loop until we get .incomplete. This means that the first time we read any record, the next looped call will ask for 16kB of free space. We won't have it (we just wrote into the buffer!) and so we'll realloc.
In practice, this forces an unconditional realloc at some stage. Not great!
There are two options to resolve this. The first is to change the capacity reservation. We could try to use SSL_pending for this. However, we need to do some investigation before we do that, because SSL_pending doesn't read from the transport, and so it may not always be able to tell us how big this needs to be.
The other option, if the above doesn't work, is simply to immediately allocate the buffer at 2 * SSL_MAX_RECORD_SIZE. In practice this will avoid this unconditional resize, and in most allocators this will also be proxied directly to mmap. That means that if we never end up using the trailing 16kB, nothing bad will happen, as the page will never be faulted in.
The text was updated successfully, but these errors were encountered:
On the read path in
SSLConnection.readDataFromNetwork
we try to avoid the number of reallocations we perform by usingByteBuffer.writeWithUnsafeMutableBytes(minimumWritableBytes:)
. We passSSL_MAX_RECORD_SIZE
(16kB) to the minimum size, ensuring we will always be able to write a complete record.This is reasonable in theory, but it behaves awkwardly when we combine it with the fact that
NIOSSLHandler.plaintextReadBuffer
is always allocated with an initial size ofSSL_MAX_RECORD_SIZE
. The reason this is awkward is that we callSSLConnection.readDataFromNetwork
in a loop until we get.incomplete
. This means that the first time we read any record, the next looped call will ask for 16kB of free space. We won't have it (we just wrote into the buffer!) and so we'll realloc.In practice, this forces an unconditional realloc at some stage. Not great!
There are two options to resolve this. The first is to change the capacity reservation. We could try to use
SSL_pending
for this. However, we need to do some investigation before we do that, becauseSSL_pending
doesn't read from the transport, and so it may not always be able to tell us how big this needs to be.The other option, if the above doesn't work, is simply to immediately allocate the buffer at
2 * SSL_MAX_RECORD_SIZE
. In practice this will avoid this unconditional resize, and in most allocators this will also be proxied directly tommap
. That means that if we never end up using the trailing 16kB, nothing bad will happen, as the page will never be faulted in.The text was updated successfully, but these errors were encountered: