forked from hypergeometric/notepack
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: add a cache for buffer-to-string conversions
The tiny and small benchmarks got quite a boost: ``` +-----------+-------------------+-----------------+----------------+-------------+ | │ tiny │ small │ medium │ large | +-----------+-------------------+-----------------+----------------+-------------+ | before │ 1,733,059 ops/sec │ 368,476 ops/sec │ 30,497 ops/sec │ 257 ops/sec | +-----------+-------------------+-----------------+----------------+-------------+ | after │ 4,278,698 ops/sec │ 701,657 ops/sec │ 34,667 ops/sec │ 249 ops/sec | +-----------+-------------------+-----------------+----------------+-------------+ ```
- Loading branch information
1 parent
40df3f6
commit d3720ed
Showing
2 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const DEFAULT_MAX_SIZE = process.env.NOTEPACK_DECODE_CACHE_MAX_SIZE || 1024; | ||
const DEFAULT_MAX_LENGTH = process.env.NOTEPACK_DECODE_CACHE_MAX_LENGTH || 16; | ||
|
||
/** | ||
* Store the buffer-to-string values in a tree | ||
*/ | ||
class BufferToStringCache { | ||
constructor({ maxSize = DEFAULT_MAX_SIZE, maxLength = DEFAULT_MAX_LENGTH } = {}) { | ||
this.size = 0; | ||
this.maxSize = maxSize; | ||
this.maxLength = maxLength; | ||
this.cache = new Map(); | ||
for (let i = 1; i <= this.maxLength; i++) { | ||
this.cache.set(i, new Map()); | ||
} | ||
} | ||
|
||
get(buffer, offset, length) { | ||
if (length > this.maxLength) { return false; } | ||
let node = this.cache.get(length); | ||
for (let i = 0; i < length; i++) { | ||
const byte = buffer[offset + i]; | ||
if (node.has(byte)) { | ||
node = node.get(byte); | ||
} else { | ||
return false; | ||
} | ||
} | ||
return node; | ||
} | ||
|
||
set(buffer, offset, length, value) { | ||
if (length > this.maxLength || this.size >= this.maxSize) { return; } | ||
this.size++; | ||
let node = this.cache.get(length); | ||
for (let i = 0; i < length; i++) { | ||
const byte = buffer[offset + i]; | ||
if (i === length - 1) { | ||
node.set(byte, value); | ||
} else if (node.has(byte)) { | ||
node = node.get(byte); | ||
} else { | ||
const newNode = new Map(); | ||
node.set(byte, newNode); | ||
node = newNode; | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = BufferToStringCache; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters