From 763d38cafbc7d198f5fdd5b6d9046de2c28afbed Mon Sep 17 00:00:00 2001 From: konstin Date: Sun, 18 Jun 2023 12:39:06 +0200 Subject: [PATCH] Refactor top llvm-lines entry (#5147) ## Summary This refactors the top entry in terms of llvm lines, `RuleCodePrefix::iter()`. It's only used for generating the schema and the clap completion so no effect on performance. I've confirmed with ``` CARGO_TARGET_DIR=target-llvm-lines RUSTFLAGS="-Csymbol-mangling-version=v0" cargo llvm-lines -p ruff --lib | head -n 20 ``` that this indeed remove the method from the list of heaviest symbols in terms of llvm-lines Before: ``` Lines Copies Function name ----- ------ ------------- 1768469 40538 (TOTAL) 10391 (0.6%, 0.6%) 1 (0.0%, 0.0%) ::iter 8250 (0.5%, 1.1%) 1 (0.0%, 0.0%) ::noqa_code 7427 (0.4%, 1.5%) 1 (0.0%, 0.0%) ::visit_stmt 6536 (0.4%, 1.8%) 1 (0.0%, 0.0%) <::deserialize::__Visitor as serde[1a28808d63625aed]::de::Visitor>::visit_map::> 6536 (0.4%, 2.2%) 1 (0.0%, 0.0%) <::deserialize::__Visitor as serde[1a28808d63625aed]::de::Visitor>::visit_map:: 6533 (0.4%, 2.6%) 1 (0.0%, 0.0%) <::deserialize::__Visitor as serde[1a28808d63625aed]::de::Visitor>::visit_map:: 5727 (0.3%, 2.9%) 1 (0.0%, 0.0%) ::visit_expr 4453 (0.3%, 3.2%) 1 (0.0%, 0.0%) ruff[fa0f2e8ef07114da]::flake8_to_ruff::converter::convert 3790 (0.2%, 3.4%) 1 (0.0%, 0.0%) <&ruff[fa0f2e8ef07114da]::registry::Linter as core[da82827a87f140f9]::iter::traits::collect::IntoIterator>::into_iter 3416 (0.2%, 3.6%) 1 (0.0%, 0.0%) ::code_for_rule 3187 (0.2%, 3.7%) 1 (0.0%, 0.0%) ::fmt 3185 (0.2%, 3.9%) 1 (0.0%, 0.0%) <&str as core[da82827a87f140f9]::convert::From<&ruff[fa0f2e8ef07114da]::codes::Rule>>::from 3185 (0.2%, 4.1%) 1 (0.0%, 0.0%) <&str as core[da82827a87f140f9]::convert::From>::from 3185 (0.2%, 4.3%) 1 (0.0%, 0.0%) >::as_ref 3183 (0.2%, 4.5%) 1 (0.0%, 0.0%) ::get 2718 (0.2%, 4.6%) 1 (0.0%, 0.0%) <::deserialize::__Visitor as serde[1a28808d63625aed]::de::Visitor>::visit_seq:: 2706 (0.2%, 4.8%) 1 (0.0%, 0.0%) <&ruff[fa0f2e8ef07114da]::codes::Pylint as core[da82827a87f140f9]::iter::traits::collect::IntoIterator>::into_iter ``` After: ``` Lines Copies Function name ----- ------ ------------- 1763380 40806 (TOTAL) 8250 (0.5%, 0.5%) 1 (0.0%, 0.0%) ::noqa_code 7427 (0.4%, 0.9%) 1 (0.0%, 0.0%) ::visit_stmt 6536 (0.4%, 1.3%) 1 (0.0%, 0.0%) <::deserialize::__Visitor as serde[1a28808d63625aed]::de::Visitor>::visit_map::> 6536 (0.4%, 1.6%) 1 (0.0%, 0.0%) <::deserialize::__Visitor as serde[1a28808d63625aed]::de::Visitor>::visit_map:: 6533 (0.4%, 2.0%) 1 (0.0%, 0.0%) <::deserialize::__Visitor as serde[1a28808d63625aed]::de::Visitor>::visit_map:: 5727 (0.3%, 2.3%) 1 (0.0%, 0.0%) ::visit_expr 4453 (0.3%, 2.6%) 1 (0.0%, 0.0%) ruff[fa0f2e8ef07114da]::flake8_to_ruff::converter::convert 3790 (0.2%, 2.8%) 1 (0.0%, 0.0%) <&ruff[fa0f2e8ef07114da]::registry::Linter as core[da82827a87f140f9]::iter::traits::collect::IntoIterator>::into_iter 3416 (0.2%, 3.0%) 1 (0.0%, 0.0%) ::code_for_rule 3187 (0.2%, 3.2%) 1 (0.0%, 0.0%) ::fmt 3185 (0.2%, 3.3%) 1 (0.0%, 0.0%) <&str as core[da82827a87f140f9]::convert::From<&ruff[fa0f2e8ef07114da]::codes::Rule>>::from 3185 (0.2%, 3.5%) 1 (0.0%, 0.0%) <&str as core[da82827a87f140f9]::convert::From>::from 3185 (0.2%, 3.7%) 1 (0.0%, 0.0%) >::as_ref 3183 (0.2%, 3.9%) 1 (0.0%, 0.0%) ::get 2718 (0.2%, 4.0%) 1 (0.0%, 0.0%) <::deserialize::__Visitor as serde[1a28808d63625aed]::de::Visitor>::visit_seq:: 2706 (0.2%, 4.2%) 1 (0.0%, 0.0%) <&ruff[fa0f2e8ef07114da]::codes::Pylint as core[da82827a87f140f9]::iter::traits::collect::IntoIterator>::into_iter 2573 (0.1%, 4.3%) 1 (0.0%, 0.0%) <::deserialize::__Visitor as serde[1a28808d63625aed]::de::Visitor>::visit_map::> ``` I didn't measure the effect on binary size this time. ## Testing `cargo test` which uses this to generate the schema didn't change --- crates/ruff_macros/src/map_codes.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/ruff_macros/src/map_codes.rs b/crates/ruff_macros/src/map_codes.rs index 327b9a9d3d..950c66f7c8 100644 --- a/crates/ruff_macros/src/map_codes.rs +++ b/crates/ruff_macros/src/map_codes.rs @@ -178,8 +178,7 @@ pub(crate) fn map_codes(func: &ItemFn) -> syn::Result { let rule_to_code = generate_rule_to_code(&linter_to_rules); output.extend(rule_to_code); - let iter = generate_iter_impl(&linter_to_rules, &all_codes); - output.extend(iter); + output.extend(generate_iter_impl(&linter_to_rules, &linter_idents)); Ok(output) } @@ -326,7 +325,7 @@ See also https://github.com/astral-sh/ruff/issues/2186. /// Implement `impl IntoIterator for &Linter` and `RuleCodePrefix::iter()` fn generate_iter_impl( linter_to_rules: &BTreeMap>, - all_codes: &[TokenStream], + linter_idents: &[&Ident], ) -> TokenStream { let mut linter_into_iter_match_arms = quote!(); for (linter, map) in linter_to_rules { @@ -352,8 +351,11 @@ fn generate_iter_impl( } impl RuleCodePrefix { - pub fn iter() -> ::std::vec::IntoIter { - vec![ #(#all_codes,)* ].into_iter() + pub fn iter() -> impl Iterator { + use strum::IntoEnumIterator; + + std::iter::empty() + #(.chain(#linter_idents::iter().map(|x| Self::#linter_idents(x))))* } } }