-
Notifications
You must be signed in to change notification settings - Fork 609
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
perf(ir): avoid exponential growth on name
attribute access
#8445
Conversation
This commit changes the use of `hasattr` + `name` attribute access to eagerly access the `name` attribute, store it and check for `None`. Accessing the attribute twice may seem innocuous, but since names may be generated recursively, this results in 2 ** n growth in the number of accesses: once for the `hasattr` call, and once for the access if `hasattr` succeeds. This can happen with large operation trees where names are automatically generated.
name
attribute accessname
attribute access
@@ -85,7 +85,11 @@ def __coerce__( | |||
# TODO(kszucs): figure out how to represent not named arguments | |||
@property |
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 think we should turn this into @attribute
in a follow-up since the .name
property is accessed multiple times during both construction and compilation.
for _ in range(n): # noqa: F402 | ||
x = x + 1 | ||
|
||
assert x.op().name.count("Add") == n |
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.
@cpcloud do we want to add some way of checking for recursion explosion? Maybe use n=200 and then test this doesn't take longer than 3 seconds to compute?
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.
It didn't finish in any reasonable amount of time, so just having it as a standard unit test seems fine.
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.
ahh, I see, so you would just notice it during local dev, no automation needed. Sure that works.
Fixes #8432. In short, the pattern we are using here for accessing
name
results in exponential growth of attribute accesses.