Skip to content

Conversation

@MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Feb 20, 2025

Summary

This PR fixes #16172 by using infer_expression_type (which is a salsa query) when evaluating visibility constraints. This should be sufficient to remove any AST reads fromsymbol_from_declarations and symbol_from_bindings.

I did consider making a more local change in own_instance_member but I think that has two downsides:

  • We pay the query cost even if the symbol has no visibility constraints (probably common for instance members?)
  • It doesn't solve the problem that symbol_from_declarations and symbol_from_bindings are still cross-module, and it's very easy to forget to put the call behind a query because it isn't obvious that the method does cross-file analysis.

Performance

The cold benchmark improves by about 1%, the incremental benchmark by 3%

I first considered removing the #[salsa::tracked] from symbol_by_id because it is no longer necessary for cross-module isolation. However, this PR showed that the version where symbol_by_id actually performs better #16279 (comment)

Test Plan

I added a new test for own_instance_member that assert that own_instance_member isn't re-executed if there's a no-op change in the module that declares the Class. All credit for that test goes to @sharkdp because I used his original test as a template and he helped me come up with an example that contains visibility constraints.

I verified that the test fails on main.

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Feb 20, 2025
@MichaReiser
Copy link
Member Author

MichaReiser commented Feb 20, 2025

Ha, this actually performs better and it also makes it harder to hold it wrong. I still haven't been able to construct a test case where own_instance_member unnecessarily recomputes...

@MichaReiser MichaReiser force-pushed the micha/untrack-symbol-by-id branch 2 times, most recently from 68a1dd2 to ad6ef57 Compare February 20, 2025 11:30
@MichaReiser MichaReiser marked this pull request as ready for review February 20, 2025 11:37
@MichaReiser MichaReiser changed the title Untrack symbol_by_id [red-knot] Untrack symbol_by_id Feb 20, 2025
@MichaReiser MichaReiser force-pushed the micha/untrack-symbol-by-id branch from ad6ef57 to a4a3193 Compare February 20, 2025 11:38
self.kind(db).category()
}

pub(crate) fn is_declaration(self, db: &'db dyn Db) -> bool {
Copy link
Member Author

Choose a reason for hiding this comment

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

These accessors are footguns. They make it very easy to introduce dependencies on kind. That's why I removed them.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

I have not reviewed line-by-line in depth but this makes sense to me!


/// This test verifies that queries
#[test]
fn dependency_own_instance_member() -> anyhow::Result<()> {
Copy link
Member

Choose a reason for hiding this comment

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

the amount of setup required here makes me think that we might want to prioritise adding support for incremental tests to mdtest, similar to mypy's test framework (take a look at https://github.com/python/mypy/blob/master/test-data/unit/check-incremental.test)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, although copy pasting is fine, considering how few we have but it could be something to consider for a more scalable approach on how to avoid cross-file queries.

Copy link
Member

Choose a reason for hiding this comment

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

yeah, it would only be worth it if we consider that it would be useful to have a lot more tests like this, and we want to make it easier to write those tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

There was already a design for this in the original mdtest design (and it's in the README as a planned improvement), we just haven't implemented yet. (And the design probably needs to change a bit to avoid using non-rendering tag info strings on code blocks.)

@MichaReiser MichaReiser changed the title [red-knot] Untrack symbol_by_id [red-knot] Remove cross-module query dependencies from own_instance_member Feb 20, 2025
@MichaReiser MichaReiser changed the title [red-knot] Remove cross-module query dependencies from own_instance_member [red-knot] Prevent cross-module query dependencies in own_instance_member Feb 20, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 20, 2025

CodSpeed Performance Report

Merging #16268 will improve performances by 5.46%

Comparing micha/untrack-symbol-by-id (c06af23) with main (b385c7d)

Summary

⚡ 1 improvements
✅ 31 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
red_knot_check_file[incremental] 5.7 ms 5.4 ms +5.46%

@MichaReiser
Copy link
Member Author

Okay, keeping the symbol_by_id is by far the best for incremental. A solid 5% perf improvement.

@MichaReiser
Copy link
Member Author

@carljm said he liked it overall. So I'll go ahead and merge it. We may want to keep iterating on how we want to enforce cross-module boundaries. E.g. is it okay that own_instance_members reads a symbol table from another module?

@MichaReiser MichaReiser merged commit 470f852 into main Feb 20, 2025
21 checks passed
@MichaReiser MichaReiser deleted the micha/untrack-symbol-by-id branch February 20, 2025 17:46
dcreager added a commit that referenced this pull request Feb 20, 2025
…raint-copies

* origin/main:
  [red-knot] Prevent cross-module query dependencies in `own_instance_member` (#16268)
  Specify the `wasm-pack` version for release workflows (#16278)
  [red-knot] Separate `definitions_by_definition` into separate fields (#16277)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[red-knot] accessing an instance member can cause cross-module AST dependency

4 participants