From 2c01596b166decc5be1b05f73a2798653f9dc8b7 Mon Sep 17 00:00:00 2001 From: Paul Adenot Date: Wed, 9 Aug 2023 16:26:36 +0200 Subject: [PATCH] Use set(...) when possible to make copies faster This fixes #22. --- js/ringbuf.js | 13 +++++++++++++ public/example/index.js | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/js/ringbuf.js b/js/ringbuf.js index 67171b8..851f9cb 100644 --- a/js/ringbuf.js +++ b/js/ringbuf.js @@ -344,6 +344,19 @@ export class RingBuffer { * @private */ _copy(input, offset_input, output, offset_output, size) { + if (!size) { + return; + } + // Fast-path: use `set(...)` if possible: copying all the input linearly to + // the output. + if ( + offset_input === 0 && + offset_output + input.length <= this._storage_capacity() && + input.length === size + ) { + output.set(input, offset_output); + return; + } for (let i = 0; i < size; i++) { output[offset_output + i] = input[offset_input + i]; } diff --git a/public/example/index.js b/public/example/index.js index 292397c..c34663a 100644 --- a/public/example/index.js +++ b/public/example/index.js @@ -596,6 +596,19 @@ class RingBuffer { * @private */ _copy(input, offset_input, output, offset_output, size) { + if (!size) { + return; + } + // Fast-path: use `set(...)` if possible: copying all the input linearly to + // the output. + if ( + offset_input === 0 && + offset_output + input.length <= this._storage_capacity() && + input.length === size + ) { + output.set(input, offset_output); + return; + } for (let i = 0; i < size; i++) { output[offset_output + i] = input[offset_input + i]; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy