-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-131798: JIT: Optimize _CALL_LEN when the length is known #135260
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
base: main
Are you sure you want to change the base?
Conversation
int tuple_length = sym_tuple_length(arg); | ||
if (tuple_length >= 0) { | ||
PyObject *temp = PyLong_FromLong(tuple_length); | ||
if (temp == NULL) { | ||
goto error; | ||
} | ||
if (_Py_IsImmortal(temp)) { | ||
REPLACE_OP(this_instr, _POP_CALL_ONE_LOAD_CONST_INLINE_BORROW, | ||
0, (uintptr_t)temp); | ||
} | ||
res = sym_new_const(ctx, temp); | ||
Py_DECREF(temp); | ||
} |
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.
This is basically copied from the recent _GET_LEN
optimization
Lib/test/test_capi/test_opt.py
Outdated
def testfunc(n): | ||
x = 0 | ||
for _ in range(n): | ||
a = (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
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.
I don't know how else to create a tuple with more than _PY_NSMALLPOSINTS
elements in a way that the optimizer will know the length 😆 (_CALL_TUPLE_1
doesn't have any optimization for instance)
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.
Class attributes are cached as constants, so maybe something like this would work?
class C:
t = tuple(range(300))
x = 0
for _ in range(n):
if len(C.t) == 300:
x += 1
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.
It does, thanks for the tip!
This uses the same principle as #135194 to completely remove
_CALL_LEN
when the length is known.I don't know how common it is to actually know the length exactly so please let me know if this is not worth it, but given that
_CALL_LEN
is the most common builtin call, it felt worth to optimize this case.