-
Notifications
You must be signed in to change notification settings - Fork 56
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
Replace OrderedDict with standard dictionaries where possible #427
Comments
We no longer support versions of Python where dicts do not remember insertion order. I'm fine with this change! |
Yeah, I go back and forth on this. I agree the readability is improved in that it's less clunky and cleaner syntax overall, but using OrderedDict explicitly indicates that the ordering within the dictionary is important and we're relying on that, which isn't always that case, and maybe is typically not the case. I could be convinced either way :) |
That's a fair counterpoint, the signaling of OrderedDict is much clearer that insertion order is important.Although I'll note that I'm not sure that distinction is immediately obvious to users. I was pair programming something yesterday, we got to an OrderedDict, and I could literally see their thought process settling on just using a regular dict instead. But I can definitely see the value in signaling to developers that insertion order matters for that object, and not to manipulate it in careless ways... Hmm, maybe I'm changing my stance from pro this change to indecision on it |
I've come to see value in using |
I think my stance is now that I am for keeping But assuming that most places an |
Would you be swayed at all by cold hard performance measurements? While I didn't state this explicitly, part of the subtext behind my suggestion is that I had heard that nowadays dicts are more performant than OrderedDict. (In fact, as I recall hearing the story told, the change in behavior in 3.6 was actually related to a re-implementation intended to improve performance, and the fact that the new implementation now remembered insertion order was a happy side-effect the devs decided to roll with and make permanent). This is hinted at in the documentation for OrderedDict:
Anyhow, I'd never actually tested this myself to confirm this was true in practice, so here are some timings on iterating through the two types of dictionaries:
The results for the regular dict were:
and for OrderedDict:
So, based on just this test regular dictionaries are almost exactly 2X faster for iteration. I suspect that we'd find the OrderedDict more performant for re-ordering operations and the like (as indicated by the docs), but I'd also posit we don't use these all that often in practice and that the primary historical use case for OrderedDict was to ensure we iterated over dictionaries in a predictable order. Have I swayed anyone back to the dark side? |
Performance metrics are usually a good way to sway me; however, it also needs to be paired with how frequently an operation is being done in normal workflows. Is there an OrderedDict that we are hitting a lot where you think this performance matters? :) |
I think OrderedDict objects are almost as common as dicts in pyGSTi. And dictionary accesses happen everywhere in pyGSTi. So these performance numbers push me strongly in favor of switching OrderedDict to dict. |
Thoughts on an alternative way to signal that the ordering matters in that case? Maybe a naming scheme where the dict has a |
Does python have an equivalent of C's "typedef" statement? It creates an
alias for a type, so "typedef float MyType" creates a new type "MyType"
that is isomorphic to (and wholly compatible with) float.
This would let you use dicts for speed, but keep the purely semantic
reminder that this *particular* object cares about order.
…On Tue, May 7, 2024, 3:59 PM Stefan Seritan ***@***.***> wrote:
Thoughts on an alternative way to signal that the ordering matters in that
case? Maybe a naming scheme where the dict has a _ordered or maybe
setting a order_matters attribute. Just wondering if we should have a
convention for we want to alert ourselves to not mess with the order too
much? Or would that end up having too much user exposure and be too
annoying? We could also just do a comment where we instantiate, but that
seems likely to be ignored/hard to maintain.
—
Reply to this email directly, view it on GitHub
<#427 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACNUZ5IOIETRYKTHJZ4EZJDZBFMDHAVCNFSM6AAAAABGWANNN2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJZGQ2DIMJYGE>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Ooh I like this possibility. In Python, this is called type aliasing: https://docs.python.org/3/library/typing.html#type-aliases Would using a type alias like this be an agreeable way to keep the semantic reminder but have increased readability @coreyostrove? Or maybe if you wanted to just have |
Marking this via type annotations works for me 👍 |
In many parts of the codebase we opt to use OrderedDict instead of standard dictionaries in places where having dictionaries 'sorted' by insertion order is important. This is now mostly a vestige from pre-python 3.6/7 days when the standard python dictionary implementation did not remember insertion order, and made no guarantees on the order of returned elements from an iterated dictionary (the change in CPython was made in 3.6, and the announcement that this behavior was now part of the official language spec for all implementations was made with 3.7). This is no longer true, and so for most intents and purposes these two classes are now equivalent. I personally think that switching over to using standard dictionaries would improve readability in most places, but am open different perspectives. Thoughts?
The text was updated successfully, but these errors were encountered: