Research: Explanation needed for the tree data structure used in Slab
secretshardul opened this issue · 1 comments
secretshardul commented
What I understand till now is that is a binary tree is used to store bid/ask data:
- Inner nodes can have upto 2 children (hence binary). Children can either be other inner nodes or leaf nodes.
- Leaf nodes represent orders and store data such as
owner
andquantity
. - The
key
attribute is used to order nodes. It is present in both inner and leaf nodes. It begins with 0 for at the root node and grows sequentially.
Questions:
- What data structure is this exactly? I'm not sure if it's a binary search tree. The key attribute used for arrangement doesn't seem to be coming from placed orders. Also it arbitarily begins from 0.
- What does the structure optimize?
- Why custom implementation instead of a cargo crate?
dafyddd commented
I'm not the person who built it but here are some answers:
- It's a critbit tree which is a type of patricia tree. The first 8 bytes of the key attribute is the price and next 8 bytes are the seq num. Together they represent price-time priority and can be used to sort for that reason
- I think the structure is a good tradeoff between speed and space usage.
- cargo crates will usually use dynamic memory but this tree needs to live on a static size slab of bytes that's on chain