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

Don't leave dangling refs when removing more than 350 constraints #272

Merged
merged 4 commits into from
Nov 26, 2024

Conversation

cdiener
Copy link
Member

@cdiener cdiener commented Sep 9, 2024

This fixes the issue illustrated in #271.

CHANGELOG.rst Outdated Show resolved Hide resolved
@@ -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
Copy link
Member

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?

Copy link
Member Author

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?

Copy link
Member

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)

Copy link
Member Author

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?

Copy link
Member

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 :(

@cdiener
Copy link
Member Author

cdiener commented Nov 20, 2024

@Midnighter I simplified it as requested.

@cdiener cdiener merged commit 8e49484 into master Nov 26, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

constraint.problem is not always being reset in _remove_constraints of Model
2 participants