This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author methane
Recipients ezio.melotti, methane, mrabarnett, rhettinger, serhiy.storchaka
Date 2017-12-18.09:23:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1513589013.45.0.213398074469.issue32338@psf.upfronthosting.co.za>
In-reply-to
Content
> This is surprising. But OrderedDict also can be O(N) here.

Current dict implementation doesn't skip empty entries.
So next(iter(D)) takes time.
On the other hand, OrderedDict uses doubly-linked list to find first entry.

(I fixed it in compact ODict branch, but it added one word to dict)

> Do you have benchmarking results Inada?

$ ./python -m perf timeit -s 'cache={}' -- '
for i in range(10000):
    if len(cache) > 512:
        del cache[next(iter(cache))]
    cache[i]=i
'
.....................
Mean +- std dev: 6.81 ms +- 0.08 ms

$ ./python -m perf timeit -s 'from collections import OrderedDict; cache=OrderedDict()' -- '
for i in range(10000):
    if len(cache) > 512:
        cache.popitem(last=False)
    cache[i]=i
'
.....................
Mean +- std dev: 3.75 ms +- 0.07 ms

Performance difference is measurable even when N is only 512.

Maybe, we can use hack similar to Python 3.5 had for O(1) popitem().
When entries[0].key==NULL, (Py_ssize_t)entries[0].value can be index
to first known non-empty entry.
History
Date User Action Args
2017-12-18 09:23:33methanesetrecipients: + methane, rhettinger, ezio.melotti, mrabarnett, serhiy.storchaka
2017-12-18 09:23:33methanesetmessageid: <1513589013.45.0.213398074469.issue32338@psf.upfronthosting.co.za>
2017-12-18 09:23:33methanelinkissue32338 messages
2017-12-18 09:23:33methanecreate