[Python-Dev] Guarantee ordered dict literals in v3.7?

Serhiy Storchaka storchaka at gmail.com
Wed Nov 8 03:33:07 EST 2017


08.11.17 04:33, Nick Coghlan пише:
> On 8 November 2017 at 11:44, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> 2. So far, I haven't actually come up with a perturbed iteration
>> implementation that doesn't segfault the interpreter. The dict
>> internals are nicely laid out to be iteration friendly, but they
>> really do assume that you're going to start at index zero, and then
>> iterate through to the end of the array. The bounds checking and
>> pointer validity testing becomes relatively fiddly if you try to push
>> against that and instead start iteration from a point partway through
>> the storage array.
> 
> In case anyone else wants to experiment with a proof of concept:
> https://github.com/ncoghlan/cpython/commit/6a8a6fa32f0a9cd71d9078fbb2b5ea44d5c5c14d
> 
> I think we've probably exhausted the utility of discussing this as a
> purely hypothetical change, and so the only way to move the discussion
> forward will be for someone to draft a patch that:
> 
> 1. Perturbs iteration for regular dicts (it's OK for our purposes if
> it's still deterministic - it just shouldn't match insertion order the
> way odict does)
> 2. Switches keyword args and class body execution namespaces over to
> odict so the test suite passes again
> 3. Measures the impact such a change would have on the benchmark suite
> 
> My experiment is a starting point, but it will still be a fair bit of
> work to get it from there to a viable proof of concept that can be
> assessed against the status quo.

It may be easy and more efficient to break the order at insertion.

1. Insert in the reversed order.

2. Add at the end or at the begin, changing the order on every insertion.

2. Swap with an arbitrary item.



More information about the Python-Dev mailing list