-
-
Notifications
You must be signed in to change notification settings - Fork 148
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
Balancing Tree to Prevent Skewness #1025
base: main
Are you sure you want to change the base?
Conversation
To prevent the tree from becoming skewed, I balanced it by splaying the first node of the sequence every 500 linear insert operations. The value 500 was determined experimentally.
WalkthroughThe changes in this pull request involve modifications to the Changes
Assessment against linked issues
Possibly related issues
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1025 +/- ##
==========================================
- Coverage 48.76% 48.75% -0.01%
==========================================
Files 79 79
Lines 11343 11355 +12
==========================================
+ Hits 5531 5536 +5
- Misses 5269 5275 +6
- Partials 543 544 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (3)
pkg/splay/splay.go (3)
96-97
: Add documentation for new fields inTree
structTo improve code readability and maintainability, please add comments explaining the purpose of the new fields
linearCount
andfirstNode
in theTree
struct. This will help other developers understand how these fields are used to prevent skewness in the splay tree.
126-126
: ResetfirstNode
after splayingAfter splaying
firstNode
and resettinglinearCount
, it's advisable to also resetfirstNode
tonil
. This prevents holding a reference to a node that may have been altered or deleted later, which can help avoid potential memory leaks or unintended behavior.Apply this diff:
t.Splay(t.firstNode) t.linearCount = 0 + t.firstNode = nil
124-126
: Define splay threshold as a named constantUsing the magic number
500
as the splay threshold can reduce code clarity. Consider defining it as a named constant or making it configurable. This enhances readability and makes future adjustments easier.Apply this diff:
+const splayThreshold = 500 ... } else if t.linearCount > splayThreshold { t.Splay(t.firstNode) t.linearCount = 0 }
if t.linearCount == 1 { | ||
t.firstNode = node | ||
} else if t.linearCount > 500 { | ||
t.Splay(t.firstNode) | ||
t.linearCount = 0 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure firstNode
is valid before splaying
There's a potential risk that firstNode
might have been modified or removed from the tree before the splay operation when linearCount
exceeds the threshold. This could lead to unexpected behavior or runtime errors. Consider adding a check to ensure that firstNode
is still a valid node in the tree before performing the splay operation.
Apply this diff to include a validity check:
} else if t.linearCount > 500 {
+ if t.isValidNode(t.firstNode) {
t.Splay(t.firstNode)
+ }
t.linearCount = 0
}
And implement the isValidNode
method:
func (t *Tree[V]) isValidNode(node *Node[V]) bool {
return node != nil && node.hasLinks()
}
To prevent the tree from becoming skewed, I balanced it by splaying the first node of the sequence every 500 linear insert operations. The value 500 was determined experimentally.
What this PR does / why we need it:
Description:
This pull request (PR) introduces an enhanced method for balancing a tree during insert operations to prevent skewness. The goal is to optimize performance by replacing the inefficient height-based splay operations with a more effective approach.
Changes Made:
Linear Operation Detection and Balancing:
Optimization of Splay Trigger Frequency:
Code Modifications:
linearCount
andfirstNode
fields to theTree
structure to track consecutive insert operations.InsertAfter
method to conditionally perform a splay operation and resetlinearCount
.Performance Improvements:
For more details, you can check this
Which issue(s) this PR fixes:
Fixes #941
Special notes for your reviewer:
Does this PR introduce a user-facing change?:
Additional documentation:
Checklist:
Summary by CodeRabbit