4

I would like to store a large amount of sorted integers, they should be traversable with efficient get and insert.

If I can iterate over a storage map, could you let me know how?

Or is there a BTreeMap storage type?

2
  • As a ballpark estimate, if you would have a sorted array of all those integers, how much memory would that consume?
    – wigy
    Apr 6, 2022 at 9:53
  • I would have a look at staking's use of bags list. It's not a perfect sort: the bags represent ranges of values. Off-chain sorting can be done and then the results posted on chain. paritytech.github.io/substrate/master/pallet_bags_list/…
    – Squirrel
    Apr 6, 2022 at 9:55

1 Answer 1

6

There is no sorted storage type. That would be way too expensive to realize, just imagine adding an item how many storage reads that would need. However, for small things you can just use a storage value that uses a BTreeMap as type. Here you just need to be aware that every time you access it, you would need to decode the entire BTreeMap. In the end it boils down to what you want to achieve and what are your constraints.

1
  • And please also pay attention to the constant cost of such advanced data structure. If the size of the container is not larger than some amount (e.g. 30-50), a naive vector can be even more efficient.
    – h4x3rotab
    Apr 12, 2022 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.