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:
- Read a disassembly listing and map it to source.
- Collect and interpret a profile of a running program.
- Distinguish wall-clock time from instruction counts.
- Compare interpreter and VM results and resource use.
- Choose an optimization and verify it did not change behaviour.
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
- The line column comes from the line table and maps back to source.
- The offset column is the jump target space.
- Arguments show constant values in parentheses for readability.
- Nested code objects appear as separate sections.
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
| Column | Meaning |
|---|---|
| Function | Code object name |
| Calls | Number of calls |
| Instructions | Instructions executed inside the function |
| Self time | Time excluding callees |
| Total time | Time 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
| Dimension | Must match? |
|---|---|
| Printed output | Yes |
| Returned values | Yes |
| Error type and message | Yes |
| Error source location | Yes |
| Resource-limit outcome for the same budget | Yes, when budgets are instruction-based |
| Wall-clock time | No |
6.8 Optimizing safely
- Profile and pick one hot spot.
- Write or confirm tests that pin its behaviour.
- Make one change.
- Re-run tests and the engine comparison.
- Re-profile and record the change in instructions and time.
- Keep the change only if behaviour is identical and the gain is real.
6.9 Typical improvements
| Pattern | Improvement |
|---|---|
| Recomputing a constant expression in a loop | Hoist it into a const before the loop |
| Repeated map lookups of the same key | Read once into a local |
| String building in a loop | Collect parts and join once |
| Many small host calls | Batch 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
| Mistake | Consequence | Fix |
|---|---|---|
| Optimizing without a profile | Effort on the wrong code | Measure first |
| Using time alone as a regression gate | Flaky CI | Gate on instruction counts |
| Skipping the engine comparison | Hidden behaviour drift | Compare after every optimization |
| Reading offsets as line numbers | Misattributed costs | Use the line column |
6.12 Guided practice
- Disassemble a function and label each instruction with its source line.
- Profile a small program and name its top function by self time.
- Run the same program on both engines and diff the output.
- Make one optimization and record the instruction change.
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
- What information does each column of a disassembly listing provide?
- Why are instruction counts better than time for regression gates?
- What must match between interpreter and VM runs?
- Why should you change one thing at a time when optimizing?
- When is a host call, rather than PeaLang code, the real bottleneck?
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
- Disassembly shows exactly what the compiler produced.
- Profiles separate deterministic instruction counts from variable time.
- Interpreter and VM must agree on results, errors, and locations.
- Optimize by measuring, changing one thing, and re-verifying.
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.