Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented May 2, 2025

Summary

Remove mutability in parameter types for a few functions such as with_self and try_call. I tried the Rc-approach with cheap cloning suggest here first, but it turns out we need a whole stack of prepended arguments (there can be both self and cls), and we would need the same construct not just for CallArguments but also for CallArgumentTypes. At that point we're cloning VecDeques anyway, so the overhead of cloning the whole VecDeque with all arguments didn't seem to justify the additional code complexity.

Benchmarks

I see small performance improvements on this branch?! Maybe because we can run more things in parallel with less mutability?

image

@sharkdp sharkdp added the ty Multi-file analysis & type inference label May 2, 2025
@github-actions
Copy link
Contributor

github-actions bot commented May 2, 2025

mypy_primer results

No ecosystem changes detected ✅

@sharkdp sharkdp marked this pull request as ready for review May 2, 2025 09:46
@sharkdp sharkdp force-pushed the david/call-api-mutability branch from 450a150 to 7ddc065 Compare May 2, 2025 10:12
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.

Nice, this seems cleaner for sure. Definitely worth it if perf is neutral/slightly better.

@AlexWaygood
Copy link
Member

Could also be worth running the knot_benchmark script on larger codebases like black, just to be sure that this doesn't regress performance on those, I suppose?

@sharkdp
Copy link
Contributor Author

sharkdp commented May 2, 2025

Inconclusive (no statistically significant improvement/regression)

Command Mean [ms] Min [ms] Max [ms] Relative
red_knot_main check 183.6 ± 8.2 170.2 197.8 1.01 ± 0.07
red_knot_feature check 182.4 ± 9.4 167.3 204.5 1.00

@sharkdp sharkdp merged commit ea3f4ac into main May 2, 2025
35 checks passed
@sharkdp sharkdp deleted the david/call-api-mutability branch May 2, 2025 11:53
F: FnOnce(&mut Self) -> R,
F: FnOnce(&Self) -> R,
{
let mut call_arguments = self.clone();
Copy link
Member

Choose a reason for hiding this comment

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

We don't need to clone when there's no bound_self. Opened #17793 for that, and to combine the clone/push_front into a single collect call, which should avoid a double-copy when there is a bound_self. (Or really, a copy + immediate move)

dcreager added a commit that referenced this pull request May 2, 2025
* main:
  [red-knot] Refactor: no mutability in call APIs (#17788)
  [red-knot] Fix panic for `tuple[x[y]]` string annotation (#17787)
  [red-knot] Implicit instance attributes in generic methods (#17769)
  doc: Add link to `check-typed-exception` from `S110` and `S112` (#17786)
  Fix module name in ASYNC110, 115, and 116 fixes (#17774)
  [red-knot] More informative hover-types for assignments (#17762)
  [syntax-errors] Use consistent message for bad starred expression usage. (#17772)
  red_knot_server: add auto-completion MVP
  Allow passing a virtual environment to `ruff analyze graph` (#17743)
  Bump 0.11.8 (#17766)
  [`flake8-use-pathlib`] Fix `PTH104`false positive when `rename` is passed a file descriptor (#17712)
dcreager added a commit that referenced this pull request May 2, 2025
…7793)

Quick follow-on to #17788. If there is no bound `self` parameter, we can
reuse the existing `CallArgument{,Type}s`, and we can use a straight
`Vec` instead of a `VecDeque`.
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.

3 participants