Skip to content

Cost Profiler Overview ​

Tier 3 • Diagnose

Execution tracing and cost attribution for Soroban smart contracts — turn a failed budget into a profile that names the functions and source lines where the cost was recorded.

Part of the Tollcraft initiative.

Product tour available

The demo page walks the pipeline and shows the real command line and output, including what a degraded run prints. It is a tour of the tool, not a profiler running 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 burned through 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 running one exported function of a compiled contract under an instrumented engine, rebuilding the call tree from what the engine reports, naming the frames from the binary's own DWARF line tables, and writing the result as collapsed-stack text, JSON, or the raw event stream.

Read the limitations before you read a number

The pipeline is complete and tested — load, trace, symbolize, aggregate, format, diff — and its ceiling is at the input: wasmi 2.0 exposes no instruction-level hook and its call hook carries no program counter, so the tracer sees a list of call boundaries rather than the instructions between them, and today every frame lands at wasm[0]. The counts are a floor and a shape, not a budget reading. See Known Risks & Failure Modes.


How It Works ​

                     ┌───────────────────────────┐
                     │   contract.wasm (DWARF)   │
                     └─────────────┬─────────────┘
                                   │
                                   ▼
┌──────────────────┐     ┌───────────────────┐     ┌────────────────────┐
│  WASM Execution  │ ──> │ Execution Tracer  │ ──> │ Profile Aggregator │
│ (soroban-env)    │     │  (wasmi hooks)    │     │ (CallStack Tree)   │
└──────────────────┘     └───────────────────┘     └─────────┬──────────┘
                                                             │
                                                             ▼
┌──────────────────┐     ┌───────────────────┐     ┌────────────────────┐
│ Text Artifact    │ <── │ Output Formatter  │ <── │   Source Mapper    │
│ .folded/.json/   │     │ (folded stacks,   │     │   (gimli/DWARF)    │
│ .raw  + summary  │     │  JSON tree, raw)  │     │                    │
└──────────────────┘     └───────────────────┘     └────────────────────┘
  1. WASM tracing: Hooks into the execution engine to record call and return boundaries, host transitions, and sampled steps with their cost deltas.
  2. DWARF source resolution: Translates a code-section address into a Rust function name, source file, and line number when the binary's line tables carry it; falls back to the wasm name section, then to wasm[pc].
  3. Cost aggregation: Calculates both exclusive (self-consumed) and inclusive (total sub-tree) CPU instructions, memory bytes, and host calls for every frame.
  4. Text output: Writes the standard collapsed-stack format that speedscope.app opens directly and flamegraph.pl turns into a picture, plus a JSON call tree and the raw event stream, and prints a terminal summary of the same run. The tool writes text and no SVG.

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.