KEMBAR78
[mypyc] Speed up equality with optional str/bytes types by JukkaL · Pull Request #19758 · python/mypy · GitHub
Skip to content

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Aug 29, 2025

Specialize most equality (== and !=) operations when one of the operands is str | None or bytes | None. First check if the value is None, and based on that branch into fast path operations. Previously we used a generic C API primitive for such comparisons, which was quite slow.

This could be generalized to other optional types, but let's start with str | None since it's a very common type and it's often used in equality tests. bytes | None is also covered, since it's very similar to the str case.

Also add support for unchecked Cast operations in the IR. These don't perform a runtime type check -- they can be used to narrow the static type when it can be known statically that the cast is always safe.

else:
r_none = BasicBlock()
r_not_none = BasicBlock()
# Fast path: eight value is None?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Fast path: eight value is None?
# Fast path: right value is None?

@JukkaL JukkaL merged commit 6a97dc6 into master Aug 29, 2025
13 checks passed
@JukkaL JukkaL deleted the mypyc-equality branch August 29, 2025 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants