-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF: Break reference cycle for all Index types #27840
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: Break reference cycle for all Index types #27840
Conversation
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.
cc @jreback
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.
looks good -
question and comment
@@ -441,7 +442,9 @@ def _formatter_func(self): | |||
|
|||
@cache_readonly | |||
def _engine(self): | |||
return self._engine_type(lambda: self, len(self)) | |||
# To avoid a reference cycle, pass a weakref of self to _engine_type. | |||
period = weakref.ref(self) |
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.
should we be using weakref in the other invocations for consistency?
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.
weakrefs cause some tests to fail.
I'm not sure currently if those failures are correct, could be worth an investigation. @crepererum , did you investigate if weakrefs not working is the correct behaviour?
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.
No, I didn't test this.
IMHO, the weakref isn't required in the other cases since we're passing a member of self
and not self
itself. Using a weakref seemed to make things only unnecessary complicated, since then you have just a chain of indirections (lambda->scope->weakref->self->member
instead of lambda->scope->member
).
Thanks for providing this extended fix :) |
86bf0fe
to
36061a1
Compare
I think this should be ok to pull in after the rebase? |
Agreed, thanks! |
* Generalize guards for index ref cycles * add issue number
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff
Generalizes the test in #27607 to be used for all index types. This finds ref cycle issues for
PeriodIndex
andCategoricalIndex
which are solved in this PR.