-
Notifications
You must be signed in to change notification settings - Fork 52
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
Don't leave dangling refs when removing more than 350 constraints #272
Conversation
src/optlang/interface.py
Outdated
@@ -1577,6 +1577,7 @@ def _remove_constraints(self, constraints): | |||
keys = [constraint.name for constraint in constraints] | |||
if len(constraints) > 350: # Need to figure out a good threshold here | |||
self._constraints = self._constraints.fromkeys(set(self._constraints.keys()).difference(set(keys))) | |||
map(lambda co: setattr(co, "problem", None), constraints) # quicker than a loop |
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 it really faster? Map generates a list which also needs to be created in memory. Does the speed difference matter much? Perhaps defining a local partial function offers similar speed if it matters?
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.
Was about 100x faster in my benchmarks for that single step (100us vs 200ns). However, the full deletion code is slower (~100ms) so it does not make much difference in practice.
Do you mean the partial instead of the lambda?
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.
Two different ideas here:
setter = partial(setattr, name="problem", value=None)
for co in constraints:
setter(co)
or
setter = object.__setattr__
for co in constraints:
setter(co, "problem", None)
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.
If you do the explicit loop
for co in constraints:
co.problem = None
is probably the most readable as suggested in the issue.
Also how is setter
in the second example different from the normal setattr
?
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 still have an optlang branch that implemented 80% of explicitly using the observer pattern with setter and getter methods. I never got beyond a proof of concept for GLPK, though :(
@Midnighter I simplified it as requested. |
Co-authored-by: Moritz E. Beber <[email protected]>
This fixes the issue illustrated in #271.