GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — src: give small stream reads their own buffer · nodejs/node@6368e00

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • env.h
    • stream_base.cc
  • test/parallel
    • test-tls-small-read-own-buffer.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/env.h‎

Copy file name to clipboardExpand all lines: src/env.h
+6-5Lines 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
Collapse file

‎src/stream_base.cc‎

Copy file name to clipboardExpand all lines: src/stream_base.cc
+6Lines 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

Collapse file

‎test/parallel/test-tls-small-read-own-buffer.js‎

Copy file name to clipboard
+46Lines 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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.