Part III — Advanced Systems · Chapter 12 · Lesson 7 of 7 · 4 min read
Chapter 12 Checkpoint: Inspect and Optimize Compiled Execution
Checkpoint level: Advanced
Purpose: Apply the full execution pipeline to one module: check it, compile it to PEAC, validate and inspect the artifact, profile it, make one safe optimization, and prove the interpreter and VM still agree.
Release note: commands, flags, and output formats are illustrative. Verify them against your installed v0.63 release.
Checkpoint outcomes
By completing this checkpoint, you will demonstrate that you can:
- trace a module through lexing, parsing, and semantic checks;
- compile it to PEAC and read its header and disassembly;
- show that the loader rejects corrupted artifacts;
- profile execution and choose one justified optimization;
- prove behavioural equivalence across engines; and
- record before-and-after evidence suitable for review.
Scenario
The order_rules batch job prices 10,000 synthetic orders each night. Operations reports it now runs close to its instruction budget. Your task is to find the cause, fix it without changing any decision, and leave evidence that the fix is safe.
12C.1 Starting module
const policy = {"bulk_threshold": 10, "gold_rate": 0.10, "bulk_rate": 0.05}
function reason_for(order) {
let gold = order["tier"] == "gold"
let bulk = order["qty"] >= policy["bulk_threshold"]
if gold and bulk { return "GOLD_BULK" }
if gold { return "GOLD" }
if bulk { return "BULK" }
return "NONE"
}
function discount_for(order) {
let r = reason_for(order)
if r == "GOLD_BULK" { return policy["gold_rate"] + policy["bulk_rate"] }
if r == "GOLD" { return policy["gold_rate"] }
if r == "BULK" { return policy["bulk_rate"] }
return 0
}
export function price_all(orders) {
let out = []
for order in orders {
let base = order["qty"] * order["unit_price"]
let d = discount_for(order)
out.append({"id": order["id"], "reason": reason_for(order), "total": base - base * d})
}
return out
}
Note that reason_for is called twice per order and that policy values are looked up inside the loop on every call.
12C.2 Required workflow
- Run the checker and record any diagnostics.
- Compile to PEAC and record the header fields.
- Disassemble reason_for and price_all.
- Corrupt a copy of the artifact and record the loader error.
- Profile price_all on the 10,000-order fixture.
- Choose one optimization and justify it from the profile.
- Apply it, rerun tests, compare engines, and re-profile.
- Write the evidence report.
12C.3 Reference commands
pea check src/pricing.pea
pea compile src/pricing.pea -o build/pricing.peac
pea inspect build/pricing.peac
pea disasm build/pricing.peac
pea profile tests/batch_fixture.pea --top 10
pea compare tests/batch_fixture.pea --engines interp,vm
pea test
12C.4 Reference optimization
The profile shows reason_for called twice per order and repeated policy map lookups. One justified change is to compute the reason once and pass it in, and to read policy values into constants:
const bulk_threshold = policy["bulk_threshold"]
const gold_rate = policy["gold_rate"]
const bulk_rate = policy["bulk_rate"]
function discount_for_reason(r) {
if r == "GOLD_BULK" { return gold_rate + bulk_rate }
if r == "GOLD" { return gold_rate }
if r == "BULK" { return bulk_rate }
return 0
}
export function price_all(orders) {
let out = []
for order in orders {
let base = order["qty"] * order["unit_price"]
let r = reason_for(order)
out.append({"id": order["id"], "reason": r, "total": base - base * discount_for_reason(r)})
}
return out
}
reason_for is updated to use bulk_threshold. Public behaviour of price_all is unchanged.
12C.5 Required test matrix
| Case | Check | Expected result |
|---|---|---|
| A | Checker on original and optimized source | No errors |
| B | Load corrupted PEAC | Refused before execution |
| C | Load PEAC with an unsupported bytecode version | Refused with a version message |
| D | Engine comparison on the fixture | Identical output and errors |
| E | Boundary orders: qty 9, 10, 11 for gold and non-gold | Reasons NONE/BULK and GOLD/GOLD_BULK as expected |
| F | Instruction count before and after | Measurable reduction recorded |
| G | Full Chapter 10 test suite | All pass |
12C.6 Acceptance criteria
- No decision changes for any fixture order.
- Both engines agree on the optimized module.
- The corrupted and wrong-version artifacts never execute.
- The instruction count reduction is measured, not estimated.
- The disassembly shows a single reason_for call per loop iteration.
- The evidence report is complete and reproducible.
12C.7 Evidence report
- header fields of the original and optimized PEAC;
- annotated disassembly of the changed functions;
- before-and-after profiles with instruction counts;
- engine comparison output;
- loader output for cases B and C; and
- a one-paragraph justification of the change.
12C.8 Assessment rubric
| Area | Evidence of mastery |
|---|---|
| Pipeline understanding | Each stage is identified and its output recorded. |
| Artifact safety | Corrupted and incompatible files are rejected. |
| Measurement | The optimization is chosen from profile data. |
| Equivalence | Engines and tests prove identical behaviour. |
| Communication | The report lets a reviewer reproduce every result. |
Completion standard: Every test case passes, the instruction count falls, and no order receives a different reason or total.
12C.9 Reflection questions
- Which evidence would convince a reviewer most quickly?
- What would you do if the engines disagreed after the change?
- Which other hot spot would you investigate next?
- How would this change affect the Chapter 14 instruction budget?
12C.10 Optional extensions
- Add an instruction-count gate to CI for price_all.
- Compare PEAC load time against compiling from source at startup.
- Write a peephole rule and test it with the differential suite.
Chapter 12 summary
You can now follow PeaLang from source text to tokens, syntax trees, and checked structure; run that structure by tree-walking; compile it to stack bytecode; store it in validated PEAC files; execute it on the VM with frames, closures, modules, and exceptions; and inspect, profile, and compare execution to optimize safely.
Transition to Chapter 13
Chapter 13 adds time and waiting to the model. It begins with async functions, await, and cooperative suspension.
Checkpoint status: Ready for review.