Performance compared with mocalpha
What the same Motoko program costs in instructions when moxzi builds it and when moc builds it, and how to measure it yourself.
moxzi and moc compile the same Motoko, but they generate different WebAssembly, so the same program costs a different number of instructions. On the Internet Computer you pay for every instruction, and each message has an instruction limit. This page compares the two compilers on common code and shows how to repeat the comparison.
Common operations#
The program below runs each operation over inputs of two sizes and reports the difference per step, so the cost of the call itself cancels out:
// The instructions one step of `run` costs. Run it over inputs of size `n` and `2n` and
// divide the difference by `n`: the cost of the call itself cancels out. The inputs are
// built before counting starts.
func perStep<T>(n : Nat, make : Nat -> T, run : T -> ()) : Nat {
let small = make(n);
let large = make(2 * n);
let once = InternetComputer.countInstructions(func() { run(small) });
let twice = InternetComputer.countInstructions(func() { run(large) });
(twice - once).toNat() / n;
};
public query func costs() : async {
whileLoop : Nat; rangeLoop : Nat; blobBytes : Nat; crc32 : Nat; sha256 : Nat; textChars : Nat;
} {
{
// per iteration of an empty loop
whileLoop = perStep(10_000, count, func(n : Nat) { var i = 0; while (i < n) { i += 1 } });
rangeLoop = perStep(10_000, count, func(n : Nat) { for (_ in Nat.range(0, n)) {} });
// per byte
blobBytes = perStep(10_000, bytes, func(b : Blob) {
var x : Nat8 = 0;
for (y in b.values()) { x ^= y };
});
crc32 = perStep(10_000, bytes, func(b : Blob) {
var c : Nat32 = 0xFFFFFFFF;
for (y in b.values()) { c := crcTable[((c ^ y.toNat32()) & 0xFF).toNat()] ^ (c >> 8) };
});
sha256 = perStep(10_000, bytes, func(b : Blob) { ignore Sha256.fromBlob(#sha256, b) });
// per character
textChars = perStep(10_000, letters, func(t : Text) {
var k = 0;
for (_ in t.chars()) { k += 1 };
});
};
};
Build it with both compilers and run both builds on the same server, counting real instructions:
moxzi build bench.mo -o moxzi.wasm
moc $(mops sources) -c bench.mo -o moc.wasm
moc --version
bench.mo -> moxzi.wasm + moxzi.did (405579 bytes, 21 file(s), 237 steps, 4.02s)
compiled by ~/.local/lib/moxzi/compiler.wasm (sha256 c10185084eaf8990a562288f29cd926e583163b0412fa27dbbecb8472a8e6e1e), moxzi 0.1.0-alpha.12
Motoko compiler 2.2.3 (source fn6flvi4-d1nsmy7q-z5dc0avk-yar1dv29)
MOXZI_FUEL=1 moxzid -m actors.json
curl -s -w '\n' -d '[]' http://127.0.0.1:7000/query-json/moxzi/costs
curl -s -w '\n' -d '[]' http://127.0.0.1:7000/query-json/moc/costs
[{"blobBytes":22,"crc32":65,"rangeLoop":27,"sha256":210,"textChars":95,"whileLoop":27}]
[{"blobBytes":117,"crc32":205,"rangeLoop":101,"sha256":296,"textChars":535,"whileLoop":54}]
The first line is moxzi's build, the second moc's. Per step:
| Operation | moxzi | moc | moc costs |
|---|---|---|---|
while loop with a Nat counter, per iteration | 27 | 54 | 2.0x |
for (i in Nat.range(a, b)), per iteration | 27 | 101 | 3.7x |
for (b in blob.values()), per byte | 22 | 117 | 5.3x |
CRC-32 over a Blob, per byte | 65 | 205 | 3.2x |
SHA-256 from the sha2 package, per byte | 210 | 296 | 1.4x |
for (c in text.chars()), per character | 95 | 535 | 5.6x |
moxzid counts instructions with its own engine. The Internet Computer weighs some instructions differently, so its counts are higher for both compilers: on a local replica CRC-32 costs 74 instructions per byte with moxzi and 268 with moc. The comparison between the compilers holds.
Where the difference comes from#
- Loops over ranges, arrays and blobs compile to a counted loop. There is no iterator object and no closure call per step.
for (c in text.chars())walks the text's bytes directly instead of calling the runtime's iterator for every character.NatandIntarithmetic runs inline for every value below 2^61, negativeInts included, without a call into the runtime.mocdoes that only for values below 2^30.- Shifts and rotations by a constant are folded at compile time, and module-level constants are read once per loop.
Code whose time goes to big-number arithmetic, such as elliptic-curve cryptography and pairings, costs about the same with both compilers. Use the native kernels for those.
Writing fast Motoko covers what to choose in your own code.
Candid and canister size#
Measured on PocketIC, in IC instructions:
| Operation | moxzi | moc |
|---|---|---|
Decode a [Nat8], per element | 27 | 199 |
Decode a [{ id : Nat; name : Text; tags : [Text] }], per element | 1,310 | 4,355 |
Decode a message (Nat, Text, record) | 3,120 | 7,498 |
to_candid of a 5-field record | 831 | 2,325 |
to_candid of a 64 KiB Blob | 66,611 | 132,829 |
Decoding is fastest when the message's type table is exactly the one moxzi expects, as for a call from another Motoko canister with the same interface. Any other message, such as one from a JavaScript agent, takes the full decoder, which accepts and rejects exactly what moc's does.
The linker drops every function nothing can call, so small canisters are smaller than moc's and cheaper to install. A one-counter actor:
| moxzi | moc | |
|---|---|---|
| Module size | 196,780 bytes | 250,160 bytes |
| Install, in cycles | 1.71 billion | 2.22 billion |
| Each later install or upgrade, in cycles | 0.45 billion | 0.56 billion |
Message cost on moxzid#
Before every update and query, moxzid saves enough of the actor's state to undo a trap. For an actor with a large memory it saves only the memory pages the message writes, so a message that changes a counter stays fast however large the actor is:
| Actor | A one-counter update | A query |
|---|---|---|
| 1.2 GB live, macOS | about 25 ms | about 12 ms |
| 500 MB live, Linux | about 50 ms | about 5 ms |
A message that rewrites most of a large memory still pays for every page it writes.
Limitations#
| Limitation | What to do |
|---|---|
The table is for the versions of moxzi, moc and core shown in the build output | Run the example with your versions; the numbers change with every compiler release |
moxzid's counts differ from the Internet Computer's | Compare compilers on the same engine, as the example does. For exact on-chain costs, measure on a replica |