[breaking] Cleanup the Context
object: remove id_hash
, simplify dictionaries
#645
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 theContext
object. However, anIdDict
is exactly a dict that usesobjectid
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 theid_hash
yourself).I don't expect this to necessarily have any performance implications, but
IdDict
s should be quite fast (since there's no hashing involved), and I think this should reduce the likelihood of bugs. E.g. I noticed inComplexConstant
we were generating theid_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 theid_hash
field. However if you had an immutable variable type, and you distinguished instances via theid_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:Convex.jl/src/atoms/affine/stack.jl
Line 87 in 4f52b6f
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.