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] Reuse alist cells when possible #16408

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

dcreager
Copy link
Member

@dcreager dcreager commented Feb 27, 2025

This is in support of #16311, and adds some additional tracking to the alist implementation so that we can tell when a cell is only used once. This lets us reuse and modify those cells in place when doing certain updates.

This also adds some property tests for alists, since I wasn't convinced that my small set of hand-coded examples were exercising all of the code paths.

I'm leaving this as draft for now, but want to get some CI and Codspeed feedback on it.

@dcreager dcreager added internal An internal refactor or improvement performance Potential performance improvement red-knot Multi-file analysis & type inference labels Feb 27, 2025
Comment on lines +80 to +81
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct List<K, V = ()> {
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a major part of this PR, and I'm not sure whether I like all of the ramifications.

The gist is that we want to track whether a particular list cell is aliased (used more than once). To do that, we need to wrap the IndexVec IDs in this type that is not Clone or Copy. You must invoke the clone_list method below to make a copy of a list. It's still cheap — copying a u32 and setting a bool — but this lets us update the mutator methods below to take in owned List handles in cases where we want to try to reuse unaliased cells.

I like that this gives us type safety for tracking aliasing information that enables this optimization. I don't love that it has knock-on effects through the rest of the code...

Comment on lines -509 to 510
#[derive(Clone, Debug)]
#[derive(Debug)]
pub(super) struct FlowSnapshot {
Copy link
Member Author

Choose a reason for hiding this comment

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

...like here, where FlowSnapshot is no longer Clone...

pub(super) struct FlowSnapshot {
symbol_states: IndexVec<ScopedSymbolId, SymbolState>,
scope_start_visibility: ScopedVisibilityConstraintId,
}

impl FlowSnapshot {
pub(super) fn clone(&self, narrowing_constraints: &mut NarrowingConstraintsBuilder) -> Self {
Copy link
Member Author

Choose a reason for hiding this comment

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

...at least not without threading extra state all of over the place!

* 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)
Copy link
Contributor

github-actions bot commented Feb 27, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement performance Potential performance improvement red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant