Skip to content

gh-160147: Optimize make_new_set_untracked() - #161191

Open
vstinner wants to merge 1 commit into
python:mainfrom
vstinner:optim_set
Open

vstinner wants to merge 1 commit into
python:mainfrom
vstinner:optim_set

Conversation

@vstinner

@vstinner vstinner commented Nov 2, 2026 •

Copy link
Copy Markdown
Member

There is no need to set PySetObject explicitly to 0/NULL, since _PyType_AllocNoTrack() already initialize memory to zeros.

There is no need to set PySetObject explicitly to 0/NULL, since
_PyType_AllocNoTrack() already initialize memory to zeros.

@eendebakpt eendebakpt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes are correct. I checked and the compiler does not optimize it away (because type is not compile time), so this is a minor optimization. Did you do benchmarks victor?

I am wondering if we should do this more (there are dozens of sites in cpython where this can be done, some hot paths are enumerate and BaseException_new).

Comment thread Objects/setobject.c

so = (PySetObject *)_PyType_AllocNoTrack(type, 0);
if (so == NULL)
// Initialize PySetObject members to 0/NULL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Initialize PySetObject members to 0/NULL
// fields are zero-initialized by _PyType_AllocNoTrack

(or leave the comment out)

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

Labels

awaiting merge skip news type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants