Skip to content

gh-150027: Avoid copying during construction of frozenset objects#150028

Merged
ZeroIntensity merged 10 commits into
python:mainfrom
ZeroIntensity:optimize-frozenset
May 19, 2026
Merged

gh-150027: Avoid copying during construction of frozenset objects#150028
ZeroIntensity merged 10 commits into
python:mainfrom
ZeroIntensity:optimize-frozenset

Conversation

@ZeroIntensity
Copy link
Copy Markdown
Member

@ZeroIntensity ZeroIntensity commented May 18, 2026

Copy link
Copy Markdown
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

Looks good in general.
Can you add a test to show that this is producing sensible bytecode?

Comment thread Objects/setobject.c Outdated
}

PyObject *
_PyFrozenSet_NewAndSteal(PyObject *set)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you rename this to something that makes it explicit than it modifies the set.
Maybe _PySet_Freeze or something like that.

Comment thread Python/codegen.c Outdated

expr_ty arg_expr = asdl_seq_GET(args, 0);

if (arg_expr->kind != GeneratorExp_kind) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you move this test after the body of this optimization:

if (_PyUnicode_EqualToASCIIString(func->v.Name.id, "frozenset") ...
    ...
}
if (arg_expr->kind != GeneratorExp_kind) {
    return 0;
}

Copy link
Copy Markdown
Member

@markshannon markshannon left a comment

Choose a reason for hiding this comment

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

Looks good, thanks.

I note that the code generating the argument to frozenset is duplicated.
This is a pre-existing issue with the way that maybe_optimize_function_call works.
We should fix that in a future PR.

@ZeroIntensity ZeroIntensity merged commit 409fa8e into python:main May 19, 2026
103 of 106 checks passed
@ZeroIntensity ZeroIntensity deleted the optimize-frozenset branch May 19, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants