Skip to content

gh-141510: Optimize frozendict(frozendict)#145592

Open
vstinner wants to merge 4 commits intopython:mainfrom
vstinner:frozendict_new_same
Open

gh-141510: Optimize frozendict(frozendict)#145592
vstinner wants to merge 4 commits intopython:mainfrom
vstinner:frozendict_new_same

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Mar 6, 2026

Return the same object unmodified if it's exactly the frozendict type.

Return the same object unmodified if it's exactly the frozendict
type.
@vstinner
Copy link
Member Author

vstinner commented Mar 6, 2026

cc @encukou

Comment on lines +8206 to +8209
if (PyFrozenDict_CheckExact(iterable)) {
// PyFrozenDict_New(frozendict) returns the same object unmodified
return Py_NewRef(iterable);
}
Copy link
Member

Choose a reason for hiding this comment

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

This is how I'd like it, but do note that PyTuple_New or PyFrozenSet_New will always return a new object.

It's probably worth noting this in documentation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, usually we don't document such optimization, but ok, I added it to PyFrozenDict_New() documentation as a "CPython implementation detail".

Copy link
Contributor

Choose a reason for hiding this comment

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

Why document it if it is an internal implementation detail anyways? It gives the wrong sense that users can rely on this in CPython

@vstinner vstinner requested a review from ZeroIntensity as a code owner March 6, 2026 16:34
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.

3 participants