mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 02:38:25 +00:00
[red-knot] optimize is_subtype_of for literals (#17394)
## Summary Allows us to establish that two literals do not have a subtype relationship with each other, without having to fallback to a typeshed Instance type, which is comparatively slow. Improves the performance of the many-string-literals union benchmark by 5x. ## Test Plan `cargo test -p red_knot_python_semantic` and `cargo bench --bench red_knot`.
This commit is contained in:
parent
9bee9429de
commit
e2a38e4c00
1 changed files with 21 additions and 0 deletions
|
@ -886,6 +886,27 @@ impl<'db> Type<'db> {
|
|||
target.is_equivalent_to(db, Type::object(db))
|
||||
}
|
||||
|
||||
// No literal type is a subtype of any other literal type, unless they are the same
|
||||
// type (which is handled above). This case is not necessary from a correctness
|
||||
// perspective (the fallback cases below will handle it correctly), but it is important
|
||||
// for performance of simplifying large unions of literal types.
|
||||
(
|
||||
Type::StringLiteral(_)
|
||||
| Type::IntLiteral(_)
|
||||
| Type::BytesLiteral(_)
|
||||
| Type::ClassLiteral(_)
|
||||
| Type::FunctionLiteral(_)
|
||||
| Type::ModuleLiteral(_)
|
||||
| Type::SliceLiteral(_),
|
||||
Type::StringLiteral(_)
|
||||
| Type::IntLiteral(_)
|
||||
| Type::BytesLiteral(_)
|
||||
| Type::ClassLiteral(_)
|
||||
| Type::FunctionLiteral(_)
|
||||
| Type::ModuleLiteral(_)
|
||||
| Type::SliceLiteral(_),
|
||||
) => false,
|
||||
|
||||
// All `StringLiteral` types are a subtype of `LiteralString`.
|
||||
(Type::StringLiteral(_), Type::LiteralString) => true,
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue