Skip to content

GH-131798: Remove JIT guards for dict, frozenset, list, set, and tuple - #132289

Merged
brandtbucher merged 7 commits into
python:mainfrom
brandtbucher:jit-guard-dict-tuple-list
Apr 9, 2025
Merged

GH-131798: Remove JIT guards for dict, frozenset, list, set, and tuple#132289
brandtbucher merged 7 commits into
python:mainfrom
brandtbucher:jit-guard-dict-tuple-list

Conversation

@brandtbucher

@brandtbucherbrandtbucher commented Apr 8, 2025

Copy link
Copy Markdown
Member

According to the stats, this removes:

  • 144 million tuple guards (25%)
  • 280 million set/frozenset guards (19%)
  • 484 million dict guards (42%)
  • 1.3 billion list guards (34%)

It also fixes a bug that I encountered in the optimizer's logic for _UNPACK_SEQUENCE_TUPLE, which puts items on the stack in reverse order.

@brandtbucherbrandtbucher added performance Performance or resource usage interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-JIT labels Apr 8, 2025
@brandtbucherbrandtbucher self-assigned this Apr 8, 2025
@brandtbucher

Copy link
Copy Markdown
MemberAuthor

@Zheaoli and @tomasr8, you might be interested in checking this out. Specifically, see the changes in Python/bytecodes.c which use the same syntax as Python/optimizer_bytecodes.c, but describes the actual implementations of these instructions.

You can see that this PR breaks up the old, larger instr definitions into smaller op definitions combined into a macro. This doesn't change the semantics of the instruction itself, but allows the JIT to remove parts of the instruction as it sees fit (in this case, separating out the type checks allows the JIT to remove them).

@Fidget-SpinnerFidget-Spinner left a comment

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.

Just one concern.

Comment threadLib/test/test_capi/test_opt.py
op(_TO_BOOL_LIST, (value -- res)) {
int already_bool = optimize_to_bool(this_instr, ctx, value, &res);
if (!already_bool) {
sym_set_type(value, &PyList_Type);

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.

Does CONTAIN_OP_SET and CONTAIN_OP_DICT need to remove their sym setting type as well? Or is that not even happening at the moment?

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

Not happening currently,. _CONTAINS_OP_SET can't even set anything, since it handles both set and frozenset (this might be worth re-evaluating, or splitting up).

@Zheaoli

Copy link
Copy Markdown
Contributor

@Zheaoli and @tomasr8, you might be interested in checking this out. Specifically, see the changes in Python/bytecodes.c which use the same syntax as Python/optimizer_bytecodes.c, but describes the actual implementations of these instructions.

You can see that this PR breaks up the old, larger instr definitions into smaller op definitions combined into a macro. This doesn't change the semantics of the instruction itself, but allows the JIT to remove parts of the instruction as it sees fit (in this case, separating out the type checks allows the JIT to remove them).

Thanks! I think I might need some time to understand this PR! Thanks for your patience!

@brandtbucher

Copy link
Copy Markdown
MemberAuthor

Going to go ahead and merge this to reduce the risk of merge conflicts, but let me know if you have any questions @Zheaoli!

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

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetopic-JIT

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@brandtbucher@Zheaoli@Fidget-Spinner