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

Use a trie-based router to resolve per-file settings #9444

Closed
wants to merge 5 commits into from

Conversation

charliermarsh
Copy link
Member

Summary

This is a small change that I put together a while ago but forgot to clean up. When the formatter is fully cached, it turns out we actually spend meaningful time mapping from file to Settings (since we use a hierarchical approach to settings). Using matchit rather than BTreeMap improves fully-cached performance by anywhere from 2-5% depending on the project, and since these are all implementation details of Resolver, it's minimally invasive.

We don't need all the behavior of matchit, but I tried other libraries like radix_trie and didn't see any speedup vs. main, whereas matchit shows a consistent improvement.

Test Plan

❯ hyperfine --warmup 20 -i "./target/release/main format ../airflow" "./target/release/ruff format ../airflow"
Benchmark 1: ./target/release/main format ../airflow
  Time (mean ± σ):      73.4 ms ±   1.2 ms    [User: 48.8 ms, System: 76.4 ms]
  Range (min … max):    68.4 ms …  78.2 ms    39 runs

Benchmark 2: ./target/release/ruff format ../airflow
  Time (mean ± σ):      69.7 ms ±   0.9 ms    [User: 41.8 ms, System: 75.7 ms]
  Range (min … max):    68.5 ms …  73.5 ms    42 runs

Summary
  './target/release/ruff format ../airflow' ran
    1.05 ± 0.02 times faster than './target/release/main format ../airflow'

@charliermarsh charliermarsh added the performance Potential performance improvement label Jan 9, 2024
@charliermarsh charliermarsh marked this pull request as draft January 9, 2024 05:35
@charliermarsh
Copy link
Member Author

Okay, need to fix Windows -- draft for now.

@charliermarsh
Copy link
Member Author

I'm realizing that this likely won't work for paths that contain characters that can be confused for routing characters in matchit (like colons).

ibraheemdev added a commit that referenced this pull request Apr 24, 2024
## Summary

Continuation of #9444.

> When the formatter is fully cached, it turns out we actually spend
meaningful time mapping from file to `Settings` (since we use a
hierarchical approach to settings). Using `matchit` rather than
`BTreeMap` improves fully-cached performance by anywhere from 2-5%
depending on the project, and since these are all implementation details
of `Resolver`, it's minimally invasive.

`matchit` supports escaping routing characters so this change should now
be fully compatible.

## Test Plan

On my machine I'm seeing a ~3% improvement with this change.

```
hyperfine --warmup 20 -i "./target/release/main format ../airflow" "./target/release/ruff format ../airflow"
Benchmark 1: ./target/release/main format ../airflow
  Time (mean ± σ):      58.1 ms ±   1.4 ms    [User: 63.1 ms, System: 66.5 ms]
  Range (min … max):    56.1 ms …  62.9 ms    49 runs
 
Benchmark 2: ./target/release/ruff format ../airflow
  Time (mean ± σ):      56.6 ms ±   1.5 ms    [User: 57.8 ms, System: 67.7 ms]
  Range (min … max):    54.1 ms …  63.0 ms    51 runs
 
Summary
  ./target/release/ruff format ../airflow ran
    1.03 ± 0.04 times faster than ./target/release/main format ../airflow
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Potential performance improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant