-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[red-knot] Reuse alist cells when possible #16408
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
Closed
Closed
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ef99212
Use named fields for list cells
dcreager 10fa0b9
Require explicit clones of alists
dcreager 2df613b
Add property tests
dcreager 42bcb82
Don't clone any cells until we know we need to stitch up
dcreager 6b3741b
Add structural sharing property tests
dcreager 5c501c8
Create structural property tests for union/intersection
dcreager fc57815
Tracked aliased cells and update them in-place on insert
dcreager 203b91e
Reuse unaliased cells in intersection
dcreager 5205e77
Reuse unaliased cells in union
dcreager 35c413b
Merge branch 'main' into dcreager/dont-have-a-cow
dcreager 6b08e62
clippy
dcreager af46086
clippy
dcreager fa1e5c1
cargo shear
dcreager 321d1e3
Add mutable list handle and `for_each`
dcreager 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -441,7 +441,7 @@ impl<'map, 'db> Iterator for BindingWithConstraintsIterator<'map, 'db> { | |
narrowing_constraint: ConstraintsIterator { | ||
predicates, | ||
constraint_ids: narrowing_constraints | ||
.iter_predicates(live_binding.narrowing_constraint), | ||
.iter_predicates(&live_binding.narrowing_constraint), | ||
}, | ||
visibility_constraint: live_binding.visibility_constraint, | ||
}) | ||
|
@@ -506,12 +506,25 @@ impl<'db> Iterator for DeclarationsIterator<'_, 'db> { | |
impl std::iter::FusedIterator for DeclarationsIterator<'_, '_> {} | ||
|
||
/// A snapshot of the definitions and constraints state at a particular point in control flow. | ||
#[derive(Clone, Debug)] | ||
#[derive(Debug)] | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...at least not without threading extra state all of over the place! |
||
Self { | ||
symbol_states: self | ||
.symbol_states | ||
.iter() | ||
.map(|state| state.clone(narrowing_constraints)) | ||
.collect(), | ||
scope_start_visibility: self.scope_start_visibility, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(super) struct UseDefMapBuilder<'db> { | ||
/// Append-only array of [`Definition`]. | ||
|
@@ -655,8 +668,12 @@ impl<'db> UseDefMapBuilder<'db> { | |
) { | ||
let def_id = self.all_definitions.push(Some(declaration)); | ||
let symbol_state = &mut self.symbol_states[symbol]; | ||
self.bindings_by_declaration | ||
.insert(declaration, symbol_state.bindings().clone()); | ||
self.bindings_by_declaration.insert( | ||
declaration, | ||
symbol_state | ||
.bindings() | ||
.clone(&mut self.narrowing_constraints), | ||
); | ||
symbol_state.record_declaration(def_id); | ||
} | ||
|
||
|
@@ -676,24 +693,33 @@ impl<'db> UseDefMapBuilder<'db> { | |
pub(super) fn record_use(&mut self, symbol: ScopedSymbolId, use_id: ScopedUseId) { | ||
// We have a use of a symbol; clone the current bindings for that symbol, and record them | ||
// as the live bindings for this use. | ||
let new_use = self | ||
.bindings_by_use | ||
.push(self.symbol_states[symbol].bindings().clone()); | ||
let new_use = self.bindings_by_use.push( | ||
self.symbol_states[symbol] | ||
.bindings() | ||
.clone(&mut self.narrowing_constraints), | ||
); | ||
debug_assert_eq!(use_id, new_use); | ||
} | ||
|
||
pub(super) fn snapshot_eager_bindings( | ||
&mut self, | ||
enclosing_symbol: ScopedSymbolId, | ||
) -> ScopedEagerBindingsId { | ||
self.eager_bindings | ||
.push(self.symbol_states[enclosing_symbol].bindings().clone()) | ||
self.eager_bindings.push( | ||
self.symbol_states[enclosing_symbol] | ||
.bindings() | ||
.clone(&mut self.narrowing_constraints), | ||
) | ||
} | ||
|
||
/// Take a snapshot of the current visible-symbols state. | ||
pub(super) fn snapshot(&self) -> FlowSnapshot { | ||
pub(super) fn snapshot(&mut self) -> FlowSnapshot { | ||
FlowSnapshot { | ||
symbol_states: self.symbol_states.clone(), | ||
symbol_states: self | ||
.symbol_states | ||
.iter() | ||
.map(|state| state.clone(&mut self.narrowing_constraints)) | ||
.collect(), | ||
scope_start_visibility: self.scope_start_visibility, | ||
} | ||
} | ||
|
@@ -713,10 +739,9 @@ impl<'db> UseDefMapBuilder<'db> { | |
// If the snapshot we are restoring is missing some symbols we've recorded since, we need | ||
// to fill them in so the symbol IDs continue to line up. Since they don't exist in the | ||
// snapshot, the correct state to fill them in with is "undefined". | ||
self.symbol_states.resize( | ||
num_symbols, | ||
SymbolState::undefined(self.scope_start_visibility), | ||
); | ||
self.symbol_states.resize_with(num_symbols, || { | ||
SymbolState::undefined(self.scope_start_visibility) | ||
}); | ||
} | ||
|
||
/// Merge the given snapshot into the current state, reflecting that we might have taken either | ||
|
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.
...like here, where
FlowSnapshot
is no longerClone
...