Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up TypeQuery #7128

Open
JukkaL opened this issue Jul 2, 2019 · 6 comments
Open

Speed up TypeQuery #7128

JukkaL opened this issue Jul 2, 2019 · 6 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jul 2, 2019

mypy.type_visitor.TypeQuery is constructed often with the any or all builtin function as an argument. The function is used as a callable inside the query. Calling callable variables is kind of slow when compiled with mypyc. Since type queries are used a lot, switching to an integer constant or a compiled object instead of a callable could speed up runtimes measurably, perhaps by as much as 0.5% or so (random guess). Since the optimization is pretty simple, this may be worth doing.

Discussion:

  • Maybe have separate type query visitor which produces boolean values. The strategy could be specified as an integer constant.
  • For non-bool type queries, experiment with using an object instead of a callable for the strategy, since callables are currently pretty slow when compiled.
  • Maybe also special case self.strategy([]) since it's used a lot.
@mridullpandey
Copy link

hi @JukkaL i want to work on can you assign it to me
thanks :-)

@mridullpandey
Copy link

can you please let me know how to get started

@JukkaL
Copy link
Collaborator Author

JukkaL commented Feb 28, 2020

@mridullpandey I've assigned this to you. Here are some hints:

  • Figure out how to run mypy in compiled mode so that you can see whether you've improved performance. A good benchmark is type checking mypy. You need to figure out a way to measure the time consistently with accuracy of about 0.2% to validate whether you've made progress (or that at least you haven't regressed anything). Victor Stinner has written about how to do stable benchmarking, in case you want to learn about this.
  • Create a copy of TypeQuery that is specialized for bool instead of accepting an arbitrary type.
  • Replace the strategy constructor argument with an integer constant (corresponding to either any or all).
  • Implement a strategy method that uses the integer constant to implement whatever is needed.

@vbarbaresi
Copy link
Contributor

vbarbaresi commented Oct 17, 2020

I just opened a PR with my attempt at TypeQuery specialized for bool

In your first description of the issue, you wrote:

For non-bool type queries, experiment with using an object instead of a callable for the strategy, since callables are currently pretty slow when compiled.

I wanted to try that out too.
You mean replacing strategy type here

def __init__(self, strategy: Callable[[Iterable[T]], T]) -> None:

by a new type of object that can be compiled by mypyc? There would still have to be a callable method someplace in that object, right?

@Hyperion101010
Copy link

Hi there I am Shivam, I know Python to an intermediate level,
I am interested to solve this issue, will you assign me?

@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 10, 2021

@Hyperion101010 There already is an open PR here: #9604. However, it's not clear if it speeds things up significantly. If you want to help, you can try measuring the impact of the PR on performance. Note that if the difference is small, you may need a specialized environment, such as a machine where you've turned off turbo boost for a constant CPU frequency, and where you've disabled most background processes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants