From bd8a9035481ff2c349a36471c8c7c3533c3385c3 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Sat, 9 Jan 2021 21:36:38 -0500 Subject: Remove unnecessary allocation The case-insensitive prefix/suffix check can be performed character-by-character. This allows the check to be done without having to allocate a new string. As a side effect, it's also no longer necessary to convert the entire string to lowercase, as it's done as needed. As the only case equality we're handling is ASCII, this operation can be further optimized by using byte equality, rather than character equality. --- crates/ide/src/inlay_hints.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index a74829cd0..ddab9a168 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -353,13 +353,18 @@ fn is_argument_similar_to_param_name( } match get_string_representation(argument) { None => false, - Some(mut repr) => { - let param_name = param_name.to_ascii_lowercase(); - let argument_string = { - repr.make_ascii_lowercase(); - repr.trim_start_matches('_') - }; - argument_string.starts_with(¶m_name) || argument_string.ends_with(¶m_name) + Some(argument_string) => { + let mut arg_bytes = argument_string.bytes().skip_while(|&c| c == b'_'); + let starts_with_pattern = param_name.bytes().all( + |expected| matches!(arg_bytes.next(), Some(actual) if expected.eq_ignore_ascii_case(&actual)), + ); + + let mut arg_bytes = argument_string.bytes(); + let ends_with_pattern = param_name.bytes().rev().all( + |expected| matches!(arg_bytes.next_back(), Some(actual) if expected.eq_ignore_ascii_case(&actual)), + ); + + starts_with_pattern || ends_with_pattern } } } -- cgit v1.2.3 From 22566ecd1b3f64c0b5d577ea3dac161ebb23eda4 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Sun, 10 Jan 2021 03:05:52 -0500 Subject: Short-circuit boolean operation --- crates/ide/src/inlay_hints.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index ddab9a168..2ad8c33f2 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -354,17 +354,21 @@ fn is_argument_similar_to_param_name( match get_string_representation(argument) { None => false, Some(argument_string) => { + // Does the argument name begin with the parameter name? Ignore leading underscores. let mut arg_bytes = argument_string.bytes().skip_while(|&c| c == b'_'); let starts_with_pattern = param_name.bytes().all( |expected| matches!(arg_bytes.next(), Some(actual) if expected.eq_ignore_ascii_case(&actual)), ); + if starts_with_pattern { + return true; + } + + // Does the argument name end with the parameter name? let mut arg_bytes = argument_string.bytes(); - let ends_with_pattern = param_name.bytes().rev().all( + param_name.bytes().rev().all( |expected| matches!(arg_bytes.next_back(), Some(actual) if expected.eq_ignore_ascii_case(&actual)), - ); - - starts_with_pattern || ends_with_pattern + ) } } } -- cgit v1.2.3 From 5e81892d4dd3b8353d11df8b9239432b5aa10512 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Sun, 10 Jan 2021 04:14:39 -0500 Subject: Skip leading underscores unconditionally --- crates/ide/src/inlay_hints.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'crates/ide/src') diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 2ad8c33f2..3e9a65d9c 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -354,8 +354,11 @@ fn is_argument_similar_to_param_name( match get_string_representation(argument) { None => false, Some(argument_string) => { + let num_leading_underscores = + argument_string.bytes().take_while(|&c| c == b'_').count(); + // Does the argument name begin with the parameter name? Ignore leading underscores. - let mut arg_bytes = argument_string.bytes().skip_while(|&c| c == b'_'); + let mut arg_bytes = argument_string.bytes().skip(num_leading_underscores); let starts_with_pattern = param_name.bytes().all( |expected| matches!(arg_bytes.next(), Some(actual) if expected.eq_ignore_ascii_case(&actual)), ); @@ -365,7 +368,7 @@ fn is_argument_similar_to_param_name( } // Does the argument name end with the parameter name? - let mut arg_bytes = argument_string.bytes(); + let mut arg_bytes = argument_string.bytes().skip(num_leading_underscores); param_name.bytes().rev().all( |expected| matches!(arg_bytes.next_back(), Some(actual) if expected.eq_ignore_ascii_case(&actual)), ) -- cgit v1.2.3