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

optimize splay tree #907

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 18 additions & 2 deletions packages/sdk/src/util/splay_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,13 @@ export abstract class SplayNode<V> {
*/
export class SplayTree<V> {
private root?: SplayNode<V>;
private static readonly SPLAY_THRESHOLD = 500;
private linearCount: number;
private firstNode?: SplayNode<V>;

constructor(root?: SplayNode<V>) {
this.root = root;
this.linearCount = 0;
}

/**
Expand Down Expand Up @@ -211,7 +215,7 @@ export class SplayTree<V> {
`out of index range: pos: ${pos} > node.length: ${node.getLength()}`,
);
}
this.splayNode(node)
this.splayNode(node);
return [node, pos];
}

Expand All @@ -226,7 +230,7 @@ export class SplayTree<V> {
return -1;
}

this.splayNode(node)
this.splayNode(node);
return this.root!.getLeftWeight();
}

Expand Down Expand Up @@ -257,6 +261,18 @@ export class SplayTree<V> {
return newNode;
}

if (target == this.root) {
this.linearCount++;
if (this.linearCount == 1) {
this.firstNode = newNode;
} else if (this.linearCount > SplayTree.SPLAY_THRESHOLD) {
this.splayNode(this.firstNode);
this.linearCount = 0;
Comment on lines +269 to +270
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ensure firstNode is defined before splaying

Although firstNode should be defined when linearCount > 500, adding a safety check can prevent unexpected errors if the state changes in the future.

Apply this diff to add a null check:

   } else if (this.linearCount > 500) {
+    if (this.firstNode) {
       this.splayNode(this.firstNode);
+    }
     this.linearCount = 0;

Committable suggestion was skipped due to low confidence.

}
} else {
this.linearCount = 0;
}

Comment on lines +264 to +275
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Implement the new balancing strategy with some improvements.

The new logic effectively implements the proposed balancing strategy for linear insertions, aligning well with the PR objectives. However, there are a few points to address:

  1. Use strict equality (===) instead of loose equality (==) for comparisons.
  2. Add a null check before splaying firstNode.
  3. Consider resetting firstNode after splaying.

Apply this diff to address the points mentioned:

-    if (target == this.root) {
+    if (target === this.root) {
       this.linearCount++;
-      if (this.linearCount == 1) {
+      if (this.linearCount === 1) {
         this.firstNode = newNode;
       } else if (this.linearCount > SplayTree.SPLAY_THRESHOLD) {
-        this.splayNode(this.firstNode);
+        if (this.firstNode) {
+          this.splayNode(this.firstNode);
+          this.firstNode = undefined;
+        }
         this.linearCount = 0;
       }
     } else {
       this.linearCount = 0;
+      this.firstNode = undefined;
     }

These changes improve type safety, prevent potential null pointer exceptions, and ensure proper reset of the firstNode reference.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (target == this.root) {
this.linearCount++;
if (this.linearCount == 1) {
this.firstNode = newNode;
} else if (this.linearCount > SplayTree.SPLAY_THRESHOLD) {
this.splayNode(this.firstNode);
this.linearCount = 0;
}
} else {
this.linearCount = 0;
}
if (target === this.root) {
this.linearCount++;
if (this.linearCount === 1) {
this.firstNode = newNode;
} else if (this.linearCount > SplayTree.SPLAY_THRESHOLD) {
if (this.firstNode) {
this.splayNode(this.firstNode);
this.firstNode = undefined;
}
this.linearCount = 0;
}
} else {
this.linearCount = 0;
this.firstNode = undefined;
}

this.splayNode(target);
this.root = newNode;
newNode.setRight(target.getRight());
Expand Down
Loading