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.

classification
Title: OrderedDict iterator allocates di_result unnecessarily
Type: resource usage Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Kevin Shweh, corona10, eric.snow, methane, miss-islington, rhettinger
Priority: normal Keywords: patch

Created on 2021-12-15 16:48 by Kevin Shweh, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30290 merged corona10, 2021-12-29 15:25
PR 30299 merged miss-islington, 2021-12-30 03:35
PR 30300 merged miss-islington, 2021-12-30 03:36
Messages (7)
msg408616 - (view) Author: Kevin Shweh (Kevin Shweh) Date: 2021-12-15 16:48
The OrderedDict iterator caches a di_result tuple for use with iter(od.items()). It's *supposed* to only do that for the items() case, but the code does

    if (kind & (_odict_ITER_KEYS | _odict_ITER_VALUES))

to test for this case. This is the wrong test. It should be

    if ((kind & _odict_ITER_KEYS) && (kind &_odict_ITER_VALUES))

The current test allocates di_result for key and value iterators as well as items iterators.
msg408670 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2021-12-16 04:09
Nice catch.

>     if ((kind & _odict_ITER_KEYS) && (kind &_odict_ITER_VALUES))

You can reduce one branch by

```
#define _odict_ITER_ITEMS (_odict_ITER_KEYS|_odict_ITER_VALUES)
...
     if (kind & _odict_ITER_ITEMS == _odict_ITER_ITEMS)
```
msg408728 - (view) Author: Kevin Shweh (Kevin Shweh) Date: 2021-12-16 19:26
Almost - C's weird bitwise operator precedence means it has to be parenthesized as

    if ((kind & _odict_ITER_ITEMS) == _odict_ITER_ITEMS)
msg409345 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-12-30 03:36
New changeset fb44d0589615590b1e7895ba78a038e96b15a219 by Dong-hee Na in branch 'main':
bpo-46085: Fix iterator cache mechanism of OrderedDict. (GH-30290)
https://github.com/python/cpython/commit/fb44d0589615590b1e7895ba78a038e96b15a219
msg409347 - (view) Author: miss-islington (miss-islington) Date: 2021-12-30 05:29
New changeset 1b37268ef10bd20c30d349b8401c88215c8a6be8 by Miss Islington (bot) in branch '3.10':
bpo-46085: Fix iterator cache mechanism of OrderedDict. (GH-30290)
https://github.com/python/cpython/commit/1b37268ef10bd20c30d349b8401c88215c8a6be8
msg409348 - (view) Author: miss-islington (miss-islington) Date: 2021-12-30 05:29
New changeset 2d4049da1f61df5cb4314d7e10b54fa556880b0e by Miss Islington (bot) in branch '3.9':
bpo-46085: Fix iterator cache mechanism of OrderedDict. (GH-30290)
https://github.com/python/cpython/commit/2d4049da1f61df5cb4314d7e10b54fa556880b0e
msg409349 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-12-30 05:29
Thanks for reporting Kevin!
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90243
2021-12-30 05:29:44corona10setstatus: open -> closed

messages: + msg409349
stage: patch review -> resolved
2021-12-30 05:29:23miss-islingtonsetmessages: + msg409348
2021-12-30 05:29:12miss-islingtonsetmessages: + msg409347
2021-12-30 03:36:05corona10setmessages: + msg409345
2021-12-30 03:36:00miss-islingtonsetpull_requests: + pull_request28512
2021-12-30 03:35:56miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request28511
2021-12-29 15:25:10corona10setkeywords: + patch
nosy: + corona10

pull_requests: + pull_request28504
stage: patch review
2021-12-16 19:26:11Kevin Shwehsetmessages: + msg408728
2021-12-16 04:09:09methanesetnosy: + methane
messages: + msg408670
2021-12-15 20:53:57rhettingersetnosy: + eric.snow
2021-12-15 16:58:07AlexWaygoodsetnosy: + rhettinger

versions: - Python 3.8
2021-12-15 16:48:00Kevin Shwehcreate