Hey there,
working with the new Region API from Motoko I’m observing a strange 8mb stable storage overhead per Region.new()
call. This seems surprising to me and I wanted to check if this is known ok, but undocumented behaviour or potentially a bug.
To reproduce the issue i’ve created this region.bench.mo
mops benchmark file. It’s very straight forward and just tries to allocate regions and write 100 bytes into the first page of each region:
import Bench "mo:bench";
import Nat "mo:base/Nat";
import Nat8 "mo:base/Nat8";
import Nat32 "mo:base/Nat32";
import Iter "mo:base/Iter";
import Array "mo:base/Array";
import Blob "mo:base/Blob";
import Region "mo:base/Region";
module {
public func init() : Bench.Bench {
let bench = Bench.Bench();
bench.name("Test the regions");
bench.rows(["Regions"]);
bench.cols(["1", "10", "100", "1000", "10000"]);
bench.runner(func(row, col) {
let ?n = Nat.fromText(col);
for (i in Iter.range(0, n - 1)) {
var reg = Region.new();
let _ = Region.grow(reg, 1);
assert Region.size(reg) == 1;
Region.storeBlob(reg, 0, make_blob(100, i));
};
});
bench;
};
private func make_blob(size : Nat, n : Nat) : Blob {
let a = Array.tabulate<Nat8>(size, func i = Nat8.fromIntWrap(Nat.bitshiftRight(n, 8 * Nat32.fromIntWrap(i))));
return Blob.fromArray(a);
};
};
Now running this surprisingly reports straight out 8mb for every single created region. E.g. 8mb for 1, 80mb for 10, 800 for 100. And then the script crashes.
$ mops bench region
Benchmark files:
• bench/region.bench.mo
==================================================
Starting dfx replica...
Deploying canisters...
——————————————————————————————————————————————————
Running bench/region.bench.mo...
| :------ | ----: | ----: | ----: | ---: | ----: |
| Regions | 244 B | 244 B | 244 B | | |
Stable Memory
| | 1 | 10 | 100 | 1000 | 10000 |
| :------ | ----: | -----: | ------: | ---: | ----: |
| Regions | 8 MiB | 80 MiB | 800 MiB | | |
Garbage Collection
| | 1 | 10 | 100 | 1000 | 10000 |
| :------ | -----: | -------: | --------: | ---: | ----: |
| Regions | 1004 B | 6.01 KiB | 56.28 KiB | | |
Unexpected error. Stopping replica...
Is this known behavior of the Region API or am I doing something wrong?
Also note that this run here crashes when trying to create 10k regions for 8gb of stable memory - this is also not clear to me (I assume it should be possible to go up to 400gb of stable storage) but I might imagine this is a local dfx instance configuration. Still any pointers would be appreciated.
Thanks!