Skip to content

gh-119477: Analyze through BUILD_TUPLE and BINARY_SUBSCR_TUPLE_INT#119478

Closed
tekknolagi wants to merge 2 commits intopython:mainfrom
tekknolagi:mb-analyze-through-tuples
Closed

gh-119477: Analyze through BUILD_TUPLE and BINARY_SUBSCR_TUPLE_INT#119478
tekknolagi wants to merge 2 commits intopython:mainfrom
tekknolagi:mb-analyze-through-tuples

Conversation

@tekknolagi
Copy link
Copy Markdown
Contributor

@tekknolagi tekknolagi commented May 23, 2024

Make an symbolic tuple arena so we can allocate arrays of pointers to
other symbols.

Co-authored-by: @Fidget-Spinner

Make an symbolic tuple arena so we can allocate arrays of pointers to
other symbols.
@tekknolagi tekknolagi changed the title Analyze through BUILD_TUPLE and BINARY_SUBSCR_TUPLE_INT gh-119477: Analyze through BUILD_TUPLE and BINARY_SUBSCR_TUPLE_INT May 23, 2024
@tekknolagi tekknolagi marked this pull request as ready for review May 23, 2024 21:26
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.

This increases the size of the stack allocated arena considerably, maybe causing stack overflows.
I've suggested how to address this in the comments.

FYI, We plan to merge BINARY_SUBSCR into BINARY_OP soon.

int flags; // 0 bits: Top; 2 or more bits: Bottom
PyTypeObject *typ; // Borrowed reference
PyObject *const_val; // Owned reference (!)
int tuple_count;
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 after the flags, and it doesn't need 32 bits.

PyTypeObject *typ; // Borrowed reference
PyObject *const_val; // Owned reference (!)
int tuple_count;
struct _Py_UopsSymbol **tuple_val;
Copy link
Copy Markdown
Member

@markshannon markshannon May 24, 2024

Choose a reason for hiding this comment

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

Can you use indexes, rather than pointers? We can fit 6 10-bit indexes into a 64 bit int.
We really don't care about tuples with len() > 6 and we are already using too much stack memory for these data structures.

Would it be possible to use a union, and overlap this field with const_val?

_Py_UopsSymbol arena[TY_ARENA_SIZE];
} ty_arena;

typedef struct tup_arena {
Copy link
Copy Markdown
Member

@markshannon markshannon May 24, 2024

Choose a reason for hiding this comment

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

This shouldn't be necessary, if we use indexes into the arena, instead of pointers.

extern _Py_UopsSymbol *_Py_uop_sym_new_type(
_Py_UOpsContext *ctx, PyTypeObject *typ);
extern _Py_UopsSymbol *_Py_uop_sym_new_tuple(_Py_UOpsContext *ctx, int count);
extern _Py_UopsSymbol *_Py_uop_sym_tuple_at(_Py_UopsSymbol *sym, int idx);
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 use get_item, not at, for consistency with the rest of the code base?
Can you add _Py_uop_sym_tuple_set_item as well?

assert(tup != NULL);
assert(!ctx->out_of_space);
for (int i = 0; i < oparg; i++) {
tup->tuple_val[i] = values[i];
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.

Use sym_tuple_set_item(tup, i, values[i]) for better encapsulation.

op(_BINARY_SUBSCR_TUPLE_INT, (tuple, sub -- res)) {
if (sym_has_type(tuple) && sym_matches_type(tuple, &PyTuple_Type) && sym_is_const(sub) ) {
PyObject* value = sym_get_const(sub);
Py_ssize_t index = ((PyLongObject*)value)->long_value.ob_digit[0];
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.

There is no guarantee that value is an int.

}
}

op(_BINARY_SUBSCR_TUPLE_INT, (tuple, sub -- res)) {
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.

This optimization applies to all BINARY_SUBSCR variants, although it is much more likely to apply here.

@bedevere-app
Copy link
Copy Markdown

bedevere-app Bot commented May 24, 2024

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@markshannon
Copy link
Copy Markdown
Member

@tekknolagi do you plan to continue working on this?

@github-actions
Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 16, 2026
@Fidget-Spinner
Copy link
Copy Markdown
Member

This is done, thanks everyone!

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

Labels

awaiting changes stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants