-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
MAINT: import time: avoid repeated textwrap function dispatch instantiation #14095
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
Conversation
numpy/ma/core.py
Outdated
""") | ||
long_std="""\ | ||
masked_%(name)s(data = | ||
%(data)s, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatting definitely doesn't look right. give me a second.
bf780b8
to
fa60c30
Compare
I'd be curious to know how much of this is the cost of importing |
The stats I gave were obtained from running
with this branch including only this one commit. As such, the code is still improting A runtime dependency on |
No need, just wanted to confirm that this timing is not caused by removing it. In the interest of breaking down the import cost here, can you leave behind the |
yeah, i just looked at the textwrap source, it seems pretty trivial, and shouldn't be a contributor, let me test. |
It seems like it is the fact that we are using dedent that is causing the slowdown
|
numpy/core/overrides.py
Outdated
relevant_args = dispatcher(*args, **kwargs) | ||
return implement_array_function( | ||
implementation, {name}, relevant_args, args, kwargs) | ||
""".format(name=implementation.__name__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect this is where all the cost is - we end up dedenting the same string for every single numpy function, which is obviously expensive. Dedenting it just once wouldn't be nearly as bad, although at that point chances are the string definition lives outside the function, at which point there's nothing to even dedent.
In the interest of preserving the visual indent, I might be inclined to move this string definition to a global _wrapped_func_source
variable in the file, which still avoids paying for textwrap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the interest of preserving the visual indent, I might be inclined to move this string definition to a global _wrapped_func_source variable in the file, which still avoids paying for textwrap
Is this an opinion, or a requirement to get your approval on this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I do suspect you are right in the source of the problem. I just don't personally see any issue with deindenting strings manually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this an opinion
It's an opinion that probably gates my immediate approval until another maintainer weighs ins.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm over it, lets hunt more milliseconds, and work to deprecate the whole testing module. Much more interesting ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a problem with dedenting strings manually, they are ugly, and ugly is unpleasant to maintain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very fair.
@eric-wieser, you know, my original attempt at ripping out I think in this case, manual deindentenation is an appropriate compromise. |
This reverts commit fa60c30.
e688181
to
71f8cbe
Compare
@eric-wieser. My milliseconds need this more than my intention to discuss coding style. Benchmarks show similar improvements, again, hard to say when you are using the computer. Seeing as textwrap is built on top of Thanks for hunting down the true source of the issue. I won't be affraid to use textwrap in my other projects now. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could have been persuaded to take the original with some more thought, but this shortcuts that step. Might be worth a comment explaining it's location.
Can you confirm this still gives the savings you needed?
71f8cbe
to
3f06982
Compare
Yeah, it is still about a 4-6 ms in saving. Pretty good. |
I agree, simply moving out the string formatting seems fine for that import time boost, thanks @hmaarrfk. I will assume that even if we do things such as vendoring dedent this will still make sense. So putting it in. |
@@ -109,6 +109,18 @@ def decorator(func): | |||
return decorator | |||
|
|||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opst, empty line too much (don't worry about it though), just noticed that I did not nitpick ;).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opps
Avoid the use of textwrap to deindent static code. This shaves about 6-8ms ms of import time (at the expense of slightly less readable code depending on who you ask -- but definitely if you ask @eric-wieser)
After: 99.1±2ms
Before: 106±2ms (master)