Skip to content

Cost Profiler Overview ​

Tier 3 • Diagnose

Visual flamegraphs and execution tracing for Soroban smart contracts — pinpoint the exact functions, loops, and host operations consuming your transaction budget.

Part of the Tollcraft initiative.

Interactive Online Playground Available

Don't want to install anything locally yet? Try out the Interactive Web Playground to inspect live sample flamegraphs, zoom into stack frames, and search hot functions right in your browser.


The Cost of Soroban Execution ​

Every smart contract on Stellar runs against strict, network-enforced resource limits. Every operation has an immediate fee impact:

Resource DimensionMetering UnitFee ImpactPrimary Cost Drivers
CPU InstructionsExecuted instruction countDirect fee chargeWasm instruction execution (WasmInsnExec), crypto hashing, host dispatches
Memory AllocationsBytes allocated / copiedHard ceiling enforcementByte array cloning, memory copies (MemCpy), large vector instantiation
Storage OperationsEntry reads & writesDirect fee chargeKey-value lookups, instance data serialization, persistent ledger mutations
Ledger I/O BytesBytes read & writtenDirect fee chargeSerialized payload sizes written to or read from the ledger

When your contract transaction executes, the Stellar network calculates a non-refundable resource fee based directly on these measured inputs.

WARNING

Cost bugs don't fail standard tests — they silently inflate production transaction fees or exhaust user budgets on-chain.


The Visibility Problem ​

Testing tools like soroban-budget-assert can tell you that an invocation consumed 8,450,000 CPU instructions and violated your CI budget. However, raw numbers do not tell you why:

  • Was the budget spike caused by an unrolled loop in your contract logic?
  • Did an innocently structured helper method repeatedly cross the WASM-to-host boundary?
  • Which specific line of code triggered redundant storage serialization?

Without execution profiling, developers are forced to manually comment out code blocks or insert ad-hoc logging to guess where instructions were burned.

soroban-cost-profiler solves this by tracing WebAssembly (WASM) execution instruction-by-instruction, resolving instruction pointers back to human-readable Rust source code lines via DWARF symbols, and rendering visual flamegraphs.


How It Works ​

                     ┌───────────────────────────┐
                     │   contract.wasm (DWARF)   │
                     └─────────────┬─────────────┘
                                   │
                                   ▼
┌──────────────────┐     ┌───────────────────┐     ┌────────────────────┐
│  WASM Execution  │ ──> │ Execution Tracer  │ ──> │ Profile Aggregator │
│ (soroban-env)    │     │  (wasmi hooks)    │     │ (CallStack Tree)   │
└──────────────────┘     └───────────────────┘     └─────────┬──────────┘
                                                             │
                                                             ▼
┌──────────────────┐     ┌───────────────────┐     ┌────────────────────┐
│ Visual Output    │ <── │ Output Formatter  │ <── │   Source Mapper    │
│ (Flamegraph/SVG) │     │ (Folded Stacks)   │     │   (gimli/DWARF)    │
└──────────────────┘     └───────────────────┘     └────────────────────┘
  1. WASM Instrumentation: Hooks into the execution engine to monitor function calls, returns, and metered instruction steps.
  2. DWARF Source Resolution: Translates raw WASM Program Counters (PC) into Rust function names, source files, and line numbers.
  3. Cost Aggregation: Calculates both exclusive (self-consumed) and inclusive (total sub-tree) CPU instructions and memory consumption for every frame.
  4. Visual Flamegraph Generation: Exports standard folded-stack profiles compatible with tools like speedscope.app or renders interactive SVGs via inferno.

The Tollcraft Cost Pipeline ​

soroban-cost-profiler operates as Tier 3 in Tollcraft's three-tiered cost awareness architecture:

TierToolFocusWhen It RunsWhat It Answers
Tier 1: Preventsoroban-cost-linterStatic AnalysisCompile time / cargo check"Are there obvious anti-patterns like storage calls in loops?"
Tier 2: Detectsoroban-budget-assertEmpirical TestingTest time / cargo test"Did a code change exceed our calibrated CPU or byte budget?"
Tier 3: Diagnosesoroban-cost-profilerDeep DiagnosticsPost-failure / CI diagnostics"Where exactly in our Rust source code was the budget spent?"

Jump In ​

INFO

Ready to start profiling? Read the Overview & Quickstart and ensure you configure The Debug Precondition before building your WASM binaries.

Built for the Stellar & Soroban ecosystem.