/** * Wall-time measurement for the compiler's own phases: `performance` * accumulates the time spent under each named phase, over the * standard library's monotonic clock, which every target supplies. * * A phase keeps a total and a call count, and `phases()` returns them * in the order each name was first measured. `--time` prints that list, * so a `performance.start` added anywhere in the compiler appears in * the report without a line being added for it. * * `start` and `end` allocate a Measure per call. That is nothing for a * phase entered once per compile and too much for one entered once per * function; for those, read `nowMs` into a local, accumulate over the * loop, and `add` the total once at the end. */ import { monotonic } from 'zena:time'; /** The monotonic clock, in milliseconds. */ export function nowMs(): f64 { return ((monotonic() as i64) as f64) / 1000000.0; } final class Measure { name: String; startTime: f64; new(this.name, this.startTime); } /** * One phase's accumulated time and call count. Only `Performance` * writes the fields; a caller reads them off `phases()`. */ export final class Phase { name: String; /** Milliseconds accumulated under this name. */ var total: f64 = 0.0; /** How many measures have closed under this name. */ var count: i32 = 0; new(this.name); } /** * Named phase timings. `start` opens a measure and returns its id; * `end` closes it and adds its duration to the phase's total. */ export final class Performance { var #nextId: i32 = 0; // How many nested compiles deep we are. A depth rather than a flag // so that a nested compile inside a nested compile still resumes at // the right level. var #suspendDepth: i32 = 0; #active = new Map(); #byName = new Map(); // The same phases in the order each was first measured, which is the // order they run in, and so the order to report them in. var #inOrder = new GrowableArray(); /** * Stops recording until the matching `resume`, for a nested compile * whose phases would otherwise be added to the enclosing compile's. * * A compile inside a compile — the component target builds the * runtime memory module this way — loads, parses and checks its own * files, and counting that work in the same totals made the report * exceed wall-clock time: once inside the phase that contains the * nested compile, and again in the top-level `load`, `parse`, * `scope` and `check`. The nested compile's own breakdown is lost, * which is the price; the caller still times it as one phase. */ suspend(): void { this.#suspendDepth += 1; } /** Resumes recording. See `suspend`. */ resume(): void { if (this.#suspendDepth > 0) { this.#suspendDepth -= 1; } } start(name: String): i32 { if (this.#suspendDepth > 0) { // -1 is not a key in `#active`, so the matching `end` is a no-op. return -1; } // Register the phase now rather than when it closes, so that a // phase that encloses others is ordered — and so reported — ahead // of them. `check` opens first and closes last. this.#phase(name); let id = this.#nextId; this.#nextId += 1; this.#active[id] = new Measure(name, nowMs()); return id; } end(id: i32): f64 { if (this.#active.has(id)) { let measure = this.#active[id]; this.#active.delete(id); let duration = nowMs() - measure.startTime; this.add(measure.name, duration); return duration; } return 0.0; } /** * Adds `ms` to `name` without opening a measure: for a phase timed * with `nowMs` around a loop, where a Measure per iteration would * cost more than the phase being measured. * * `times` is how much to raise the count by, for a counter kept in a * local across a loop and reported once. Counting in the loop and * reporting after it keeps the hot path free of both the clock and * the map lookup, which matters where the thing being counted is the * reason the loop is hot. */ add(name: String, ms: f64, times: i32 = 1): void { if (this.#suspendDepth > 0) { return; } let phase = this.#phase(name); phase.total = phase.total + ms; phase.count = phase.count + times; } #phase(name: String): Phase { if (let (true, existing) = this.#byName.get(name)) { return existing; } let created = new Phase(name); this.#byName[name] = created; this.#inOrder.push(created); return created; } getDuration(name: String): f64 { if (let (true, phase) = this.#byName.get(name)) { return phase.total; } return 0.0; } getCount(name: String): i32 { if (let (true, phase) = this.#byName.get(name)) { return phase.count; } return 0; } /** Every phase measured so far, in the order each was first measured. */ phases(): GrowableArray { let all = new Array(this.#inOrder.length); for (let phase in this.#inOrder) { all.push(phase); } return all; } clear(): void { this.#active.clear(); this.#byName.clear(); this.#inOrder = new GrowableArray(); } } export let performance = new Performance();