moxzi
Docs / Compatibility / Performance compared with moc

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:

bench.mo
// 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;
};
bench.mo
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:

Operationmoxzimocmoc costs
while loop with a Nat counter, per iteration27542.0x
for (i in Nat.range(a, b)), per iteration271013.7x
for (b in blob.values()), per byte221175.3x
CRC-32 over a Blob, per byte652053.2x
SHA-256 from the sha2 package, per byte2102961.4x
for (c in text.chars()), per character955355.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#

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:

Operationmoxzimoc
Decode a [Nat8], per element27199
Decode a [{ id : Nat; name : Text; tags : [Text] }], per element1,3104,355
Decode a message (Nat, Text, record)3,1207,498
to_candid of a 5-field record8312,325
to_candid of a 64 KiB Blob66,611132,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:

moxzimoc
Module size196,780 bytes250,160 bytes
Install, in cycles1.71 billion2.22 billion
Each later install or upgrade, in cycles0.45 billion0.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:

ActorA one-counter updateA query
1.2 GB live, macOSabout 25 msabout 12 ms
500 MB live, Linuxabout 50 msabout 5 ms

A message that rewrites most of a large memory still pays for every page it writes.

Limitations#

LimitationWhat to do
The table is for the versions of moxzi, moc and core shown in the build outputRun the example with your versions; the numbers change with every compiler release
moxzid's counts differ from the Internet Computer'sCompare compilers on the same engine, as the example does. For exact on-chain costs, measure on a replica
On this pageCommon operationsWhere the difference comes fromCandid and canister sizeMessage cost on moxzidLimitations