Swapping Jolt's sorted collections to a B+tree
Jolt, a Clojure-inspired language, previously used a red-black tree to implement its sorted-map and sorted-set data structures. However, this tree had drawbacks, such as high memory usage and slower operations due to each entry requiring its own small node. The developer decided to explore if a different data structure could provide better performance.
After investigation, they found that a B+tree could be used, which led to swapping out the red-black tree for a persistent B+tree in Jolt's sorted collections. This change resulted in a 40% reduction in peak memory usage and faster operations across all sizes, as demonstrated in performance benchmarks. A B+tree keeps data in leaves with up to 16 entries and branches through inner nodes with up to 32 children, resulting in a tree that is much shallower than a red-black tree.
For example, a tree with 4 million entries in a red-black tree was only 5 levels deep in the B+tree. Additionally, B+trees have smaller metadata per entry, which reduces memory usage further. However, B+trees have a weakness in that building small ones can be slower than red-black trees. To mitigate this, the developer implemented tricks such as starting a fresh node when a node is full or dropping entries to half before merging.
These optimizations resulted in a scenario where the worst-case performance of the B+tree was only 1.05 times slower than the red-black tree, making it a viable alternative for large collections.
Rewritten by urgent.news; may contain errors — check the original.
Source: yogthos.net