IC stable memory
With this library you can:
- use stable variables in your code - they store their data completely in stable memory, so you don't have to do your regular routine serializing/deserializing them in
pre_updage/post_upgradehooks - use stable collections, like
SVecandSHashMapwhich work directly with stable memory and are able to hold as many data as the subnet would allow your canister to hold
Pros:
- Use all the memory, which your canister's subnet can provide (additional to 4GB of heap you already have).
- Still be able to upgrade your canister.
Cons:
- Your canister will consume more cycles, than usual, since it now does a lot of system calls in order to use stable memory.
- It is a early version software, so there may be bugs. This will improve in future. Please, report if you've encountered one.
Installation
# cargo.toml
[]
= "0.0.1"
Quick example
Check out the example project to find out more.
Let's suppose, you have a vector of strings, which you want to persist between canister upgrades. For every data chunk which is small enough (so it would be cheap to serialize/deserialize it every time you use it) , you can use stable variables to store it in stable memory:
use stable;
use ;
use ;
type MyStrings = ;
This would work fine for any kind of small data, like settings. But when you need to store bigger data, it may be really inefficient to serialize/deserialize gigabytes of data just to read a couple of kilobytes from it. For example, if you're storing some kind of an event log (which can grow into a really big thing), you only want to access some limited number of entries at a time. In this case, you want to use a stable collection.
use SVec;
use stable;
use ;
use ;
// Note, that Vec transformed into SVec
type MyStrings = ;
type MyStringsSlice = ;
There is also a SHashMap collection, if you need keyed values.