-
-
Notifications
You must be signed in to change notification settings - Fork 129
perf(hir): O(1) scope resolution — index local bindings by name (#5267) #5270
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
Merged
Merged
Changes from all commits
Commits
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
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,175 @@ | ||
| //! Scope-local bindings with an O(1) name→position index (#5267). | ||
| //! | ||
| //! The lowerer resolves every identifier reference by finding the | ||
| //! innermost (most-recently-pushed) binding with a given name. The old | ||
| //! representation was a bare `Vec<(String, LocalId, Type)>` scanned with a | ||
| //! reverse `find`/`rposition` per reference, so a scope holding N bindings | ||
| //! with N references lowered in **O(n²)** time. Real minified/bundled JS | ||
| //! puts tens of thousands of bindings and references in one module (or one | ||
| //! wrapper-function) scope, so `check-lower` stalled for minutes and got | ||
| //! killed with no diagnostic on large bundles. | ||
| //! | ||
| //! `Locals` keeps the same ordered stack (`entries`, the authoritative | ||
| //! source of truth) plus a side `index` mapping each name to the ascending | ||
| //! list of positions in `entries` that currently hold that name. The *last* | ||
| //! position in a name's list is the innermost binding, so `lookup`, | ||
| //! `lookup_index`, and `lookup_type` are O(1). | ||
| //! | ||
| //! Every operation that moves a binding's position (`push`, `drain_from`, | ||
| //! `extend`, `remove`) goes through an inherent method here that keeps | ||
| //! `index` in sync. Read-only and in-place ops (`iter`, `iter_mut`, | ||
| //! slicing, `len`) reach the underlying slice via `Deref`/`DerefMut` — | ||
| //! those never move a binding or rename it, so the index stays valid. Note | ||
| //! `DerefMut` hands out `&mut [..]` (a slice), **not** `&mut Vec`, so | ||
| //! callers cannot bypass the index with `Vec::push`/`remove`/`truncate`. | ||
|
|
||
| use std::ops::{Deref, DerefMut}; | ||
|
|
||
| use perry_types::{LocalId, Type}; | ||
| use std::collections::HashMap; | ||
|
|
||
| /// Ordered stack of scope-local bindings with a name→positions index. | ||
| #[derive(Debug, Clone, Default)] | ||
| pub(crate) struct Locals { | ||
| /// Authoritative ordered list of `(name, id, type)` bindings. | ||
| entries: Vec<(String, LocalId, Type)>, | ||
| /// name -> ascending positions into `entries`. The last entry of each | ||
| /// list is the innermost (most-recent) binding for that name. | ||
| index: HashMap<String, Vec<usize>>, | ||
| } | ||
|
|
||
| impl Locals { | ||
| pub(crate) fn new() -> Self { | ||
| Self::default() | ||
| } | ||
|
|
||
| /// Push a new innermost binding. O(1) amortized. | ||
| pub(crate) fn push(&mut self, entry: (String, LocalId, Type)) { | ||
| let pos = self.entries.len(); | ||
| self.index.entry(entry.0.clone()).or_default().push(pos); | ||
| self.entries.push(entry); | ||
| } | ||
|
|
||
| /// Position of the innermost binding named `name`, if any. O(1). | ||
| /// Equivalent to the old `iter().rposition(|(n, ..)| n == name)`. | ||
| pub(crate) fn lookup_index(&self, name: &str) -> Option<usize> { | ||
| self.index.get(name).and_then(|v| v.last()).copied() | ||
| } | ||
|
|
||
| /// `LocalId` of the innermost binding named `name`, if any. O(1). | ||
| pub(crate) fn lookup(&self, name: &str) -> Option<LocalId> { | ||
| self.lookup_index(name).map(|i| self.entries[i].1) | ||
| } | ||
|
|
||
| /// `Type` of the innermost binding named `name`, if any. O(1). | ||
| pub(crate) fn lookup_type(&self, name: &str) -> Option<&Type> { | ||
| self.lookup_index(name).map(|i| &self.entries[i].2) | ||
| } | ||
|
|
||
| /// Mutable `Type` of the innermost binding named `name`, if any. O(1). | ||
| /// Replaces the old `iter_mut().rev().find(|(n, ..)| n == name)` type | ||
| /// patch-ups that were O(n) per declaration (#5267). | ||
| pub(crate) fn lookup_type_mut(&mut self, name: &str) -> Option<&mut Type> { | ||
| let idx = self.lookup_index(name)?; | ||
| Some(&mut self.entries[idx].2) | ||
| } | ||
|
|
||
| /// Position of the innermost binding named `name` whose position is at | ||
| /// or past `min_pos` (i.e. introduced in the current scope), if any. | ||
| /// O(1): the innermost binding for a name has the maximal position, so | ||
| /// one exists at `>= min_pos` iff that maximum is `>= min_pos`. Replaces | ||
| /// the `iter().enumerate().rev().any(|(idx, (n, ..))| n == name && idx >= | ||
| /// min_pos)` scans that were O(n) per declaration (#5267). | ||
| pub(crate) fn lookup_index_in_scope(&self, name: &str, min_pos: usize) -> Option<usize> { | ||
| self.lookup_index(name).filter(|&pos| pos >= min_pos) | ||
| } | ||
|
|
||
| /// Iterate the bindings named `name` from innermost to outermost | ||
| /// (descending position), yielding `(position, &binding)`. O(number of | ||
| /// bindings sharing `name`) — a single step for the common case of a | ||
| /// unique name — instead of the old O(n) reverse scan of the entire | ||
| /// stack (#5267). Used by the `var`-redeclaration reuse checks, which | ||
| /// need the innermost binding matching an extra predicate (e.g. | ||
| /// var-hoisted) plus its position for an O(1) type patch-up afterwards. | ||
| pub(crate) fn iter_named<'a>( | ||
| &'a self, | ||
| name: &'a str, | ||
| ) -> impl Iterator<Item = (usize, &'a (String, LocalId, Type))> + 'a { | ||
| self.index | ||
| .get(name) | ||
| .into_iter() | ||
| .flat_map(move |positions| positions.iter().rev().map(move |&p| (p, &self.entries[p]))) | ||
| } | ||
|
|
||
| /// Mutable `Type` of the binding at `pos`. Pairs with `iter_named` / | ||
| /// `lookup_index*` to patch a binding's inferred type without a second | ||
| /// O(n) scan to re-find it (#5267). | ||
| pub(crate) fn type_mut_at(&mut self, pos: usize) -> &mut Type { | ||
| &mut self.entries[pos].2 | ||
| } | ||
|
|
||
| /// Drain every binding at position `>= mark` (a scope/block pop), | ||
| /// returning the drained entries in ascending-position order. The caller | ||
| /// may filter and `extend` survivors back (var-hoisted / sloppy globals). | ||
| pub(crate) fn drain_from(&mut self, mark: usize) -> Vec<(String, LocalId, Type)> { | ||
| if mark >= self.entries.len() { | ||
| return Vec::new(); | ||
| } | ||
| let drained: Vec<(String, LocalId, Type)> = self.entries.drain(mark..).collect(); | ||
| // Each drained entry sat at a position `>= mark`, and for any single | ||
| // name those positions are the highest in its index list. Walking the | ||
| // drained entries in reverse (descending positions) lets us pop them | ||
| // off the back one-for-one, leaving any `< mark` positions intact. | ||
| for (name, _, _) in drained.iter().rev() { | ||
| let now_empty = match self.index.get_mut(name) { | ||
| Some(positions) => { | ||
| positions.pop(); | ||
| positions.is_empty() | ||
| } | ||
| None => false, | ||
| }; | ||
| if now_empty { | ||
| self.index.remove(name); | ||
| } | ||
| } | ||
| drained | ||
| } | ||
|
|
||
| /// Re-append previously-drained survivors, assigning fresh (compacted) | ||
| /// positions. Mirrors `Vec::extend`; keeps the index in sync. | ||
| pub(crate) fn extend<I: IntoIterator<Item = (String, LocalId, Type)>>(&mut self, iter: I) { | ||
| for entry in iter { | ||
| self.push(entry); | ||
| } | ||
| } | ||
|
|
||
| /// Remove the binding at `idx`, shifting later bindings down by one. | ||
| /// Rare (native-require dedup, #5216), so a full O(n) reindex is fine. | ||
| pub(crate) fn remove(&mut self, idx: usize) -> (String, LocalId, Type) { | ||
| let removed = self.entries.remove(idx); | ||
| self.reindex(); | ||
| removed | ||
| } | ||
|
|
||
| /// Rebuild the whole name→positions index from `entries`. | ||
| fn reindex(&mut self) { | ||
| self.index.clear(); | ||
| for (pos, (name, _, _)) in self.entries.iter().enumerate() { | ||
| self.index.entry(name.clone()).or_default().push(pos); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Deref for Locals { | ||
| type Target = [(String, LocalId, Type)]; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.entries | ||
| } | ||
| } | ||
|
|
||
| impl DerefMut for Locals { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| &mut self.entries | ||
| } | ||
| } | ||
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
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.
🛠️ Refactor suggestion | 🟠 Major | 🏗️ Heavy lift
Lock down mutable slice access to preserve
Localsindex invariants.Line 171 currently exposes
&mut [(String, LocalId, Type)]. That still permits reordering (swap/slice ops) and renaming (entry.0), which can desynchronizeindexfromentriesand makelookup*return incorrect bindings. Please keep all index-affecting mutations behindLocalsmethods only.Proposed direction
🤖 Prompt for AI Agents