Skip to content
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

Performance optimizations for @wry/trie package. #138

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node_version: ['10', '12', '14', '15']
node_version: ['10', '12', '14', '15', '16']
os: [ubuntu-latest]

steps:
Expand Down
71 changes: 71 additions & 0 deletions packages/trie/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,75 @@ describe("Trie", function () {
// Verify that all Data objects are distinct.
assert.strictEqual(new Set(datas).size, datas.length);
});

describe("performance", () => {
const objectKeys: object[][] = [];
for (let k = 0; k < 2e4; ++k) {
const key: object[] = [];
for (let i = 0; i < 100; ++i) {
key.push({});
}
objectKeys.push(key);
}

const numberKeys: number[][] = [];
for (let k = 0; k < 2e4; ++k) {
const key: number[] = [];
for (let i = 0; i < 100; ++i) {
key.push(i * k);
}
numberKeys.push(key);
}

const nodeMajorVersion = parseInt(process.versions.node.split('.')[0], 10);

(nodeMajorVersion > 10 ? it : it.skip)
("lots of fresh object references added weakly", function () {
this.timeout(20000);
const trie = new Trie<object>(true);
objectKeys.forEach(key => {
trie.lookupArray(key);
});
});

it("lots of fresh object references added strongly", function () {
this.timeout(20000);
const trie = new Trie<object>(false);
objectKeys.forEach(key => {
trie.lookupArray(key);
});
});

it("lots of numeric key arrays added weakly", function () {
this.timeout(20000);
const trie = new Trie<object>(true);
numberKeys.forEach(key => {
trie.lookupArray(key);
});
});

it("lots of numeric key arrays added strongly", function () {
this.timeout(20000);
const trie = new Trie<object>(false);
numberKeys.forEach(key => {
trie.lookupArray(key);
});
});

it("looking up the same key lots of times, weakly", function () {
this.timeout(20000);
const trie = new Trie<object>(true);
objectKeys.forEach(() => {
trie.lookupArray(objectKeys[0]);
});
});

it("looking up the same key lots of times, strongly", function () {
this.timeout(20000);
const trie = new Trie<object>(false);
objectKeys.forEach(() => {
trie.lookupArray(objectKeys[0]);
});
});
});
});
8 changes: 5 additions & 3 deletions packages/trie/src/trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
// null-prototype Object.
const defaultMakeData = () => Object.create(null);

// Useful for processing arguments objects as well as arrays.
const { forEach, slice } = Array.prototype;
const { slice } = Array.prototype;

export class Trie<Data> {
// Since a `WeakMap` cannot hold primitive values as keys, we need a
Expand All @@ -27,8 +26,11 @@ export class Trie<Data> {
}

public lookupArray<T extends IArguments | any[]>(array: T): Data {
const { length } = array;
let node: Trie<Data> = this;
forEach.call(array, key => node = node.getChildTrie(key));
for (let i = 0; i < length; ++i) {
node = node.getChildTrie(array[i]);
}
return node.data || (node.data = this.makeData(slice.call(array)));
}

Expand Down