[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.
This commit is contained in:
David Peter 2025-02-20 23:06:11 +01:00 committed by GitHub
parent 1be4394155
commit f62e5406f2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 3 additions and 1 deletions

View File

@ -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