The Motoko team has an experimental branch of the compiler that implements stable regions.
For context, Motoko currently exposes low-level canister stable memory through an experimental API.
For those developers who implement or use existing stable data structures, I’m very curious to know what you think of the new API, which is a minimal generalization over the existing API.
For instance, here are the small changes needed to adapt this data structure, I expect:
- Upgrade to Regions (WIP) sardariuss/MotokoStableBTree#4
- Upgrade to Regions (WIP) sardariuss/MotokoStableBTreeTest#1
For some context, see this part of the forum thread about the structure.
What is a “region”?
In essence, a Region
is a new abstract data type in Motoko that admits the same API as before, except that distinct regions use distinct portions of stable memory, and never interfere with one another.
Critically, new Region
s can also be created dynamically, and old ones that become “garbage” can (eventually, in subsequent revisions of this system) be collected and their portion of stable memory reused for a new Region
s, in the future. The current WIP branch implements dynamic allocation, and provisions in the design will eventually permit automatic reclamation.
For now, the old API still “works”, but it uses a special, pre-allocated region, isolated from all others created dynamically. We will use this special region as a migration path (WIP now) for existing stable memory structures that were created and maintained using the current, experimental API.
What can we do now?
Let me know if you have a stable data structure that I can try to adapt, like the example above!
If you want access to the branch of the compiler and base library that permit you to do your own tests, let’s discuss that. It should be possible.
Another example
As another example of using the new Region
type, see this WIP PR in base
that gives a “stable buffer” that permits growth with arbitrary-sized blobs, and random-access reads based on numerical indicies..
Caveat: I’m not sure if this belongs in base
, but it’s a place to have the PR for now.
What’s left to do?
Before merging this feature:
- Misc cleanup of the PR
- More tests (basic tests show that the mechanism works; we need more though)
- Migration path, plus tests for it (installing the region manager and creating “region zero” for existing data).