-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Remove ScopeStack
in favor of child-parent ScopeId
pointers
#4138
Conversation
7f5258b
to
f47a9f3
Compare
(A couple things I want to clean up here but if anything seems way off base, feel free to flag it @MichaReiser.) |
PR Check ResultsEcosystem✅ ecosystem check detected no changes. BenchmarkLinux
Windows
|
10bb124
to
d87cf3d
Compare
} | ||
|
||
/// Returns the id of the top-most scope | ||
pub fn scope_id(&self) -> ScopeId { |
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.
I removed this method since scope_id
is still pub
, and it's confusing (IMO) to have a getter for a public field.
It's hard to use this method everywhere (same with some of the utilities, like scopes
) because of the borrow checker semantics. Often, we borrow (e.g.) self.ctx.scope_id
, then mutate self.ctx.bindings
or something similar. The borrow checker is fine with that. But if we use self.ctx.scope_id()
, we can't mutate other parts of self.ctx
, since that's considered an immutable borrow of self.ctx
.
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.
Hmm, scope_id
shouldn't borrow because it's copied. I do see value in migrating towards getters to avoid our very loose APIs and long getter chains to context-specific data structures.
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.
Oh, hmm, perhaps that does work here, I have to check. (It doesn't work in general for references, but you're right that I'd expect it to work with copies.)
I don't disagree, but if we add / retain this, I think we should try to remove pub
from the scope_id
attribute. Otherwise, it's unclear which access pattern to use (and editor completion is confusing too).
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.
Nice!
I have one question and one improvement suggestion:
- What's currently preventing us from removing
ctx.parents
? - I think it should now be possible to remove
Scope.id
by:- Internally storing a
ScopeData
that matches today'sScope
without theid
- Create a
Scope
struct that references aScopeData
and stores theid
with the scope - The iterator constructs
Scope
s and uses the implicitindex
of theScope
as theScopeId
.
- Internally storing a
)); | ||
self.deferred | ||
.for_loops | ||
.push((stmt, (self.ctx.scope_id, self.ctx.parents.clone()))); |
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.
Is it still necessary to clone parents
or can we even remove parents
?
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.
I think we can remove parents
soon, but it requires a similar approach. We do need to set the list of parents when we "restore" state, but we should be able to use IDs with parent pointers, just like with scopes.
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.
We may also want to consider using indextree
for statements, to enable us to traverse up and get symbolic representations?
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.
(Ignore the indextree
reference here, I think we can make improvements to parent tracking but that crate might end up being irrelevant.)
This reverts commit 51f14f3.
d87cf3d
to
cbe31ef
Compare
cbe31ef
to
30b53b7
Compare
I was able to remove |
To clarify: separate |
Biasing towards merging but always happy to handle follow-ups in new PRs :) |
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [ruff](https://togithub.com/charliermarsh/ruff) | `^0.0.263` -> `^0.0.264` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/compatibility-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/confidence-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.264`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.264) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.263...v0.0.264) <!-- Release notes generated using configuration in .github/release.yml at 8cb76f85eba1c970a8c800348fd1e0c874621a57 --> #### What's Changed ##### Rules - Autofix `EM101`, `EM102`, `EM103` if possible by [@​dhruvmanila](https://togithub.com/dhruvmanila) in [https://github.com/charliermarsh/ruff/pull/4123](https://togithub.com/charliermarsh/ruff/pull/4123) - Add bugbear immutable functions as allowed in dataclasses by [@​mosauter](https://togithub.com/mosauter) in [https://github.com/charliermarsh/ruff/pull/4122](https://togithub.com/charliermarsh/ruff/pull/4122) ##### Settings - Add support for providing command-line arguments via `argfile` by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4087](https://togithub.com/charliermarsh/ruff/pull/4087) ##### Bug Fixes - Make D410/D411 autofixes mutually exclusive by [@​evanrittenhouse](https://togithub.com/evanrittenhouse) in [https://github.com/charliermarsh/ruff/pull/4110](https://togithub.com/charliermarsh/ruff/pull/4110) - Remove `pyright` comment prefix from PYI033 checks by [@​evanrittenhouse](https://togithub.com/evanrittenhouse) in [https://github.com/charliermarsh/ruff/pull/4152](https://togithub.com/charliermarsh/ruff/pull/4152) - Fix F811 false positive with match by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [https://github.com/charliermarsh/ruff/pull/4161](https://togithub.com/charliermarsh/ruff/pull/4161) - Fix `E713` and `E714` false positives for multiple comparisons by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [https://github.com/charliermarsh/ruff/pull/4083](https://togithub.com/charliermarsh/ruff/pull/4083) - Fix B023 shadowed variables in nested functions by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/4111](https://togithub.com/charliermarsh/ruff/pull/4111) - Preserve star-handling special-casing for force-single-line by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4129](https://togithub.com/charliermarsh/ruff/pull/4129) - Respect parent-scoping rules for `NamedExpr` assignments by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4145](https://togithub.com/charliermarsh/ruff/pull/4145) - Fix UP032 auto-fix by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [https://github.com/charliermarsh/ruff/pull/4165](https://togithub.com/charliermarsh/ruff/pull/4165) - Allow boolean parameters for `pytest.param` by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4176](https://togithub.com/charliermarsh/ruff/pull/4176) ##### Internal - Replace row/column based `Location` with byte-offsets. by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/3931](https://togithub.com/charliermarsh/ruff/pull/3931) - perf(logical-lines): Various small perf improvements by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/4022](https://togithub.com/charliermarsh/ruff/pull/4022) - Use `memchr` to speedup newline search on x86 by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/3985](https://togithub.com/charliermarsh/ruff/pull/3985) - Remove `ScopeStack` in favor of child-parent `ScopeId` pointers by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4138](https://togithub.com/charliermarsh/ruff/pull/4138) **Full Changelog**: astral-sh/ruff@v0.0.263...v0.0.264 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/ixm-one/pytest-cmake-presets). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS42Ni4zIiwidXBkYXRlZEluVmVyIjoiMzUuNjYuMyIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==--> Signed-off-by: Renovate Bot <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [ruff](https://togithub.com/charliermarsh/ruff) | `==0.0.263` -> `==0.0.264` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/compatibility-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/confidence-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.264`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.264) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.263...v0.0.264) <!-- Release notes generated using configuration in .github/release.yml at 8cb76f85eba1c970a8c800348fd1e0c874621a57 --> #### What's Changed ##### Rules - Autofix `EM101`, `EM102`, `EM103` if possible by [@​dhruvmanila](https://togithub.com/dhruvmanila) in [https://github.com/charliermarsh/ruff/pull/4123](https://togithub.com/charliermarsh/ruff/pull/4123) - Add bugbear immutable functions as allowed in dataclasses by [@​mosauter](https://togithub.com/mosauter) in [https://github.com/charliermarsh/ruff/pull/4122](https://togithub.com/charliermarsh/ruff/pull/4122) ##### Settings - Add support for providing command-line arguments via `argfile` by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4087](https://togithub.com/charliermarsh/ruff/pull/4087) ##### Bug Fixes - Make D410/D411 autofixes mutually exclusive by [@​evanrittenhouse](https://togithub.com/evanrittenhouse) in [https://github.com/charliermarsh/ruff/pull/4110](https://togithub.com/charliermarsh/ruff/pull/4110) - Remove `pyright` comment prefix from PYI033 checks by [@​evanrittenhouse](https://togithub.com/evanrittenhouse) in [https://github.com/charliermarsh/ruff/pull/4152](https://togithub.com/charliermarsh/ruff/pull/4152) - Fix F811 false positive with match by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [https://github.com/charliermarsh/ruff/pull/4161](https://togithub.com/charliermarsh/ruff/pull/4161) - Fix `E713` and `E714` false positives for multiple comparisons by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [https://github.com/charliermarsh/ruff/pull/4083](https://togithub.com/charliermarsh/ruff/pull/4083) - Fix B023 shadowed variables in nested functions by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/4111](https://togithub.com/charliermarsh/ruff/pull/4111) - Preserve star-handling special-casing for force-single-line by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4129](https://togithub.com/charliermarsh/ruff/pull/4129) - Respect parent-scoping rules for `NamedExpr` assignments by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4145](https://togithub.com/charliermarsh/ruff/pull/4145) - Fix UP032 auto-fix by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [https://github.com/charliermarsh/ruff/pull/4165](https://togithub.com/charliermarsh/ruff/pull/4165) - Allow boolean parameters for `pytest.param` by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4176](https://togithub.com/charliermarsh/ruff/pull/4176) ##### Internal - Replace row/column based `Location` with byte-offsets. by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/3931](https://togithub.com/charliermarsh/ruff/pull/3931) - perf(logical-lines): Various small perf improvements by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/4022](https://togithub.com/charliermarsh/ruff/pull/4022) - Use `memchr` to speedup newline search on x86 by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/3985](https://togithub.com/charliermarsh/ruff/pull/3985) - Remove `ScopeStack` in favor of child-parent `ScopeId` pointers by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4138](https://togithub.com/charliermarsh/ruff/pull/4138) **Full Changelog**: astral-sh/ruff@v0.0.263...v0.0.264 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/allenporter/flux-local). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS42OS4zIiwidXBkYXRlZEluVmVyIjoiMzUuNjkuMyIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [ruff](https://togithub.com/charliermarsh/ruff) | `==0.0.263` -> `==0.0.264` | [![age](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/age-slim)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/adoption-slim)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/compatibility-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://badges.renovateapi.com/packages/pypi/ruff/0.0.264/confidence-slim/0.0.263)](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>charliermarsh/ruff</summary> ### [`v0.0.264`](https://togithub.com/charliermarsh/ruff/releases/tag/v0.0.264) [Compare Source](https://togithub.com/charliermarsh/ruff/compare/v0.0.263...v0.0.264) <!-- Release notes generated using configuration in .github/release.yml at 8cb76f85eba1c970a8c800348fd1e0c874621a57 --> #### What's Changed ##### Rules - Autofix `EM101`, `EM102`, `EM103` if possible by [@​dhruvmanila](https://togithub.com/dhruvmanila) in [https://github.com/charliermarsh/ruff/pull/4123](https://togithub.com/charliermarsh/ruff/pull/4123) - Add bugbear immutable functions as allowed in dataclasses by [@​mosauter](https://togithub.com/mosauter) in [https://github.com/charliermarsh/ruff/pull/4122](https://togithub.com/charliermarsh/ruff/pull/4122) ##### Settings - Add support for providing command-line arguments via `argfile` by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4087](https://togithub.com/charliermarsh/ruff/pull/4087) ##### Bug Fixes - Make D410/D411 autofixes mutually exclusive by [@​evanrittenhouse](https://togithub.com/evanrittenhouse) in [https://github.com/charliermarsh/ruff/pull/4110](https://togithub.com/charliermarsh/ruff/pull/4110) - Remove `pyright` comment prefix from PYI033 checks by [@​evanrittenhouse](https://togithub.com/evanrittenhouse) in [https://github.com/charliermarsh/ruff/pull/4152](https://togithub.com/charliermarsh/ruff/pull/4152) - Fix F811 false positive with match by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [https://github.com/charliermarsh/ruff/pull/4161](https://togithub.com/charliermarsh/ruff/pull/4161) - Fix `E713` and `E714` false positives for multiple comparisons by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [https://github.com/charliermarsh/ruff/pull/4083](https://togithub.com/charliermarsh/ruff/pull/4083) - Fix B023 shadowed variables in nested functions by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/4111](https://togithub.com/charliermarsh/ruff/pull/4111) - Preserve star-handling special-casing for force-single-line by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4129](https://togithub.com/charliermarsh/ruff/pull/4129) - Respect parent-scoping rules for `NamedExpr` assignments by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4145](https://togithub.com/charliermarsh/ruff/pull/4145) - Fix UP032 auto-fix by [@​JonathanPlasse](https://togithub.com/JonathanPlasse) in [https://github.com/charliermarsh/ruff/pull/4165](https://togithub.com/charliermarsh/ruff/pull/4165) - Allow boolean parameters for `pytest.param` by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4176](https://togithub.com/charliermarsh/ruff/pull/4176) ##### Internal - Replace row/column based `Location` with byte-offsets. by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/3931](https://togithub.com/charliermarsh/ruff/pull/3931) - perf(logical-lines): Various small perf improvements by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/4022](https://togithub.com/charliermarsh/ruff/pull/4022) - Use `memchr` to speedup newline search on x86 by [@​MichaReiser](https://togithub.com/MichaReiser) in [https://github.com/charliermarsh/ruff/pull/3985](https://togithub.com/charliermarsh/ruff/pull/3985) - Remove `ScopeStack` in favor of child-parent `ScopeId` pointers by [@​charliermarsh](https://togithub.com/charliermarsh) in [https://github.com/charliermarsh/ruff/pull/4138](https://togithub.com/charliermarsh/ruff/pull/4138) **Full Changelog**: astral-sh/ruff@v0.0.263...v0.0.264 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Enabled. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/allenporter/pyrainbird). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNS42OS4zIiwidXBkYXRlZEluVmVyIjoiMzUuNjkuMyIsInRhcmdldEJyYW5jaCI6Im1haW4ifQ==--> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Summary
Currently, we maintain a stack of
ScopeId
elements to track the current stack of scopes (module, function, etc.). This makes iteration and scope traversal very easy, but a common operation we perform is "Save the current scope, so we can restore it later" -- and with our current design, that operation becomes unnecessarily expensive, as we need to clone the entire vector ofScopeId
elements in order to save our position in the stack.This PR instead migrates to a model in which we store the parent
ScopeId
on every scope, which in turn allows us to merely store the currentScopeId
when saving and restoring the current scope.We could introduce a crate like
indextree
to help with this, and it may make sense to do so in the future, but for now, I've just implemented this tracking manually, since it's very straightforward.Test Plan
This improves benchmark performance by a couple of percentage points:
I also experimented with a design in which we stored parallel vectors on
Scopes
: a vector ofScope
structs, and a parallel vector of parent pointers (ScopeId
). While I expected this to perform better (SoA vs. AoS), it actually performed worse in practice 🤔