Skip to content
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

gh-116621: Specialize list.extend for dict keys/values #116816

Merged
merged 2 commits into from Mar 15, 2024

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Mar 14, 2024

@@ -568,14 +568,22 @@ def __hash__(self):
od[key] = i

# These should not crash.
with self.assertRaises(KeyError):
try:
Copy link
Member Author

@corona10 corona10 Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colesbury
I noticed that this change has some behavior changes.(in nogil 3.12, it has same behavior changes)
I'm not sure it's worth applying it.
See: colesbury/nogil-3.12@da071fa also

Let me check a microbenchmark instead.

Copy link
Member Author

@corona10 corona10 Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my local benchmark, for the default build, it makes 2% slower.
(Or we can handle it as a noise)

(.venv) ➜  cpython git:(gh-116621-more) ✗ python -m pyperf compare_to base.json opt.json
Mean +- std dev: [base] 36.3 us +- 0.7 us -> [opt] 36.8 us +- 0.6 us: 1.02x slower
import pyperf

runner = pyperf.Runner()
runner.timeit(name="bench list.extend keys",
              stmt="""
keys = list(d.keys())
""",
              setup = """
d = dict()
for k in range(10000):
    d[k] = k
"""
              )

Copy link
Member Author

@corona10 corona10 Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few other cases in nogil-3.12 that we probably want to handle atomically as well, because I think we use the same pattern where the calling code assumes list(some_type) is atomic for certain built-in types:

When considering your suggestion, IMO, we should apply this technique to the free-threaded build only, WDYT?
And please let me know if this is not a fair benchmark.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the OrderedDict changes: let's check for the exact type PyDictKeys_Type and PyDictValues_Type. I'm not sure using PyDict_Next is always appropriate for subclasses.

On the benchmark, I think that's just noise. I was not able to reproduce that locally. Avoiding the iterator creation in the specialized case is going to be faster when the dict has few items.

Objects/listobject.c Outdated Show resolved Hide resolved
Objects/listobject.c Outdated Show resolved Hide resolved
Objects/listobject.c Outdated Show resolved Hide resolved
@@ -568,14 +568,22 @@ def __hash__(self):
od[key] = i

# These should not crash.
with self.assertRaises(KeyError):
try:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the OrderedDict changes: let's check for the exact type PyDictKeys_Type and PyDictValues_Type. I'm not sure using PyDict_Next is always appropriate for subclasses.

On the benchmark, I think that's just noise. I was not able to reproduce that locally. Avoiding the iterator creation in the specialized case is going to be faster when the dict has few items.

@corona10
Copy link
Member Author

corona10 commented Mar 14, 2024

Thanks for the review, I will take a look at tomorrow :)

@corona10 corona10 marked this pull request as ready for review March 15, 2024 04:51
@corona10
Copy link
Member Author

Tests / CIFuzz (memory) (pull_request)

Unrelated to this PR.

@corona10 corona10 requested a review from colesbury March 15, 2024 05:47
@corona10 corona10 merged commit 8da83f3 into python:main Mar 15, 2024
35 of 36 checks passed
@corona10 corona10 deleted the gh-116621-more branch March 15, 2024 14:48
vstinner pushed a commit to vstinner/cpython that referenced this pull request Mar 20, 2024
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 25, 2024
diegorusso pushed a commit to diegorusso/cpython that referenced this pull request Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants