Closed Michal-Leszczynski closed 2 years ago
Came here with exactly the same proposal (and similar benchmarks). Maybe it is time to create a v2
release?
@Michal-Leszczynski please send a PR. Merging this would require go mod v2.
In the context of performance you can consider sending a followup PR that replaces slice with array in node
items.
Fixed by #45
With support for generics, we can replace interfaces with generic types. The use of generics avoids variables escaping to heap when calling the B-tree functions yielding 20-30% improvement.
Below are some comparisons between benchmarks (taken from original repository) done with benchstat:
In case of plain Insert Benchmark, the results are even better:
Unfortunately the functions that return nil do not work ex.
func (t *BTree) Delete(item Item) Item
. We can't simply return zero value for generic type, because it's still a valid value, so I decided to change the API, so that:we also return bool which indicates whether anything was really deleted. Of course, we can implement B-tree on pointers to generic types, but it harms performance in many cases (like having B-tree for numeric types).
Changes of API destroy backward compatibility, but because of reasons mentioned above, I believe that this approach is justified.
Here is link to forked repository with generic B-tree: https://github.com/Michal-Leszczynski/btree
How do you suggest we further proceed with this effort.