Mypy micro-optimizations (batch 3/3) by JukkaL · Pull Request #19770 · python/mypy

Skip to content

Navigation Menu

Sign in

Appearance settings

Conversation

@JukkaL

Several mypy micro-optimizations. Together with batches 1 and 2 these improve self check performance by 1.8%.

This mostly avoids some allocations of nested function objects.

@github-actions

This comment has been minimized.

hauntsaninja

Choose a reason for hiding this comment

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

would _is_subtype = is_proper_subtype if overlap_for_overloads else is_subtype be fast enough since we don't allocate a function object? would be more readable

@ilevkivskyi

@hauntsaninja That will be still slower because the only way you can an "unknown" Callable is py_call. The idea is to compile those calls into static calls.

@hauntsaninja

yes it'll be slower, but should be much faster than allocating a new function object while keeping things more readable. don't feel strongly though and i didn't measure

ilevkivskyi

if is_subtype(left, right, ignore_promotions=ignore_promotions) or is_subtype(
right, left, ignore_promotions=ignore_promotions
):
return True

Choose a reason for hiding this comment

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

One possible way to de-uglify this is to move this whole chunk to a new function outside (say overlap_simple()), and re-use it below. This way it will at least not appear twice.

@github-actions

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅