Skip to content

gh-158660: Optimize list.extend() for frozenset and frozendict - #163008

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:list_extend_lockless
Dec 2, 2026
Merged

vstinner merged 2 commits into
python:mainfrom
vstinner:list_extend_lockless

Conversation

@vstinner

@vstinner vstinner commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Do not lock frozenset and frozendict to increase threading scalability and performance.

Previously, frozendict used the slow generic
list_extend_iter_lock_held() code path: use the iterator protocol.

Add test_extend_types() to test_list.

Do not lock frozenset and frozendict to increase threading
scalability and performance.

Previously, frozendict used the slow generic
list_extend_iter_lock_held() code path: use the iterator protocol.

Add test_extend_types() to test_list.
@picnixz

picnixz commented Dec 2, 2026

Copy link
Copy Markdown
Member

I'm not sure list.extend(frozenset) makes sense. Lists are for ordered items, but frozenset is unordered. For frozendict, it may makes sense as we guarantee key insertion order (I hope it's the case btw, I haven't checked), but I don't know why we should optimize it for frozenset.

@vstinner

vstinner commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Oh, currently frozenset methods requires to lock the frozenset. I wrote #163017 to allow calling frozenset methods without locking. Once the other PR will be merged, this PR should be rebased on top of it.

@vstinner

vstinner commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

I'm not sure list.extend(frozenset) makes sense.

list.extend() already has a fast-path for set and frozenset objects. This PR only makes a small optimization on frozenset.

I checked which code calls list.extend(set) in the stdlib:

  • Lib/locale.py body: __all__.extend(vars().keys() & _conditional_constants_names). The extend() argument becomes a set.
  • _PyAST_Compile(): compiler_cellvars() calls PySequence_List() on a set and then call PyList_Sort() on the list. PySequence_List(set) calls list.extend(set).
  • _PyAST_Compile(): _PyCompile_StaticAttributesAsTuple() calls PySequence_List() on a set and then call PyList_Sort() on the list. PySequence_List(set) calls list.extend(set).

Yeah, I was also surprised that it's actually used, but it is used :-)

Lists are for ordered items, but frozenset is unordered. For frozendict, it may makes sense as we guarantee key insertion order (I hope it's the case btw, I haven't checked), but I don't know why we should optimize it for frozenset.

The two _PyAST_Compile() sub-functions create a list from an unordered set and then sort the list.

@picnixz

picnixz commented Dec 2, 2026

Copy link
Copy Markdown
Member

Ah I see, so it's not a new change. Well I guess it makes sense then though I wonder why we have the special case in the first place (like historically)

@vstinner

vstinner commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Ah I see, so it's not a new change. Well I guess it makes sense then though I wonder why we have the special case in the first place (like historically)

According to the Git history, list.extend() got a fast-path for set and frozenset with the PR gh-116657 / commit 3325699:

commit 3325699ffa3c633084f3e3fd94c4f3843066db85
Author: Donghee Na <[email protected]>
Date:   Wed Mar 13 07:28:23 2024 +0900

    gh-116621: Set manual critical section for list.extend (gh-116657)

The commit also added TODO(@corona10): Add more special cases for other types. comment. Later, more fast-paths have been added (dict, dict keys, dict values, dict items).

cc @corona10

@vstinner

vstinner commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

@picnixz: So what do you think about this change? Are you ok with it?

The change was suggested by @encukou on my first PR adding PyListBuilder API: #160127 (comment).

@picnixz

picnixz commented Dec 2, 2026

Copy link
Copy Markdown
Member

Oh yes I am ok, I was surprised by the title first actually!

@vstinner
vstinner enabled auto-merge (squash) December 2, 2026 23:11
@vstinner
vstinner merged commit c85a3f2 into python:main Dec 2, 2026
57 checks passed
@vstinner
vstinner deleted the list_extend_lockless branch December 2, 2026 23:41
@bedevere-bot

Copy link
Copy Markdown

⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️

Hi! The buildbot RISC-V 64-bit Ubuntu Clang 3.x (tier-3) has failed when building commit c85a3f2.

What do you need to do:

  1. Don't panic.
  2. Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
  3. Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/2255/builds/886) and take a look at the build logs.
  4. Check if the failure is related to this commit (c85a3f2) or if it is a false positive.
  5. If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.

You can take a look at the buildbot page here:

https://buildbot.python.org/#/builders/2255/builds/886

Failed tests:

  • test_remote_pdb

Summary of the results of the build (if available):

==

Click to see traceback logs
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 1352, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1248, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 856, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 750, in exec_module
  File "<frozen importlib._bootstrap_external>", line 879, in get_code
  File "<frozen importlib._bootstrap_external>", line 503, in _compile_bytecode
ValueError: bad marshal data (unknown type code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Performance or resource usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants