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 1 commit
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
15 changes: 15 additions & 0 deletions packages/sdk/src/util/splay_tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,12 @@ export abstract class SplayNode<V> {
*/
export class SplayTree<V> {
private root?: SplayNode<V>;
private linearCount : number;
private firstNode? : SplayNode<V>;

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

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

if (target == this.root) {
this.linearCount++;
if (this.linearCount==1) {
this.firstNode = newNode;
} else if (this.linearCount > 500) {
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 {
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Use strict equality === instead of ==

Using strict equality ensures both value and type are compared, preventing potential bugs with type coercion.

Apply this diff to use strict equality:

-if (target == this.root) {
+if (target === this.root) {
📝 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 > 500) {
this.splayNode(this.firstNode);
this.linearCount = 0;
}
} else {
if (target === this.root) {
this.linearCount++;
if (this.linearCount==1) {
this.firstNode = newNode;
} else if (this.linearCount > 500) {
this.splayNode(this.firstNode);
this.linearCount = 0;
}
} else {

this.linearCount = 0;
}

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Make the splay threshold configurable

Hardcoding the value 500 limits flexibility. Defining it as a constant or making it configurable allows for easier tuning based on different use cases.

Define a constant at the top of the class:

 export class SplayTree<V> {
   private root?: SplayNode<V>;
+  private static readonly SPLAY_THRESHOLD = 500;
   private linearCount: number;
   private firstNode?: SplayNode<V>;

Update the code to use the constant:

-  } else if (this.linearCount > 500) {
+  } else if (this.linearCount > SplayTree.SPLAY_THRESHOLD) {

Committable suggestion was skipped due to low confidence.

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