Skip to content

perf(lowering): index optimization directives per file - #606

Merged
Teakowa merged 1 commit into
mainfrom
perf/optimization-state-index
Oct 19, 2026
Merged

Teakowa merged 1 commit into
mainfrom
perf/optimization-state-index

Conversation

@e54-bot

@e54-bot e54-bot commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #596

optimization_state_at scanned every #! directive record for each lowered value. It now looks up a per-file index built once per Lowering (a map from file to the directive positions and states, queried with a binary search for the last directive at or before the position). The previous scan is kept in test code only, as the oracle.

Measured on the Bastion project (211 files, zh-CN; lpp/compile-equivalent harness, release, 14+ interleaved warm runs, medians):

before after
backend 189.3 ms 164.2 ms
total in-process compile 301.4 ms 280.4 ms

The gain, about 21–25 ms, is lower than the Issue's 30–40 ms estimate. The estimate came from a sampling profile; a timer around the function had reported only about 7 ms. The measurements were taken while the machine carried unrelated load, so treat the numbers as approximate.

Equivalence: the emitted Bastion output is byte-identical before and after (sha256 7f53ffa8…). Tests compare the index with the old scan over every span lowered by the corpus, a synthetic edge-case fixture (no span, before the first directive, exactly at a directive, one column before, included files, unknown file ids), and the full Bastion project when OPY_BASTION_PROJECT is set (81,927 queries, no disagreement). The new corpus fixture synthetic/optimization-state-edge carries an oracle.json produced by the pinned upstream OverPy 9.7.17, not by this compiler.

cargo fmt --check, cargo clippy --workspace --all-targets -D warnings and cargo test --workspace pass with no changed expectations.

optimization_state_at scanned every recorded #! directive event for each
lowered value, making the cost values x directives (about 81,927 queries
against 652 records on Bastion). Build a per-file index of directive
positions and optimization states once per lowering and answer each query
with a partition_point binary search over that file's events.

Semantics are unchanged: the index reproduces the scan's ordering (last
directive at or before the query position in the same file wins), including
files with no directive, positions before the first directive, included
files, and spans without file information. The emitted Bastion Workshop
output is byte-identical (sha256 7f53ffa8...) and the previous scan is
retained as the test-only oracle optimization_state_scan; new tests diff it
against the index over every span lowered by the corpus, a synthetic
edge-case fixture, and the full Bastion project (OPY_BASTION_PROJECT).

Measured on the lpp/compile-equivalent harness (release, 14+ interleaved
warm runs): backend median 189.3ms -> 164.2ms, total median 301.4ms ->
280.4ms on a machine under unrelated load. The ~25ms backend gain is
slightly below the issue's 30-40ms profile estimate.

Fixes #596
@Teakowa
Teakowa merged commit 7914b06 into main Oct 19, 2026
6 checks passed
@Teakowa
Teakowa deleted the perf/optimization-state-index branch October 19, 2026 12:44
@e54-bot e54-bot mentioned this pull request Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make optimization_state_at independent of the number of directives

2 participants