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

perf: migrate rbTree to js-sdsl #16267

Merged
merged 3 commits into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 19 additions & 15 deletions lib/rules/indent.js
Expand Up @@ -12,7 +12,7 @@
// Requirements
//------------------------------------------------------------------------------

const createTree = require("functional-red-black-tree");
const { OrderedMap } = require("js-sdsl");

const astUtils = require("./utils/ast-utils");

Expand Down Expand Up @@ -135,7 +135,8 @@ class BinarySearchTree {
* Creates an empty tree
*/
constructor() {
this._rbTree = createTree();
this._rbTree = new OrderedMap();
this._rbTreeEnd = this._rbTree.end();
aladdin-add marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -145,13 +146,7 @@ class BinarySearchTree {
* @returns {void}
*/
insert(key, value) {
const iterator = this._rbTree.find(key);

if (iterator.valid) {
this._rbTree = iterator.update(value);
} else {
this._rbTree = this._rbTree.insert(key, value);
}
this._rbTree.setElement(key, value);
}

/**
Expand All @@ -160,9 +155,9 @@ class BinarySearchTree {
* @returns {{key: number, value: *}|null} The found entry, or null if no such entry exists.
*/
findLe(key) {
const iterator = this._rbTree.le(key);
const iterator = this._rbTree.reverseLowerBound(key);

return iterator && { key: iterator.key, value: iterator.value };
return { key: iterator.pointer[0], value: iterator.pointer[1] };
ZLY201 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand All @@ -177,11 +172,20 @@ class BinarySearchTree {
if (start === end) {
return;
}
const iterator = this._rbTree.ge(start);
const iterator = this._rbTree.lowerBound(start);

while (iterator.valid && iterator.key < end) {
this._rbTree = this._rbTree.remove(iterator.key);
iterator.next();
if (iterator.equals(this._rbTreeEnd)) {
return;
}

if (end > this._rbTree.back()[0]) {
while (!iterator.equals(this._rbTreeEnd)) {
this._rbTree.eraseElementByIterator(iterator);
}
} else {
while (iterator.pointer[0] < end) {
this._rbTree.eraseElementByIterator(iterator);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -74,7 +74,6 @@
"fast-deep-equal": "^3.1.3",
"file-entry-cache": "^6.0.1",
"find-up": "^5.0.0",
"functional-red-black-tree": "^1.0.1",
"glob-parent": "^6.0.1",
"globals": "^13.15.0",
"globby": "^11.1.0",
Expand All @@ -83,6 +82,7 @@
"import-fresh": "^3.0.0",
"imurmurhash": "^0.1.4",
"is-glob": "^4.0.0",
"js-sdsl": "^4.1.4-beta.0",
ZLY201 marked this conversation as resolved.
Show resolved Hide resolved
ZLY201 marked this conversation as resolved.
Show resolved Hide resolved
"js-yaml": "^4.1.0",
"json-stable-stringify-without-jsonify": "^1.0.1",
"levn": "^0.4.1",
Expand Down