Description
Design: Storage Allocator
This issue tracks the design of an initial, simple and efficient storage allocator.
Problem
To dynamically create objects on the storage we have to have a way to allocate and deallocate them.
Since we are currently lacking an implementation of a storage allocator we cannot do these things in a sane and user friendly way.
Requirements
The requirements for a storage allocator are the following:
- Operations
alloc
anddealloc
shall perform in constant time - As few storage accesses as possible
- Allocations must not overlap
Design Basis
The Allocator
trait that should be used for that purpose should ideally be something like this:
trait Allocator {
/// Allocates a storage area.
fn alloc(size: u32) -> Key;
/// Deallocates a storage area.
fn dealloc(at: Key);
}
Design 1: Cell & Chunk Allocator
This allocator is specialized in allocating only for single cells or chunks of cells of size 2^32.
One may think that this constraint is too limiting, however, since the entire storage space allows for 2^256 different cells we would still have 2^224 different chunks if we partitioned this space in 2^32 big chunks.
So for every alloc(1)
this allocator tries to allocate for a single cell and for everything else it will default to allocating an entire 2^32 large chunk of cells.
This has some serious advantages.
By separating storage regions into single-cells and cell-chunk regions we do no longer have to memorize the exact size of an allocation.
For the underlying implementation we could use something similar to a stash
that works on the contract storage.
Other ideas are welcome.