From 04b636cba2c2eff5539c846fe4e654583b7ee472 Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 14 Oct 2024 15:17:19 +0200 Subject: [PATCH] [red knot] Use memmem::find instead of custom version (#13750) This is a follow-up on #13746: - Use `memmem::find` instead of rolling our own inferior version. - Avoid `x.as_ref()` calls using `&**x` --- Cargo.lock | 1 + crates/red_knot_python_semantic/Cargo.toml | 1 + .../src/types/infer.rs | 22 +++++++------------ 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7da7c035dbb2e..965a28362d55e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2083,6 +2083,7 @@ dependencies = [ "hashbrown 0.15.0", "insta", "itertools 0.13.0", + "memchr", "ordermap", "red_knot_test", "red_knot_vendored", diff --git a/crates/red_knot_python_semantic/Cargo.toml b/crates/red_knot_python_semantic/Cargo.toml index cc475b71e07e1..7a74f5c307043 100644 --- a/crates/red_knot_python_semantic/Cargo.toml +++ b/crates/red_knot_python_semantic/Cargo.toml @@ -34,6 +34,7 @@ hashbrown = { workspace = true } smallvec = { workspace = true } static_assertions = { workspace = true } test-case = { workspace = true } +memchr = { workspace = true } [dev-dependencies] ruff_db = { workspace = true, features = ["os", "testing"] } diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index e5c173cbc69ea..4b1c420bb4741 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -2661,18 +2661,8 @@ impl<'db> TypeInferenceBuilder<'db> { } (Type::BytesLiteral(salsa_b1), Type::BytesLiteral(salsa_b2)) => { - let contains_subsequence = |needle: &[u8], haystack: &[u8]| { - if needle.is_empty() { - true - } else { - haystack - .windows(needle.len()) - .any(|window| window == needle) - } - }; - - let b1 = salsa_b1.value(self.db).as_ref(); - let b2 = salsa_b2.value(self.db).as_ref(); + let b1 = &**salsa_b1.value(self.db); + let b2 = &**salsa_b2.value(self.db); match op { ast::CmpOp::Eq => Some(Type::BooleanLiteral(b1 == b2)), ast::CmpOp::NotEq => Some(Type::BooleanLiteral(b1 != b2)), @@ -2680,8 +2670,12 @@ impl<'db> TypeInferenceBuilder<'db> { ast::CmpOp::LtE => Some(Type::BooleanLiteral(b1 <= b2)), ast::CmpOp::Gt => Some(Type::BooleanLiteral(b1 > b2)), ast::CmpOp::GtE => Some(Type::BooleanLiteral(b1 >= b2)), - ast::CmpOp::In => Some(Type::BooleanLiteral(contains_subsequence(b1, b2))), - ast::CmpOp::NotIn => Some(Type::BooleanLiteral(!contains_subsequence(b1, b2))), + ast::CmpOp::In => { + Some(Type::BooleanLiteral(memchr::memmem::find(b2, b1).is_some())) + } + ast::CmpOp::NotIn => { + Some(Type::BooleanLiteral(memchr::memmem::find(b2, b1).is_none())) + } ast::CmpOp::Is => { if b1 == b2 { Some(KnownClass::Bool.to_instance(self.db))