# StableBTreeMap in Canisters

**URL:** <https://forum.dfinity.org/t/stablebtreemap-in-canisters/14210>\
**Category:** Rust\
**Created:** [July 7, 2022, 9:30pm UTC](https://forum.dfinity.org/t/stablebtreemap-in-canisters/14210 "2022-07-07T21:30:27Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![ielashi](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ielashi/32/15704_2.png) [@ielashi](https://forum.dfinity.org/u/ielashi)\
**Post date:** [July 7, 2022, 9:30pm UTC](https://forum.dfinity.org/t/stablebtreemap-in-canisters/14210/1 "2022-07-07T21:30:27Z")

</div>

Hey everyone,

While working on the Bitcoin integration we developed `StableBTreeMap`. It is a KV store that has a similar interface to a standard `BTreeMap` in Rust, but it manages its own memory. This data structure allowed us to store large amounts of state in the subnet while minimizing our memory footprint as well minimizing the time it takes to persist this map to disk.

Several of you have been asking if this same data structure can be useful for canister developers. In theory, the answer is yes. This data structure can allow canisters to store state that can grow to several gigabytes in size, without having to define a `pre_upgrade` /`post_upgrade` hooks in the canister.

@senior.joinu has been doing some great work with the `ic-stable-memory` library, and @ovictor already worked on porting `StableBTreeMap` for use in canisters. Given the interest, I made some updates to the `StableBTreeMap` API to make it more canister friendly, and I published some examples on how to use it [here](https://github.com/ielashi/stable-btreemap-example).

This data structure has been tested with tens of millions of keys, but as I mentioned in an [earlier post](https://forum.dfinity.org/t/direct-integration-with-bitcoin/6147/349), we didn’t test this data structure within a canister, so please experiment with it cautiously.

Hope this helps!

---

_[View the full topic](https://forum.dfinity.org/t/stablebtreemap-in-canisters/14210)._
