From f62e5406f247e45df05a4f27b2fbd178f9851b9d Mon Sep 17 00:00:00 2001 From: David Peter Date: Thu, 20 Feb 2025 23:06:11 +0100 Subject: [PATCH] [red-knot] Short-circuit bool calls on bool (#16292) ## Summary This avoids looking up `__bool__` on class `bool` for every `Type::Instance(bool).bool()` call. 1% performance win on cold cache, 4% win on incremental performance. --- crates/red_knot_python_semantic/src/types.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index 625a48689f..76b55cea27 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -1452,7 +1452,9 @@ impl<'db> Type<'db> { Type::AlwaysTruthy => Truthiness::AlwaysTrue, Type::AlwaysFalsy => Truthiness::AlwaysFalse, instance_ty @ Type::Instance(InstanceType { class }) => { - if class.is_known(db, KnownClass::NoneType) { + if class.is_known(db, KnownClass::Bool) { + Truthiness::Ambiguous + } else if class.is_known(db, KnownClass::NoneType) { Truthiness::AlwaysFalse } else { // We only check the `__bool__` method for truth testing, even though at