Same lock script in Rust and in ckb-js-vm: 15k cycles vs 15M

I wrote the same CKB lock script twice, once in Rust and once in TypeScript for ckb-js-vm, and ran both through the same 58 scenarios in the real CKB-VM. They agree on every accept/reject decision. The cost is very different, and most of the difference is not where I expected.

The script is the session lock from ckb-session-kit. It lets a browser-held session key spend a bounded amount (per-transaction limit, optional single recipient, optional rate limit, no splitting of session cells, no type scripts the key could use to freeze funds), while the owner can always sweep. It also has a token mode that bounds xUDT amounts the same way. It delegates signature checks to the standard locks, so it does no cryptography. That makes it a test of the VM rather than of a crypto library.

Results (the lock’s own script group only):

                                       Rust        ckb-js-vm
  Code deployed                        7,656 B     30,917 B bytecode + 410,248 B VM
  Mean cycles, accepted CKB spends     14,758      15,037,968
  Mean cycles, accepted token spends   20,348      15,518,698
  Median ratio, all 58 scenarios       1x          1009x
  Cheapest run (args rejected)         3,145       14,272,805

What surprised me: the cheapest ckb-js-vm run, which rejects the args almost immediately, still costs 14.3M cycles. That is starting the JavaScript VM and loading the bytecode. An accepted spend costs 15.04M, so the lock’s own logic is about 0.77M cycles, around 5% of the total. The logic runs at roughly 66x the Rust logic’s cycles, but for a script this small the fixed start-up cost dominates.

For scale, a standard secp256k1 lock costs about 1.6M cycles. So the Rust session lock adds under 1% to a typical spend, and the ckb-js-vm version adds about 9x a signature check.

The lock gained three rules and a token mode after my first measurement on 8 Oct, so I re-ran the original 20 scenarios against both versions. The Rust lock’s accepted spends went from 12,263 to 15,393 cycles (+26%: a bigger binary and checks on every output). The ckb-js-vm version went from 14.57M to 15.07M (+3%), and most of that was start-up again. The 8 Oct TypeScript port, rebuilt today, still costs exactly its 8 Oct cycles, so the extra 0.36M comes from loading 1.8 KB more bytecode.

What I take from it:

  • For small, frequently run scripts like locks, ckb-js-vm’s start-up cost is the number to know. It does not shrink with better JavaScript, and it grows with bytecode size.
  • For larger or rarely run scripts, or for prototyping, ckb-js-vm is a real option. The TypeScript version was much faster to write, and the logic overhead (66x) is smaller than the headline ratio (about 1000x) suggests.
  • Writing it twice doubles as a test. Two independent implementations must agree on all 58 scenarios, in CI. Porting the new rules to TypeScript was a useful second read of the Rust code.

How it is measured: ckb-cycle-tools runs each scenario in ckb-debugger through ckb-testtool, one run per script group. CI rebuilds both implementations from source, the Rust lock from a pinned session-kit commit, and re-runs every scenario, so the numbers are reproducible from a clean checkout. Five of the ckb-js-vm scenarios are also in a cross-version check: every ckb-debugger release from 0.121.0 to 1.1.1, native and WebAssembly (14 builds), gave the same cycles and the same result.

The same tool works on your own contracts. Since ckb-cycle-tools 1.1.0 (npm, 31 Oct), a project made from ckb-script-templates can use its existing ckb-testtool tests as the scenarios: a helper call saves each test’s transaction, ckb-cycles scan writes the config, and ckb-cycles report gives cycles per script group and the costliest functions. Steps are in the README.

A question for people who run ckb-js-vm in production: is there a way to share VM start-up across script groups in one transaction, or is a preloaded VM snapshot planned?

4 Likes

Interesting benchmark, and the reproducible results are valuable. However, I think the main conclusion about startup overhead needs stronger experimental evidence.

The 14.3M-cycle early-rejection case does not necessarily isolate VM startup and bytecode loading. It may also include runtime initialization, argument parsing, bindings, and other execution costs. Consequently, subtracting this value from the accepted-spend cycles does not rigorously establish that application logic accounts for only 5% of the total, or that JavaScript logic is approximately 66× more expensive than Rust.

Similarly, attributing the additional 0.36M cycles entirely to 1.8 KB of extra bytecode is not justified without controlling for changes in initialization and executed instructions.

I would suggest three additional experiments: (1) measure a minimal JS contract with no application logic, (2) vary bytecode size while keeping executed logic constant, and (3) compare equivalent execution stages in Rust and JavaScript.

The 58 matching scenarios provide useful evidence of functional agreement, but they do not establish general equivalence, particularly for a security-sensitive lock.

The roughly 1000× observed difference is compelling. My main concern is that the benchmark measures total execution costs, while the discussion presents an inferred decomposition of those costs as if it had been directly measured. Separating these two would make the conclusions substantially more convincing.

2 Likes

I want to raise a few additional issues beyond the startup-cost attribution I mentioned earlier.

First, the benchmark does not establish how these results translate to actual transaction-level costs. You report cycles for the lock’s own script group, but then draw conclusions about the overhead of a typical spend. Those are different quantities. The comparison with a 1.6M-cycle secp256k1 lock is useful for scale, but it does not establish the total cost of a transaction using either session-lock implementation.

If startup overhead is really the dominant factor, why not benchmark complete transactions with 1, 2, 4, and 8 distinct script groups? That would directly test how the cost scales when multiple independent instances require verification. Without this experiment, the practical impact of the reported overhead remains unclear.

Second, I don’t think the benchmark adequately separates language-runtime overhead from implementation differences. The Rust and TypeScript versions may be functionally equivalent on the tested inputs, but that does not mean they perform equivalent operations internally. Differences in syscall frequency, data representations, memory allocation, argument decoding, and control flow could all influence the results.

Have you compared syscall counts, execution paths, or instruction-level profiles between the two implementations? Without that information, attributing the gap primarily to the JavaScript VM is premature.

Third, there is a problem with the generalization of your recommendations. You suggest that ckb-js-vm becomes a reasonable choice for larger scripts, yet no scaling experiment demonstrates that the relative overhead decreases as application complexity increases. A large script might perform significantly more interpreted operations, potentially preserving or even increasing the performance gap. A smaller proportion of fixed overhead does not automatically imply acceptable total execution cost.

You also state that startup cost cannot be reduced through better JavaScript. Where is the experiment supporting that? Reducing imports, bytecode size, initialization work, or dependencies could potentially change the measured overhead. That claim requires an optimization ablation, not just a comparison between two application versions.

Fourth, reproducibility should not be confused with experimental validity. Rebuilding both implementations in CI and obtaining identical cycles across debugger versions demonstrates repeatability. It does not show that the benchmark isolates the quantities it claims to measure. A systematically confounded benchmark can still be perfectly reproducible.

Since ckb-cycle-tools reportedly identifies the costliest functions, it would be particularly useful to publish detailed execution profiles for the early-rejection and successful-spend paths. That could provide more concrete evidence for the proposed explanation than the current subtraction-based estimates.

Finally, I would like to see the performance conclusions separated into three categories: directly measured results, estimates derived under explicit assumptions, and hypotheses requiring additional experiments.

At present, these categories are mixed together. The data convincingly show a large execution-cost difference for this particular contract, but they do not establish a general performance model for Rust versus ckb-js-vm.

A 1000× result is not automatically a 1000× explanation. The magnitude of the difference makes rigorous attribution more important, not less. Before recommending architectural choices based on these numbers, the benchmark needs controlled profiling, workload scaling, and complete transaction-level measurements.

2 Likes

thank you so much for your comments, they will help a lot!