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

[breaking] Cleanup the Context object: remove id_hash, simplify dictionaries #645

Merged
merged 3 commits into from
May 9, 2024

Conversation

ericphanson
Copy link
Collaborator

@ericphanson ericphanson commented May 8, 2024

We are using objectid as our hash, since there are no collisions thanks to the Julia runtime. We are using this as keys in dictionaries inside the Context object. However, an IdDict is exactly a dict that uses objectid for lookup. This also makes making new AbstractVariables a bit easier, although they still need to be mutable. (Before they had to be mutable and you had to set the id_hash yourself).

I don't expect this to necessarily have any performance implications, but IdDicts should be quite fast (since there's no hashing involved), and I think this should reduce the likelihood of bugs. E.g. I noticed in ComplexConstant we were generating the id_hash randomly (for some reason), which does have the chance of collision. We are still fairly type unstable, as before.

Breakingness

I tagged this breaking since the instructions on how to create a custom AbstractVariable have changed. However, I don't think it is very breaking in practice; if you followed the old advice and had a mutable object with an id_hash field, it should still work fine, we just no longer inspect the id_hash field. However if you had an immutable variable type, and you distinguished instances via the id_hash value, now you will get collisions. I think there are essentially 0 users of the AbstractVariable interface in the wild though, so IMO it's worth cleaning up now.

Aside on ordering

This PR has the potential to regress on #488, since this drops the ordering from some dictionaries, and we don't have a test for #488. However, I've looked through the code, and I don't think we actually rely on dictionary iteration ordering anywhere with these dictionaries. We only iterate to retrieve results from MOI, not to add variables/constraints (since those get added during conic_form! whose order is determined by the problem formulation).

The PR that fixed #488 also changed a dictionary inside Hcat, which we definitely did use iteration order to choose the order of objects that got added to the model:

for (id, col_size) in variable_to_sizes

I suspect this was the real fix. We no longer build the hcat atom in such a complicated way, and this dictionary has been deleted as far as I can tell.

I did run the problem from #488 a couple times (recreating it from scratch) and got the same results (even the same values of the variable X1 and the objective), as well.

@ericphanson ericphanson changed the title Cleanup the Context object: remove id_hash, simplify dictionaries [breaking] Cleanup the Context object: remove id_hash, simplify dictionaries May 8, 2024
Copy link

codecov bot commented May 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.88%. Comparing base (84a30f2) to head (16073bf).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #645      +/-   ##
==========================================
+ Coverage   97.86%   97.88%   +0.01%     
==========================================
  Files          88       88              
  Lines        5116     5108       -8     
==========================================
- Hits         5007     5000       -7     
+ Misses        109      108       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@odow odow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems okay. I don't know if I truly understand the implications, but it looks sensible.

@odow
Copy link
Member

odow commented May 9, 2024

I took a second look at the ordering: I concur that we do not rely on the ordering for these dicts.

@ericphanson ericphanson merged commit 19ab3a9 into master May 9, 2024
11 of 12 checks passed
@ericphanson ericphanson deleted the eph/cleanup-context branch May 9, 2024 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Different result each time problem is solved
2 participants