PeaBox Lab › PeaLang › Learn › Chapter 12 › Lesson 6

Part III — Advanced Systems · Chapter 12 · Lesson 6 of 7 · 4 min read

Lesson 6: Disassembly, Profiling, and Execution Comparison

Lesson level: Advanced

Purpose: Use disassembly to read compiled code, profiling to find where time and instructions go, and side-by-side execution to confirm that the interpreter and VM agree.

Release note: commands, flags, and output formats in this lesson are illustrative. Verify them against your installed v0.63 release.

Learning objectives

By the end of this lesson, you should be able to:

6.1 Disassembly

pea disasm build/rules.peac
code object reason  (args=1, locals=1, line 2)
  line  off  instruction        argument
     3    0  LOAD_FAST          qty
     3    1  LOAD_CONST         #0  (10)
     3    2  COMPARE_GE
     3    3  POP_JUMP_IF_FALSE  6
     4    4  LOAD_CONST         #1  ("BULK")
     4    5  RETURN_VALUE
     6    6  LOAD_CONST         #2  ("NONE")
     6    7  RETURN_VALUE

6.2 Reading the listing

A listing answers questions such as "Is this constant folded?" or "Why does this loop run more instructions than expected?" without guessing.

6.3 Profiling

pea profile src/main.pea --top 10
ColumnMeaning
FunctionCode object name
CallsNumber of calls
InstructionsInstructions executed inside the function
Self timeTime excluding callees
Total timeTime including callees

6.4 Instructions versus time

Instruction counts are deterministic: the same input produces the same count on any machine. Wall-clock time varies with hardware, load, and host calls. Use instruction counts for regression gates (Chapter 10) and time for real performance tuning.

6.5 Finding hot spots

Sort by self time or instructions. A function with many calls but small cost per call suggests batching. A single call with a large cost suggests an algorithm change. Host calls such as HTTP or file reads often dominate time but not instruction counts.

6.6 Comparing interpreter and VM

pea run --engine=interp src/main.pea > interp.txt
pea run --engine=vm     src/main.pea > vm.txt
pea compare src/main.pea --engines interp,vm

The comparison should show identical output, identical errors, and identical side effects. Differences in timing are expected; differences in results are defects.

6.7 What to compare

DimensionMust match?
Printed outputYes
Returned valuesYes
Error type and messageYes
Error source locationYes
Resource-limit outcome for the same budgetYes, when budgets are instruction-based
Wall-clock timeNo

6.8 Optimizing safely

6.9 Typical improvements

PatternImprovement
Recomputing a constant expression in a loopHoist it into a const before the loop
Repeated map lookups of the same keyRead once into a local
String building in a loopCollect parts and join once
Many small host callsBatch into one call

6.10 Worked example

A profile of the order_rules batch job shows that discount_for accounts for most instructions because it reads policy["bulk_threshold"] on every call. Moving the lookup into a module-level constant cuts instructions for that function by about a third in the illustrative profile. The engine comparison and all Chapter 10 tests still pass, so the change is kept.

6.11 Common mistakes

MistakeConsequenceFix
Optimizing without a profileEffort on the wrong codeMeasure first
Using time alone as a regression gateFlaky CIGate on instruction counts
Skipping the engine comparisonHidden behaviour driftCompare after every optimization
Reading offsets as line numbersMisattributed costsUse the line column

6.12 Guided practice

6.13 Hands-on lab

Profile the order_rules batch job on 1,000 synthetic orders. Choose one hot spot, optimize it, and prove with the engine comparison and test suite that behaviour is unchanged.

Deliverable: before-and-after profiles, the disassembly of the changed function, and the comparison report.

6.14 Review questions

6.15 Applied challenge

Write a CI job that runs the engine comparison and an instruction-count gate on every pull request. Define the tolerance and explain how a developer updates the baseline after an intentional change.

Lesson summary

Transition to the Chapter 12 Checkpoint

The checkpoint combines the chapter: you will compile, inspect, profile, optimize, and compare execution of a real module.

Lesson status: Ready for review.

← Previous Next →