From f959987ddeebc3e1105ffb00b843efe50d5f764d Mon Sep 17 00:00:00 2001 From: togami <62130798+togami2864@users.noreply.github.com> Date: Tue, 1 Oct 2024 21:39:47 +0900 Subject: [PATCH] perf(linter): remove unneeded memory allocation for str (#4142) --- .../no_shorthand_property_overrides.rs | 16 +++++++--------- crates/biome_css_analyze/src/utils.rs | 8 ++++---- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/crates/biome_css_analyze/src/lint/suspicious/no_shorthand_property_overrides.rs b/crates/biome_css_analyze/src/lint/suspicious/no_shorthand_property_overrides.rs index 8e0987022551..b4c0d4252b77 100644 --- a/crates/biome_css_analyze/src/lint/suspicious/no_shorthand_property_overrides.rs +++ b/crates/biome_css_analyze/src/lint/suspicious/no_shorthand_property_overrides.rs @@ -7,12 +7,12 @@ use biome_console::markup; use biome_css_syntax::{AnyCssDeclarationName, CssGenericProperty, CssLanguage, CssSyntaxKind}; use biome_rowan::{AstNode, Language, SyntaxNode, TextRange, WalkEvent}; -fn remove_vendor_prefix(prop: &str, prefix: &str) -> String { +fn remove_vendor_prefix<'a>(prop: &'a str, prefix: &'a str) -> &'a str { if let Some(prop) = prop.strip_prefix(prefix) { - return prop.to_string(); + return prop; } - prop.to_string() + prop } fn get_override_props(property: &str) -> Vec<&str> { @@ -109,18 +109,16 @@ impl Visitor for NoDeclarationBlockShorthandPropertyOverridesVisitor { let prop_lowercase = prop.to_lowercase(); let prop_prefix = vender_prefix(&prop_lowercase); - let unprefixed_prop = remove_vendor_prefix(&prop_lowercase, &prop_prefix); - let override_props = get_override_props(&unprefixed_prop); + let unprefixed_prop = remove_vendor_prefix(&prop_lowercase, prop_prefix); + let override_props = get_override_props(unprefixed_prop); self.prior_props_in_block.iter().for_each(|prior_prop| { let prior_prop_prefix = vender_prefix(&prior_prop.lowercase); let unprefixed_prior_prop = - remove_vendor_prefix(&prior_prop.lowercase, &prior_prop_prefix); + remove_vendor_prefix(&prior_prop.lowercase, prior_prop_prefix); if prop_prefix == prior_prop_prefix - && override_props - .binary_search(&unprefixed_prior_prop.as_str()) - .is_ok() + && override_props.binary_search(&unprefixed_prior_prop).is_ok() { ctx.match_query( NoDeclarationBlockShorthandPropertyOverridesQuery { diff --git a/crates/biome_css_analyze/src/utils.rs b/crates/biome_css_analyze/src/utils.rs index 9c73f0a94541..ecf9ab0ec0bb 100644 --- a/crates/biome_css_analyze/src/utils.rs +++ b/crates/biome_css_analyze/src/utils.rs @@ -122,13 +122,13 @@ pub fn is_custom_function(value: &str) -> bool { } // Returns the vendor prefix extracted from an input string. -pub fn vender_prefix(prop: &str) -> String { +pub fn vender_prefix(prop: &str) -> &str { for prefix in VENDOR_PREFIXES.iter() { if prop.starts_with(prefix) { - return (*prefix).to_string(); + return prefix; } } - String::new() + "" } pub fn is_pseudo_elements(prop: &str) -> bool { @@ -138,7 +138,7 @@ pub fn is_pseudo_elements(prop: &str) -> bool { || OTHER_PSEUDO_ELEMENTS.contains(&prop) } -/// Check if the input string is custom selector +/// Check if the input string is custom selector /// See https://drafts.csswg.org/css-extensions/#custom-selectors for more details pub fn is_custom_selector(prop: &str) -> bool { prop.starts_with("--")