-
Notifications
You must be signed in to change notification settings - Fork 2
Fix sketch edge cases and speed up tracked-key updates; restore tests #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,17 @@ | ||
| name: CI | ||
| on: [push, pull_request] | ||
| permissions: | ||
| contents: read | ||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| matrix: | ||
| node: [22, 24, 26] | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
| - uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: ${{ matrix.node }} | ||
| - run: npm test | ||
| - run: npm pack |
This file contains hidden or 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,14 @@ | ||
| # Changelog | ||
|
|
||
| ## Unreleased | ||
|
|
||
| - Count keys such as `__proto__`, `constructor` and `toString` without interacting with Object.prototype. | ||
| - Return independent top-k tuples so callers cannot modify internal sketch state. | ||
| - Match serialized-size estimates to the actual power-of-two width. | ||
| - Repair only the affected heap path on tracked-key increments instead of sorting the entire heap. A local Node 24/macOS arm64 benchmark of 100,000 repeat updates across 1,000 tracked keys measured a seven-run median of 29 ms versus 1,141 ms before this change. This is workload-specific; see `bench/repeated-updates.js`. | ||
| - Restore the original test scenarios on Node's built-in runner, add regression coverage, and add current-Node CI and explicit package contents. | ||
| - Replace deprecated Buffer construction with zero-initialized buffers. | ||
|
|
||
| ### Release compatibility | ||
|
|
||
| Runtime minimum becomes Node 6 because Buffer.alloc is now used; test development requires modern Node (CI: 22/24/26). Dropping previously advertised Node 0.6 support requires a major release. No version has been bumped yet. Top-k tie ordering is unspecified and may change. The existing binary format is retained; it does not record the original top-k capacity when a sketch is serialized before filling, so that capacity cannot be fully recovered. Malformed-input/deserialization validation remains a follow-up before a release is considered complete. |
This file contains hidden or 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,13 @@ | ||
| const path = require('node:path'); | ||
| const { performance } = require('node:perf_hooks'); | ||
| const CountMinSketch = require(path.resolve(process.argv[2] || path.join(__dirname, '../lib/countMinSketch.js'))); | ||
| const times = []; | ||
| for (let trial = 0; trial < 7; trial++) { | ||
| const sketch = new CountMinSketch(1000, 0.0005, 0.0001); | ||
| const keys = Array.from({length: 1000}, (_, i) => 'item-' + i); | ||
| keys.forEach(key => sketch.increment(key)); | ||
| const start = performance.now(); | ||
| for (let i = 0; i < 100000; i++) sketch.increment(keys[(i * 337) % 1000]); | ||
| times.push(performance.now() - start); | ||
| } | ||
| console.log(JSON.stringify({times, median: times.sort((a,b) => a-b)[3]})); |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,42 @@ | ||
| const { test } = require('node:test'); | ||
| const assert = require('node:assert/strict'); | ||
| const { CountMinSketch, getViewsObjSize } = require('..'); | ||
|
|
||
| test('counts keys which match Object prototype properties', () => { | ||
| const sketch = new CountMinSketch(10, 0.001, 0.001); | ||
| sketch.increment('toString'); | ||
| assert.deepEqual(sketch.getTopK(), [[1, 'toString']]); | ||
| }); | ||
| test('top-k results cannot mutate internal count or key tuples', () => { | ||
| const sketch = new CountMinSketch(10, 0.001, 0.001); | ||
| sketch.increment('a'); | ||
| const result = sketch.getTopK(); | ||
| result[0][0] = 100; | ||
| result[0][1] = 'changed'; | ||
| assert.deepEqual(sketch.getTopK(), [[1, 'a']]); | ||
| }); | ||
| test('size estimate uses the actual power-of-two sketch width', () => { | ||
| const sketch = new CountMinSketch(10, 0.002, 0.0001); | ||
| assert.equal(getViewsObjSize(0.002, 0.0001), sketch.serialize().length); | ||
| }); | ||
|
|
||
| test('reserved keys survive serialization and later increments', () => { | ||
| const sketch = new CountMinSketch(10, 0.001, 0.001); | ||
| const keys = ['__proto__', 'constructor', 'toString']; | ||
| const previous = Object.getOwnPropertyDescriptor(Object.prototype, '0'); | ||
| for (const key of keys) sketch.increment(key); | ||
| const restored = CountMinSketch.deserialize(sketch.serialize()); | ||
| for (const key of keys) restored.increment(key); | ||
| assert.deepEqual(restored.getTopK().sort((a,b) => a[1].localeCompare(b[1])), keys.map(key => [2,key]).sort((a,b) => a[1].localeCompare(b[1]))); | ||
| assert.deepEqual(Object.getOwnPropertyDescriptor(Object.prototype, '0'), previous); | ||
| }); | ||
| test('heap repairs retain every tracked key and its count', () => { | ||
| const sketch = new CountMinSketch(1000, 0.0005, 0.0001); | ||
| const keys = Array.from({ length: 1000 }, (_, i) => 'item-' + i); | ||
| keys.forEach(key => sketch.increment(key)); | ||
| for (let i = 0; i < 100000; i++) sketch.increment(keys[(i * 337) % 1000]); | ||
| const result = sketch.getTopK(); | ||
| assert.equal(result.length, 1000); | ||
| assert.equal(new Set(result.map(entry => entry[1])).size, 1000); | ||
| assert.ok(result.every(entry => entry[0] === 101)); | ||
| }); |
This file contains hidden or 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,11 @@ | ||
| // Preserve the legacy suite's ordered, shared-topic assertions using node:test. | ||
| module.exports = function runBatch(name, groups) { | ||
| const { test } = require('node:test'); | ||
| test(name, async (t) => { | ||
| for (const [groupName, group] of Object.entries(groups)) { | ||
| for (const [assertion, run] of Object.entries(group)) { | ||
| if (assertion !== 'topic') await t.test(groupName + ': ' + assertion, () => run(group.topic)); | ||
| } | ||
| } | ||
| }); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.