From feb61459fdecd05684d127e8e082cc710f47fb60 Mon Sep 17 00:00:00 2001 From: Christian Bates-White <45979878+Whitecx@users.noreply.github.com> Date: Fri, 14 Jun 2024 08:27:01 -0400 Subject: [PATCH] doc: add Buffer.from(string) to functions that use buffer pool MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Buffer.from(string) is one of the functions that may use the pre-allocated buffer. It's mentioned in the description of Buffer.from(array), but not in Buffer.from(string), or in the two other places where functions that behave this way are listed, so this commit adds those references. PR-URL: https://github.com/nodejs/node/pull/52801 Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: James M Snell --- doc/api/buffer.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 1749aba25ab5e9..be7998b5b7d066 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -808,7 +808,7 @@ A `TypeError` will be thrown if `size` is not a number. The `Buffer` module pre-allocates an internal `Buffer` instance of size [`Buffer.poolSize`][] that is used as a pool for the fast allocation of new `Buffer` instances created using [`Buffer.allocUnsafe()`][], [`Buffer.from(array)`][], -and [`Buffer.concat()`][] only when `size` is less than +[`Buffer.from(string)`][], and [`Buffer.concat()`][] only when `size` is less than `Buffer.poolSize >>> 1` (floor of [`Buffer.poolSize`][] divided by two). Use of this pre-allocated internal memory pool is a key difference between @@ -846,11 +846,11 @@ _may contain sensitive data_. Use [`buf.fill(0)`][`buf.fill()`] to initialize such `Buffer` instances with zeroes. When using [`Buffer.allocUnsafe()`][] to allocate new `Buffer` instances, -allocations under 4 KiB are sliced from a single pre-allocated `Buffer`. This -allows applications to avoid the garbage collection overhead of creating many -individually allocated `Buffer` instances. This approach improves both -performance and memory usage by eliminating the need to track and clean up as -many individual `ArrayBuffer` objects. +allocations less than `Buffer.poolSize >>> 1` (4KiB when default poolSize is used) are sliced +from a single pre-allocated `Buffer`. This allows applications to avoid the +garbage collection overhead of creating many individually allocated `Buffer` +instances. This approach improves both performance and memory usage by +eliminating the need to track and clean up as many individual `ArrayBuffer` objects. However, in the case where a developer may need to retain a small chunk of memory from a pool for an indeterminate amount of time, it may be appropriate @@ -1388,6 +1388,9 @@ console.log(buf1.toString('latin1')); A `TypeError` will be thrown if `string` is not a string or another type appropriate for `Buffer.from()` variants. +[`Buffer.from(string)`][] may also use the internal `Buffer` pool like +[`Buffer.allocUnsafe()`][] does. + ### Static method: `Buffer.isBuffer(obj)`