From 1ba88a7892fa1b0d7cf229b0cd5709575901ebd0 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Tue, 30 Jul 2024 05:39:55 -0700 Subject: perf(ext/node): improve `Buffer` from string performance (#24567) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes https://github.com/denoland/deno/issues/24323 - Use a Buffer pool for `fromString` - Implement fast call base64 writes - Direct from string `create` method for each encoding op ``` $ deno bench -A bench.mjs # 1.45.1+fee4d3a cpu: Apple M1 Pro runtime: deno 1.45.1+fee4d3a (aarch64-apple-darwin) benchmark time (avg) (min … max) p75 p99 p999 ----------------------------------------------------------- ----------------------------- Buffer.from base64 550 ns/iter (490 ns … 1'265 ns) 572 ns 606 ns 1'265 ns Buffer#write base64 285 ns/iter (259 ns … 371 ns) 307 ns 347 ns 360 ns $ ~/gh/deno/target/release/deno bench -A bench.mjs # this PR cpu: Apple M1 Pro runtime: deno dev (aarch64-apple-darwin) benchmark time (avg) (min … max) p75 p99 p999 ----------------------------------------------------------- ----------------------------- Buffer.from base64 151 ns/iter (145 ns … 770 ns) 148 ns 184 ns 648 ns Buffer#write base64 62.58 ns/iter (60.79 ns … 157 ns) 61.65 ns 75.79 ns 141 ns $ node bench.mjs # v22.4.0 cpu: Apple M1 Pro runtime: node v22.4.0 (arm64-darwin) benchmark time (avg) (min … max) p75 p99 p999 ----------------------------------------------------------- ----------------------------- Buffer.from base64 163 ns/iter (96.92 ns … 375 ns) 99.45 ns 127 ns 220 ns Buffer#write base64 75.48 ns/iter (74.97 ns … 134 ns) 75.17 ns 81.83 ns 96.84 ns ``` --- tests/unit_node/zlib_test.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/unit_node/zlib_test.ts b/tests/unit_node/zlib_test.ts index 215717dde..657433f11 100644 --- a/tests/unit_node/zlib_test.ts +++ b/tests/unit_node/zlib_test.ts @@ -123,7 +123,9 @@ Deno.test( Deno.test("should work with dataview", () => { const buf = Buffer.from("hello world"); - const compressed = brotliCompressSync(new DataView(buf.buffer)); + const compressed = brotliCompressSync( + new DataView(buf.buffer, buf.byteOffset, buf.byteLength), + ); const decompressed = brotliDecompressSync(compressed); assertEquals(decompressed.toString(), "hello world"); }); @@ -141,7 +143,9 @@ Deno.test( "zlib compression with dataview", () => { const buf = Buffer.from("hello world"); - const compressed = gzipSync(new DataView(buf.buffer)); + const compressed = gzipSync( + new DataView(buf.buffer, buf.byteOffset, buf.byteLength), + ); const decompressed = unzipSync(compressed); assertEquals(decompressed.toString(), "hello world"); }, -- cgit v1.2.3