Skip to content

Commit dcb440f

Browse files
orgadsaduh95
authored andcommitted
src: give small stream reads their own buffer
Every read was carved out of the env-wide 64KB StreamReadSlab and handed to JS as a view over it. TLS asks for exactly the decrypted size, so the copy-on-waste path in Commit() never triggers, and any TLS chunk that JS keeps pins the whole slab and every other read in it. Chunks queued for seconds (e.g. audio streamed faster than real time) get the slabs promoted, and their external memory then drives most major GCs. Reads asking for less than a slab now get a right-sized managed buffer, as before the slab existed. Large reads (plain TCP, where libuv suggests 64KB) still use the slab. Assisted-by: Claude Code Signed-off-by: Orgad Shaneh <orgad.shaneh@audiocodes.com> PR-URL: #67327 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent bb6d44d commit dcb440f

3 files changed

Lines changed: 58 additions & 5 deletions

File tree

‎src/env.h‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,12 @@ class EnabledDebugList;
322322
class StreamReadSlab {
323323
public:
324324
// Sized to match the read buffer size that libuv suggests for stream
325-
// reads: a slab typically serves a single large read (still avoiding the
326-
// copy that right-sizing the buffer would need), or many small ones.
327-
// Larger slabs amortize allocations further, but stay alive (pinned by
328-
// chunk views) long enough to be promoted to V8's old generation, where
329-
// their external memory is only reclaimed by major GCs.
325+
// reads: a slab serves a single large read (still avoiding the copy that
326+
// right-sizing the buffer would need), or is reused for small reads that
327+
// are copied out of it. Larger slabs amortize allocations further, but
328+
// stay alive (pinned by chunk views) long enough to be promoted to V8's
329+
// old generation, where their external memory is only reclaimed by major
330+
// GCs.
330331
static constexpr size_t kSlabSize = 64 * 1024;
331332

332333
// Reserve `suggested` bytes. Starts a new slab if the current one does

‎src/stream_base.cc‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,12 @@ void StreamResource::ClearError() {
725725
uv_buf_t EmitToJSStreamListener::OnStreamAlloc(size_t suggested_size) {
726726
CHECK_NOT_NULL(stream_);
727727
Environment* env = static_cast<StreamBase*>(stream_)->stream_env();
728+
// Reads that ask for less than a slab (e.g. decrypted TLS records, which
729+
// are allocated at their exact size) get their own buffer: as views over
730+
// a shared slab, any one of them kept by JS would keep the whole slab
731+
// alive, along with every other read that was carved out of it.
732+
if (suggested_size < StreamReadSlab::kSlabSize)
733+
return env->allocate_managed_buffer(suggested_size);
728734
return env->stream_read_slab().Allocate(env->isolate(), suggested_size);
729735
}
730736

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
const common = require('../common');
3+
if (!common.hasCrypto)
4+
common.skip('missing crypto');
5+
6+
// Small TLS reads must not be views over a larger shared buffer: JS keeping
7+
// one of them would keep the whole buffer, and every other read in it, alive.
8+
9+
const assert = require('assert');
10+
const tls = require('tls');
11+
const fixtures = require('../common/fixtures');
12+
13+
const kBlock = 320;
14+
const kBlocks = 3;
15+
16+
const server = tls.createServer({
17+
key: fixtures.readKey('agent1-key.pem'),
18+
cert: fixtures.readKey('agent1-cert.pem'),
19+
}, common.mustCall((socket) => {
20+
let received = 0;
21+
socket.on('data', common.mustCallAtLeast((chunk) => {
22+
assert.strictEqual(chunk.byteOffset, 0);
23+
assert.strictEqual(chunk.buffer.byteLength, chunk.length);
24+
received += chunk.length;
25+
if (received === kBlocks * kBlock) {
26+
socket.end();
27+
server.close();
28+
} else if (received % kBlock === 0) {
29+
// Ask for the next block only once this one has arrived, so that each
30+
// block is a separate small read.
31+
socket.write('n');
32+
}
33+
}));
34+
}));
35+
36+
server.listen(0, common.mustCall(() => {
37+
const client = tls.connect({
38+
port: server.address().port,
39+
rejectUnauthorized: false,
40+
}, common.mustCall(() => {
41+
client.write(Buffer.alloc(kBlock));
42+
}));
43+
client.on('data', common.mustCall(() => {
44+
client.write(Buffer.alloc(kBlock));
45+
}, kBlocks - 1));
46+
}));

0 commit comments

Comments
 (0)