From 779dba763fd28fc8695d2761f09455adf373d02a Mon Sep 17 00:00:00 2001 From: Simon Brugman Date: Sun, 16 Jul 2023 10:20:25 +0200 Subject: [PATCH 1/3] try not consuming the entire iterator --- CONTRIBUTING.md | 7 +++++++ crates/ruff/src/rules/pycodestyle/helpers.rs | 21 +++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ae047347e008c6..dfda88dfb4aa5a 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -419,6 +419,13 @@ Summary 159.43 ± 2.48 times faster than 'pycodestyle crates/ruff/resources/test/cpython' ``` +To benchmark a subset of rules, e.g. `LineTooLong` and `DocLineTooLong`: + +```shell +cargo build --release && hyperfine --warmup 10 \ + "./target/release/ruff ./crates/ruff/resources/test/cpython/ --no-cache -e --select W505,E501" +``` + You can run `poetry install` from `./scripts/benchmarks` to create a working environment for the above. All reported benchmarks were computed using the versions specified by `./scripts/benchmarks/pyproject.toml` on Python 3.11. diff --git a/crates/ruff/src/rules/pycodestyle/helpers.rs b/crates/ruff/src/rules/pycodestyle/helpers.rs index f99524e74312af..c3976a81853a72 100644 --- a/crates/ruff/src/rules/pycodestyle/helpers.rs +++ b/crates/ruff/src/rules/pycodestyle/helpers.rs @@ -53,16 +53,8 @@ pub(super) fn is_overlong( task_tags: &[String], tab_size: TabSize, ) -> Option { - let mut start_offset = line.start(); let mut width = LineWidth::new(tab_size); - - for c in line.chars() { - if width < limit { - start_offset += c.text_len(); - } - width = width.add_char(c); - } - + width = width.add_str(line.as_str()); if width <= limit { return None; } @@ -91,6 +83,17 @@ pub(super) fn is_overlong( } } + // Obtain the start offset of the part of te line that exceeds the limit + let mut start_offset = line.start(); + let mut start_width = LineWidth::new(tab_size); + for c in line.chars() { + if start_width < limit { + start_offset += c.text_len(); + start_width = start_width.add_char(c); + } else { + break; + } + } Some(Overlong { range: TextRange::new(start_offset, line.end()), width: width.get(), From 4e3b9dbbde7f9d0d140d0da6372bb1abbc6a706a Mon Sep 17 00:00:00 2001 From: Simon Brugman Date: Sun, 16 Jul 2023 21:16:11 +0200 Subject: [PATCH 2/3] Update crates/ruff/src/rules/pycodestyle/helpers.rs Co-authored-by: konsti --- crates/ruff/src/rules/pycodestyle/helpers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ruff/src/rules/pycodestyle/helpers.rs b/crates/ruff/src/rules/pycodestyle/helpers.rs index c3976a81853a72..f671bb0af5eb4c 100644 --- a/crates/ruff/src/rules/pycodestyle/helpers.rs +++ b/crates/ruff/src/rules/pycodestyle/helpers.rs @@ -83,7 +83,7 @@ pub(super) fn is_overlong( } } - // Obtain the start offset of the part of te line that exceeds the limit + // Obtain the start offset of the part of the line that exceeds the limit let mut start_offset = line.start(); let mut start_width = LineWidth::new(tab_size); for c in line.chars() { From 9c129c138958cdea1fad6f953ea5ac1a75105469 Mon Sep 17 00:00:00 2001 From: Simon Brugman Date: Sun, 16 Jul 2023 22:10:36 +0200 Subject: [PATCH 3/3] check byte length --- crates/ruff/src/rules/pycodestyle/helpers.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/ruff/src/rules/pycodestyle/helpers.rs b/crates/ruff/src/rules/pycodestyle/helpers.rs index f671bb0af5eb4c..42781c1cd93e92 100644 --- a/crates/ruff/src/rules/pycodestyle/helpers.rs +++ b/crates/ruff/src/rules/pycodestyle/helpers.rs @@ -53,6 +53,11 @@ pub(super) fn is_overlong( task_tags: &[String], tab_size: TabSize, ) -> Option { + // Each character is between 1-4 bytes. If the number of bytes is smaller than the limit, it cannot be overlong. + if line.len() < limit.get() { + return None; + } + let mut width = LineWidth::new(tab_size); width = width.add_str(line.as_str()); if width <= limit {