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

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:

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

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

CaseCheckExpected result
AChecker on original and optimized sourceNo errors
BLoad corrupted PEACRefused before execution
CLoad PEAC with an unsupported bytecode versionRefused with a version message
DEngine comparison on the fixtureIdentical output and errors
EBoundary orders: qty 9, 10, 11 for gold and non-goldReasons NONE/BULK and GOLD/GOLD_BULK as expected
FInstruction count before and afterMeasurable reduction recorded
GFull Chapter 10 test suiteAll pass

12C.6 Acceptance criteria

12C.7 Evidence report

12C.8 Assessment rubric

AreaEvidence of mastery
Pipeline understandingEach stage is identified and its output recorded.
Artifact safetyCorrupted and incompatible files are rejected.
MeasurementThe optimization is chosen from profile data.
EquivalenceEngines and tests prove identical behaviour.
CommunicationThe 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

12C.10 Optional extensions

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.

← Previous Next →