GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-141510: Optimize {frozen}dict.fromkeys for frozendict by corona10 · Pull Request #144915 · python/cpython

Conversation

@corona10

@corona10 corona10 commented Feb 17, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: PEP 814: Add built-in frozendict type #141510

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
corona10 added 2 commits February 17, 2026 22:24
@corona10
pythongh-141510: Optimize {frozen}dict.fromkeys for frozendict
fd3be29
@corona10
Add NEWS.d
36f2d77
@corona10
corona10 requested review from markshannon and methane as code owners February 17, 2026 13:27
@bedevere-app bedevere-app Bot added the awaiting core review label Feb 17, 2026
@corona10
corona10 requested review from kumaraditya303 and vstinner February 17, 2026 13:27
@bedevere-app bedevere-app Bot mentioned this pull request Feb 17, 2026
PEP 814: Add built-in frozendict type #141510
Closed
vstinner
vstinner reviewed Feb 17, 2026
View reviewed changes
Comment thread Objects/dictobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Objects/dictobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

corona10 added 2 commits February 17, 2026 22:49
@corona10
Update docs
f056fa6
@corona10
fix
b51b1aa
@corona10
corona10 requested a review from vstinner February 17, 2026 14:15
@corona10
Address code review
a49d630
kumaraditya303
kumaraditya303 reviewed Feb 17, 2026
View reviewed changes
Comment thread Objects/dictobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@corona10
Address code review
da588a1
@corona10
corona10 requested a review from kumaraditya303 February 17, 2026 14:51
kumaraditya303
kumaraditya303 approved these changes Feb 17, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Feb 17, 2026
@corona10
corona10 enabled auto-merge (squash) February 17, 2026 14:56
vstinner
vstinner reviewed Feb 17, 2026
View reviewed changes
Comment thread Doc/library/stdtypes.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@corona10
corona10 disabled auto-merge February 17, 2026 15:01
@corona10
Address code review
49e8a98
methane
methane reviewed Feb 17, 2026
View reviewed changes
Comment thread Objects/dictobject.c
PyObject *d;
int status;

d = _PyObject_CallNoArgs(cls);

@methane methane Feb 17, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

If d is a newly created object, whether it's a dict or a frozendict, wouldn't it be unnecessary to lock it?

If we consider the possibility that d might leak to another thread for some reason, wouldn't it be necessary to lock it even in the case of a frozendict?

class D(frozendict):
    def __new__(cls):
        return frozendict({"a":1, "b":2})

d = D.fromkeys("def")
print(d)
# frozendict({'a': 1, 'b': 2, 'd': None, 'e': None, 'f': None})

In this case, d is exact frozendict, but d can be leaked to other thread.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

corona10 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@corona10 corona10 Feb 17, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think we could remove the lock for d, but since it has been locked before, would it make sense to test this in a separate PR?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Feb 17, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I don't think that other threads can access d during fromkeys() call.

D.fromkeys() calls dict.fromkeys() and dict.__new__(): the return type is dict, not D.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

corona10 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@methane methane Feb 18, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

D.fromkeys() calls dict.fromkeys() and dict.__new__(): the return type is dict, not D.

dict.fromkeys() is classmethod. D.fromkeys() calls dict.fromkeys(D).

Here's an even worse example.

d0 = None

class D(frozendict):
    def __new__(cls):
        global d0
        d0 = frozendict({"a":1, "b":2})
        return d0

d = D.fromkeys("def")
print(d0)  # frozendict({'a': 1, 'b': 2, 'd': None, 'e': None, 'f': None})
print(d)   # frozendict({'a': 1, 'b': 2, 'd': None, 'e': None, 'f': None})

In this case d0 is mutated after it is exposed to Python.
It completely betrays the expectations of a frozendict.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Feb 18, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Oh. This is bad :-( I wrote #144944 to raise an exception if this case occurs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@corona10
corona10 requested review from methane and vstinner February 17, 2026 15:19
@corona10
Address code review
8d8fc98
@corona10

corona10 commented Feb 17, 2026

Copy link
Copy Markdown
Member Author

@vstinner @methane I've simplifed the PR to follow your last reivews.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@corona10
nit
fa2505a
@corona10 corona10 changed the title gh-141510: Optimize {frozen}dict.fromkeys for frozendict [WIP} gh-141510: Optimize {frozen}dict.fromkeys for frozendict Feb 17, 2026
@corona10 corona10 changed the title [WIP} gh-141510: Optimize {frozen}dict.fromkeys for frozendict [WIP] gh-141510: Optimize {frozen}dict.fromkeys for frozendict Feb 17, 2026
@corona10
fix
fcef217
@corona10 corona10 changed the title [WIP] gh-141510: Optimize {frozen}dict.fromkeys for frozendict gh-141510: Optimize {frozen}dict.fromkeys for frozendict Feb 17, 2026
corona10 added 7 commits February 18, 2026 01:12
@corona10
fix
0e70217
@corona10
fix
305b2ef
@corona10
Revert "fix"
0a1b400
This reverts commit 305b2ef.
@corona10
Revert "fix"
5e27575
This reverts commit 0e70217.
@corona10
Revert "fix"
6c8d856
This reverts commit fcef217.
@corona10
Revert "nit"
26c46c7
This reverts commit fa2505a.
@corona10
Revert "Address code review"
d2fb5a0
This reverts commit 8d8fc98.
@corona10

corona10 commented Feb 17, 2026

Copy link
Copy Markdown
Member Author

@vstinner @methane
It turns out that removing the lock from d requires quite a bit of cleanup, since there is a series of internal lock-holding checks. I’ll handle this separately in a follow-up change.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

vstinner
vstinner approved these changes Feb 17, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Objects/dictobject.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@corona10
Address code review
e7c7dff
@corona10
corona10 enabled auto-merge (squash) February 17, 2026 21:17
@corona10
corona10 merged commit d1b541b into python:main Feb 17, 2026
47 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Feb 17, 2026
@corona10
corona10 deleted the gh-141510-_PyDict_FromKeys branch February 17, 2026 21:53
brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
@corona10 @brijkapadia
pythongh-141510: Optimize {frozen}dict.fromkeys for frozendict (pytho…
c2b8cdf
…ngh-144915)
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@corona10 @ljfp
pythongh-141510: Optimize {frozen}dict.fromkeys for frozendict (pytho…
235e2ae
…ngh-144915)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@kumaraditya303 kumaraditya303 kumaraditya303 approved these changes
@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@methane methane Awaiting requested review from methane methane is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@corona10 @vstinner @methane @kumaraditya303
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.