Repository navigation
gh-150027: Avoid copying during construction of frozenset objects
#150028
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4a05023
Initial implementation.
ZeroIntensity 3fe7fac
Add tests and fix things.
ZeroIntensity ab4bd0a
Add blurb.
ZeroIntensity 3e21fd6
Changes based on sprint feedback.
ZeroIntensity a0ecb7c
Only optimize for set literals (and comprehensions).
ZeroIntensity a44772b
Modify the type instead of making a new object.
ZeroIntensity e7f2d56
Rename to _PySet_Freeze
ZeroIntensity ed49167
Refactor if statement.
ZeroIntensity e7fc901
Add a bytecode test.
ZeroIntensity eeea087
Add a label to the test.
ZeroIntensity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Core_and_Builtins/2026-05-18-17-16-51.gh-issue-150027.sJgLvd.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Improve performance of :class:`frozenset` objects by avoiding copies during | ||
| construction. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3953,22 +3953,45 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end) | |
|
|
||
| if (! (func->kind == Name_kind && | ||
| asdl_seq_LEN(args) == 1 && | ||
| asdl_seq_LEN(kwds) == 0 && | ||
| asdl_seq_GET(args, 0)->kind == GeneratorExp_kind)) | ||
| asdl_seq_LEN(kwds) == 0)) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| expr_ty generator_exp = asdl_seq_GET(args, 0); | ||
| PySTEntryObject *generator_entry = _PySymtable_Lookup(SYMTABLE(c), (void *)generator_exp); | ||
| location loc = LOC(func); | ||
|
|
||
| expr_ty arg_expr = asdl_seq_GET(args, 0); | ||
|
|
||
| if (arg_expr->kind != GeneratorExp_kind) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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;
} |
||
| if (_PyUnicode_EqualToASCIIString(func->v.Name.id, "frozenset") | ||
| && (arg_expr->kind == Set_kind || arg_expr->kind == SetComp_kind)) { | ||
| NEW_JUMP_TARGET_LABEL(c, skip_optimization); | ||
|
|
||
| ADDOP_I(c, loc, COPY, 1); | ||
| ADDOP_I(c, loc, LOAD_COMMON_CONSTANT, CONSTANT_BUILTIN_FROZENSET); | ||
| ADDOP_COMPARE(c, loc, Is); | ||
| ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, skip_optimization); | ||
| ADDOP(c, loc, POP_TOP); | ||
|
|
||
| VISIT(c, expr, arg_expr); | ||
| ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_BUILD_FROZENSET); | ||
|
|
||
| ADDOP_JUMP(c, loc, JUMP, end); | ||
|
|
||
| USE_LABEL(c, skip_optimization); | ||
| return 1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| PySTEntryObject *generator_entry = _PySymtable_Lookup(SYMTABLE(c), (void *)arg_expr); | ||
| if (generator_entry->ste_coroutine) { | ||
| Py_DECREF(generator_entry); | ||
| return 0; | ||
| } | ||
| Py_DECREF(generator_entry); | ||
|
|
||
| location loc = LOC(func); | ||
|
|
||
| int optimized = 0; | ||
| NEW_JUMP_TARGET_LABEL(c, skip_optimization); | ||
|
|
||
|
|
@@ -3994,6 +4017,9 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end) | |
| else if (_PyUnicode_EqualToASCIIString(func->v.Name.id, "set")) { | ||
| const_oparg = CONSTANT_BUILTIN_SET; | ||
| } | ||
| else if (_PyUnicode_EqualToASCIIString(func->v.Name.id, "frozenset")) { | ||
| const_oparg = CONSTANT_BUILTIN_FROZENSET; | ||
| } | ||
| if (const_oparg != -1) { | ||
| ADDOP_I(c, loc, COPY, 1); // the function | ||
| ADDOP_I(c, loc, LOAD_COMMON_CONSTANT, const_oparg); | ||
|
|
@@ -4003,10 +4029,10 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end) | |
|
|
||
| if (const_oparg == CONSTANT_BUILTIN_TUPLE || const_oparg == CONSTANT_BUILTIN_LIST) { | ||
| ADDOP_I(c, loc, BUILD_LIST, 0); | ||
| } else if (const_oparg == CONSTANT_BUILTIN_SET) { | ||
| } else if (const_oparg == CONSTANT_BUILTIN_SET || const_oparg == CONSTANT_BUILTIN_FROZENSET) { | ||
| ADDOP_I(c, loc, BUILD_SET, 0); | ||
| } | ||
| VISIT(c, expr, generator_exp); | ||
| VISIT(c, expr, arg_expr); | ||
|
|
||
| NEW_JUMP_TARGET_LABEL(c, loop); | ||
| NEW_JUMP_TARGET_LABEL(c, cleanup); | ||
|
|
@@ -4017,7 +4043,7 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end) | |
| if (const_oparg == CONSTANT_BUILTIN_TUPLE || const_oparg == CONSTANT_BUILTIN_LIST) { | ||
| ADDOP_I(c, loc, LIST_APPEND, 3); | ||
| ADDOP_JUMP(c, loc, JUMP, loop); | ||
| } else if (const_oparg == CONSTANT_BUILTIN_SET) { | ||
| } else if (const_oparg == CONSTANT_BUILTIN_SET || const_oparg == CONSTANT_BUILTIN_FROZENSET) { | ||
| ADDOP_I(c, loc, SET_ADD, 3); | ||
| ADDOP_JUMP(c, loc, JUMP, loop); | ||
| } | ||
|
|
@@ -4029,7 +4055,8 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end) | |
| ADDOP(c, NO_LOCATION, POP_ITER); | ||
| if (const_oparg != CONSTANT_BUILTIN_TUPLE && | ||
| const_oparg != CONSTANT_BUILTIN_LIST && | ||
| const_oparg != CONSTANT_BUILTIN_SET) { | ||
| const_oparg != CONSTANT_BUILTIN_SET && | ||
| const_oparg != CONSTANT_BUILTIN_FROZENSET) { | ||
| ADDOP_LOAD_CONST(c, loc, initial_res == Py_True ? Py_False : Py_True); | ||
| } | ||
| ADDOP_JUMP(c, loc, JUMP, end); | ||
|
|
@@ -4044,6 +4071,9 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end) | |
| } else if (const_oparg == CONSTANT_BUILTIN_SET) { | ||
| // result is already a set | ||
| } | ||
| else if (const_oparg == CONSTANT_BUILTIN_FROZENSET) { | ||
| ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_BUILD_FROZENSET); | ||
| } | ||
| else { | ||
| ADDOP_LOAD_CONST(c, loc, initial_res); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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.
Can you rename this to something that makes it explicit than it modifies the set.
Maybe
_PySet_Freezeor something like that.