ruff/crates
Charlie Marsh 8c8988ea40
Improve performance of commented-out-code (~50-80%) (#7706)
## Summary

This PR implements a variety of optimizations to improve performance of
the Eradicate rule, which always shows up in all-rules benchmarks and
bothers me. (These improvements are not hugely important, but it was
kind of a fun Friday thing to spent a bit of time on.)

The improvements include:

- Doing cheaper work first (checking for some explicit substrings
upfront).
- Using `aho-corasick` to speed an exact substring search.
- Merging multiple regular expressions using a `RegexSet`.
- Removing some unnecessary `\s*` and other pieces from the regular
expressions (since we already trim strings before matching on them).

## Test Plan

I benchmarked this function in a standalone crate using a variety of
cases. Criterion reports that this version is up to 80% faster, and
almost every case is at least 50% faster:

```
Eradicate/Detection/# Warn if we are installing over top of an existing installation. This can
                        time:   [101.84 ns 102.32 ns 102.82 ns]
                        change: [-77.166% -77.062% -76.943%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high mild
Eradicate/Detection/#from foo import eradicate
                        time:   [74.872 ns 75.096 ns 75.314 ns]
                        change: [-84.180% -84.131% -84.079%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild
Eradicate/Detection/# encoding: utf8
                        time:   [46.522 ns 46.862 ns 47.237 ns]
                        change: [-29.408% -28.918% -28.471%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  6 (6.00%) high mild
  1 (1.00%) high severe
Eradicate/Detection/# Issue #999
                        time:   [16.942 ns 16.994 ns 17.058 ns]
                        change: [-57.243% -57.064% -56.815%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 3 outliers among 100 measurements (3.00%)
  2 (2.00%) high mild
  1 (1.00%) high severe
Eradicate/Detection/# type: ignore
                        time:   [43.074 ns 43.163 ns 43.262 ns]
                        change: [-17.614% -17.390% -17.152%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe
Eradicate/Detection/# user_content_type, _ = TimelineEvent.objects.using(db_alias).get_or_create(
                        time:   [209.40 ns 209.81 ns 210.23 ns]
                        change: [-32.806% -32.630% -32.470%] (p = 0.00 < 0.05)
                        Performance has improved.
Eradicate/Detection/# this is = to that :(
                        time:   [72.659 ns 73.068 ns 73.473 ns]
                        change: [-68.884% -68.775% -68.655%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  7 (7.00%) high mild
  2 (2.00%) high severe
Eradicate/Detection/#except Exception:
                        time:   [92.063 ns 92.366 ns 92.691 ns]
                        change: [-64.204% -64.052% -63.909%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 4 outliers among 100 measurements (4.00%)
  2 (2.00%) high mild
  2 (2.00%) high severe
Eradicate/Detection/#print(1)
                        time:   [68.359 ns 68.537 ns 68.725 ns]
                        change: [-72.424% -72.356% -72.278%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) low mild
  1 (1.00%) high mild
Eradicate/Detection/#'key': 1 + 1,
                        time:   [79.604 ns 79.865 ns 80.135 ns]
                        change: [-69.787% -69.667% -69.549%] (p = 0.00 < 0.05)
                        Performance has improved.
```
2023-09-29 20:13:12 +00:00
..
flake8_to_ruff Add explicit-preview-rules to toggle explicit selection of preview rules (#7390) 2023-09-28 15:00:33 -05:00
ruff_benchmark Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_cache Introduce FormatterSettings (#7545) 2023-09-21 08:01:24 +02:00
ruff_cli Remove unused black compatibility tests (#7712) 2023-09-29 10:31:54 -05:00
ruff_dev Rename Autofix to Fix (#7657) 2023-09-28 10:53:05 +00:00
ruff_diagnostics Rename Autofix to Fix (#7657) 2023-09-28 10:53:05 +00:00
ruff_formatter Add most formatter options to ruff.toml / pyproject.toml (#7566) 2023-09-22 15:47:57 +00:00
ruff_index Add unreachable code rule (#5384) 2023-07-04 14:27:23 +00:00
ruff_linter Improve performance of commented-out-code (~50-80%) (#7706) 2023-09-29 20:13:12 +00:00
ruff_macros Rename Autofix to Fix (#7657) 2023-09-28 10:53:05 +00:00
ruff_notebook Rename Autofix to Fix (#7657) 2023-09-28 10:53:05 +00:00
ruff_python_ast Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_python_codegen Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_python_formatter Use Expr::is_* methods in more matches (#7714) 2023-09-29 17:28:50 +00:00
ruff_python_index Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_python_literal Implement our own small-integer optimization (#7584) 2023-09-25 15:13:21 +00:00
ruff_python_parser Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_python_resolver Replace .map_or(false, $closure) with .is_some_and(closure) (#6244) 2023-08-01 19:29:42 +02:00
ruff_python_semantic Track fix isolation in unnecessary-pass (#7715) 2023-09-29 17:23:04 +00:00
ruff_python_stdlib Use u8 to represent version segments (#7578) 2023-09-21 14:24:51 -04:00
ruff_python_trivia Extend pragma comment cases (#7687) 2023-09-28 18:55:19 +00:00
ruff_shrinking Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_source_file Skip BOM when inserting start-of-file imports (#7622) 2023-09-23 19:36:50 +00:00
ruff_text_size Unify line size settings between ruff and the formatter (#6873) 2023-08-28 06:44:56 +00:00
ruff_wasm Add support for PEP 701 (#7376) 2023-09-29 02:55:39 +00:00
ruff_workspace Add explicit-preview-rules to toggle explicit selection of preview rules (#7390) 2023-09-28 15:00:33 -05:00