Skip to content
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

[red-knot] unify LoopState and saved_break_states #16406

Merged
merged 2 commits into from
Feb 26, 2025
Merged

[red-knot] unify LoopState and saved_break_states #16406

merged 2 commits into from
Feb 26, 2025

Conversation

carljm
Copy link
Contributor

@carljm carljm commented Feb 26, 2025

We currently keep two separate pieces of state regarding the current loop on SemanticIndexBuilder. One is an enum simply reflecting whether we are currently inside a loop, and the other is the saved flow states for break statements found in the current loop.

For adding loopy control flow, I'll need to add some additional loop state (continue states, for example). Prepare for this by consolidating our existing loop state into a single struct and simplifying the API for pushing and popping a loop.

This is purely a refactor, so tests are not changed.

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@carljm carljm enabled auto-merge (squash) February 26, 2025 22:27
@carljm carljm merged commit fb778ee into main Feb 26, 2025
20 checks passed
@carljm carljm deleted the cjm/loopstate branch February 26, 2025 22:31
dcreager added a commit that referenced this pull request Feb 27, 2025
* main:
  [red-knot] unify LoopState and saved_break_states (#16406)
  [`pylint`] Also reports `case np.nan`/`case math.nan` (`PLW0177`) (#16378)
  [FURB156] Do not consider docstring(s) (#16391)
  Use `is_none_or` in `stdlib-module-shadowing` (#16402)
  [red-knot] Upgrade salsa to include `AtomicPtr` perf improvement (#16398)
  [red-knot] Fix file watching for new non-project files (#16395)
  document MSRV policy (#16384)
  [red-knot] fix non-callable reporting for unions (#16387)
  bump MSRV to 1.83 (#16294)
  Avoid unnecessary info at non-trace server log level (#16389)
  Expand `ruff.configuration` to allow inline config (#16296)
  Start detecting version-related syntax errors in the parser (#16090)
dcreager added a commit that referenced this pull request Feb 27, 2025
* dcreager/dont-have-a-cow:
  [red-knot] unify LoopState and saved_break_states (#16406)
  [`pylint`] Also reports `case np.nan`/`case math.nan` (`PLW0177`) (#16378)
  [FURB156] Do not consider docstring(s) (#16391)
  Use `is_none_or` in `stdlib-module-shadowing` (#16402)
  [red-knot] Upgrade salsa to include `AtomicPtr` perf improvement (#16398)
  [red-knot] Fix file watching for new non-project files (#16395)
  document MSRV policy (#16384)
  [red-knot] fix non-callable reporting for unions (#16387)
  bump MSRV to 1.83 (#16294)
  Avoid unnecessary info at non-trace server log level (#16389)
  Expand `ruff.configuration` to allow inline config (#16296)
  Start detecting version-related syntax errors in the parser (#16090)
#[derive(Clone, Debug, Default)]
struct Loop {
/// Flow states at each `break` in the current loop.
break_states: Vec<FlowSnapshot>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked through all usages but I'm wondering if we could use a single break_states struct stored on SemanticIndexBuilder instead of allocating a Vec for each Loop.

What I'm thinking about:

  • Loop: stores the length of the "arena" at the time this loop was pushed
  • Getting all break states for the current loop is the same as arena[start..]
  • When popping a loop, truncate the arena to start to remove all snapshots belonging to the current state.

Copy link
Contributor Author

@carljm carljm Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had originally thought I might invest more time in reducing vec allocations here, but the simple version was completely neutral on benchmarks, so I'm not sure it's worth putting more time into this right now. I think perf optimization work should be motivated by some evidence.

I suspect that in practice loops are relatively uncommon, and loops with break in them even more uncommon, so in the great majority of cases we never allocate for this vec at all. I think if we want to improve performance of semantic indexing, there will be much higher-ROI things to look at.

dcreager added a commit that referenced this pull request Feb 27, 2025
* main:
  [red-knot] unify LoopState and saved_break_states (#16406)
  [`pylint`] Also reports `case np.nan`/`case math.nan` (`PLW0177`) (#16378)
  [FURB156] Do not consider docstring(s) (#16391)
  Use `is_none_or` in `stdlib-module-shadowing` (#16402)
  [red-knot] Upgrade salsa to include `AtomicPtr` perf improvement (#16398)
  [red-knot] Fix file watching for new non-project files (#16395)
  document MSRV policy (#16384)
  [red-knot] fix non-callable reporting for unions (#16387)
  bump MSRV to 1.83 (#16294)
  Avoid unnecessary info at non-trace server log level (#16389)
  Expand `ruff.configuration` to allow inline config (#16296)
  Start detecting version-related syntax errors in the parser (#16090)
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.

3 participants