-
Notifications
You must be signed in to change notification settings - Fork 141
Performance: Optimize sorted_set union/symmetric_difference and make bit extraction branchless #2978
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
base: main
Are you sure you want to change the base?
Performance: Optimize sorted_set union/symmetric_difference and make bit extraction branchless #2978
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -141,36 +141,41 @@ pub fn[V : Compare] SortedSet::union( | |||||||||||
| self : SortedSet[V], | ||||||||||||
| src : SortedSet[V], | ||||||||||||
| ) -> SortedSet[V] { | ||||||||||||
| fn aux(a : Node[V]?, b : Node[V]?) -> Node[V]? { | ||||||||||||
| fn aux(a : Node[V]?, b : Node[V]?) -> (Node[V]?, Int) { | ||||||||||||
| match (a, b) { | ||||||||||||
| (Some(_), None) => a | ||||||||||||
| (None, Some(_)) => b | ||||||||||||
| (Some(_), None) => (a, count_nodes(a)) | ||||||||||||
| (None, Some(_)) => (b, count_nodes(b)) | ||||||||||||
| (Some({ value: va, left: la, right: ra, .. }), Some(_)) => { | ||||||||||||
| let (l, r) = split(b, va) | ||||||||||||
| Some(join(aux(la, l), va, aux(ra, r))) | ||||||||||||
| let (left_tree, left_size) = aux(la, l) | ||||||||||||
| let (right_tree, right_size) = aux(ra, r) | ||||||||||||
| (Some(join(left_tree, va, right_tree)), left_size + 1 + right_size) | ||||||||||||
| } | ||||||||||||
| (None, None) => None | ||||||||||||
| (None, None) => (None, 0) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| match (self.root, src.root) { | ||||||||||||
| (Some(_), Some(_)) => { | ||||||||||||
| let t1 = copy_tree(self.root) | ||||||||||||
| let t2 = copy_tree(src.root) | ||||||||||||
| let t = aux(t1, t2) | ||||||||||||
| let mut ct = 0 | ||||||||||||
| let ret = { root: t, size: 0 } | ||||||||||||
| // TODO: optimize this. Avoid counting the size of the set. | ||||||||||||
| ret.each(_x => ct = ct + 1) | ||||||||||||
| ret.size = ct | ||||||||||||
| ret | ||||||||||||
| let (t, size) = aux(t1, t2) | ||||||||||||
| { root: t, size } | ||||||||||||
| } | ||||||||||||
| (Some(_), None) => { root: copy_tree(self.root), size: self.size } | ||||||||||||
| (None, Some(_)) => { root: copy_tree(src.root), size: src.size } | ||||||||||||
| (None, None) => new() | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| ///| | ||||||||||||
| fn[V] count_nodes(node : Node[V]?) -> Int { | ||||||||||||
| match node { | ||||||||||||
| None => 0 | ||||||||||||
| Some({ left, right, .. }) => 1 + count_nodes(left) + count_nodes(right) | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| ///| | ||||||||||||
| fn[V : Compare] split(root : Node[V]?, value : V) -> (Node[V]?, Node[V]?) { | ||||||||||||
| match root { | ||||||||||||
|
|
@@ -289,10 +294,10 @@ pub fn[V : Compare] SortedSet::symmetric_difference( | |||||||||||
| self : SortedSet[V], | ||||||||||||
| other : SortedSet[V], | ||||||||||||
| ) -> SortedSet[V] { | ||||||||||||
| // TODO: Optimize this function to avoid creating two intermediate sets. | ||||||||||||
| let set1 = self.difference(other) | ||||||||||||
| let set2 = other.difference(self) | ||||||||||||
| set1.union(set2) | ||||||||||||
| let ret = new() | ||||||||||||
| self.each(x => if !other.contains(x) { ret.add(x) }) | ||||||||||||
| other.each(x => if !self.contains(x) { ret.add(x) }) | ||||||||||||
| ret | ||||||||||||
|
Comment on lines
+297
to
+300
|
||||||||||||
| let ret = new() | |
| self.each(x => if !other.contains(x) { ret.add(x) }) | |
| other.each(x => if !self.contains(x) { ret.add(x) }) | |
| ret | |
| self.difference(other).union(other.difference(self)) |
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.
The count_nodes() calls defeat the optimization's purpose. When one subtree is exhausted during union, count_nodes() still traverses the entire remaining tree in O(n) time, which is what the original TODO aimed to eliminate. Consider storing size information in each node, or accumulating sizes from split() operations to avoid this traversal.