-
Notifications
You must be signed in to change notification settings - Fork 69
feat(driver): add per-phase pipeline timing instrumentation #1741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ghaith
wants to merge
4
commits into
master
Choose a base branch
from
incremental/pr-a-phase-timing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0475ae8
feat(driver): add per-phase pipeline timing instrumentation
ghaith f41aeba
feat(driver): time the link step and document build vs check traces
ghaith 614447e
perf(driver): skip lowering hooks whose preconditions don't hold (#1744)
ghaith 17ceaaf
Merge branch 'master' into incremental/pr-a-phase-timing
ghaith File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # Profiling Build Phases | ||
|
|
||
| The compiler driver has a built-in phase timer that records wall-clock | ||
| time for each stage of `BuildPipeline` and for every participant | ||
| invocation. It is intended for ad-hoc performance work — for example, | ||
| when investigating why a particular project compiles slower than | ||
| expected, or when measuring the impact of a change to the lowering | ||
| pipeline. | ||
|
|
||
| ## Enabling | ||
|
|
||
| Set the environment variable `PLC_TIMING=1` before invoking the | ||
| compiler: | ||
|
|
||
| ```sh | ||
| PLC_TIMING=1 plc --check my_project.st | ||
| ``` | ||
|
|
||
| When the variable is unset (or set to `0` / empty), the timers compile | ||
| to a no-op and emit nothing. There is no behavioural difference and no | ||
| test impact, so it is safe to leave the code path always present. | ||
|
|
||
| ## Reading the output | ||
|
|
||
| Each timed scope writes one line to stderr on completion, indented by | ||
| nesting depth. Children appear *before* their parent's log line (the | ||
| parent prints when it drops, i.e. at end-of-scope), which matches the | ||
| standard flame-graph convention. | ||
|
|
||
| Each participant invocation is timed individually with a label of | ||
| the form `<hook>/<participant-type-name>`, e.g. | ||
| `post_index/PolymorphismLowerer`. `ParsedProject::index` and | ||
| `IndexedProject::annotate` self-time, so any participant that | ||
| re-invokes them appears with a nested `ParsedProject::index` (or | ||
| `IndexedProject::annotate`) child line. Those nested re-passes are | ||
| the main thing to look for when investigating slow builds. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### `plc build` (full pipeline) | ||
|
|
||
| `plc build plc.json` runs every phase end-to-end — parsing, indexing, | ||
| annotation, codegen, and linking. The trace shows all four | ||
| top-level driver scopes, with `link` nested inside | ||
| `generate (driver)`: | ||
|
|
||
| ```text | ||
| [plc-timing] parse: 25.7ms | ||
| [plc-timing] pre_index (participants): 12.6ms | ||
| [plc-timing] pre_index/LoopDesugarer: 6.6ms | ||
| [plc-timing] pre_index/ControlStatementParticipant: 3.0ms | ||
| [plc-timing] ParsedProject::index: 7.4ms | ||
| [plc-timing] post_index (participants): 27.6ms | ||
| [plc-timing] post_index/PolymorphismLowerer: 11.7ms | ||
| [plc-timing] ParsedProject::index: 9.6ms | ||
| [plc-timing] post_index/RetainParticipant: 15.8ms | ||
| [plc-timing] ParsedProject::index: 5.8ms | ||
| [plc-timing] index (driver): 47.6ms | ||
| [plc-timing] annotate (driver): 615.2ms | ||
| [plc-timing] link: 32.1ms | ||
| [plc-timing] generate (driver): 184.4ms | ||
| ``` | ||
|
|
||
| The default mode (`plc <files>` without a subcommand) follows the | ||
| same shape — same four scopes, same nesting. | ||
|
|
||
| ### `plc check` (front-end only) | ||
|
|
||
| `plc check plc.json` (or the global `--check` flag) stops the | ||
| pipeline after annotation; codegen and linking never run, so the | ||
| trace ends at `annotate`: | ||
|
|
||
| ```text | ||
| [plc-timing] parse: 24.9ms | ||
| [plc-timing] pre_index (participants): 12.4ms | ||
| [plc-timing] ParsedProject::index: 7.3ms | ||
| [plc-timing] post_index (participants): 27.0ms | ||
| [plc-timing] index (driver): 47.0ms | ||
| [plc-timing] annotate (driver): 612.8ms | ||
| ``` | ||
|
|
||
| This is the right mode for measuring front-end work in isolation — | ||
| the codegen and link costs are excluded. | ||
|
|
||
| ## What to look for | ||
|
|
||
| The trace is most useful for spotting **redundant whole-project | ||
| re-passes**: cases where a participant mutates the AST and then re-runs | ||
| indexing or annotation against the entire project, even though only a | ||
| few units were touched. A nested `ParsedProject::index` or | ||
| `IndexedProject::annotate` under a participant hook is a visible | ||
| indicator of one of those re-passes. | ||
|
|
||
| ## Adding new timed scopes | ||
|
|
||
| To time a new scope, construct a `PhaseTimer` and let it drop at the | ||
| end of the scope you want to measure: | ||
|
|
||
| ```rust | ||
| use crate::pipelines::timing::PhaseTimer; | ||
|
|
||
| fn expensive_thing() { | ||
| let _t = PhaseTimer::new("expensive_thing"); | ||
| // ... work ... | ||
| } | ||
| ``` | ||
|
|
||
| For participant-style instrumentation, prefer wrapping the *callee* | ||
| (the inner method that does the work) rather than each call site. That | ||
| way a re-entrant call from a participant gets timed automatically, | ||
| without having to thread timer code through every place that might | ||
| call into the method. | ||
|
|
||
| The timer label is the only argument and accepts any | ||
| `Into<String>`. Use a stable, easily-greppable string — these strings | ||
| end up in the trace output and may be parsed by tooling. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already chatted with @ghaith about this, but this pattern is quite strange and probably merits a rethink when we get back to this PR.