faster-cpython / ideas

1.67k stars 49 forks source link

BUILD_LIST should expect items in reverse order #510

Closed lpereira closed 1 year ago

lpereira commented 1 year ago

Is there any reason for the order of the items in the stack that are used by BUILD_LIST be what they are right now?

If items were in the reverse order to what's currently done, then it could be implemented very similarly to BUILD_TUPLE (by stealing the references from the stack), avoiding a lot of stack pointer adjustments while items are being popped.

gvanrossum commented 1 year ago

Good question. I noticed that LIST_APPEND also expects the arguments in reverse order. Maybe related?

markshannon commented 1 year ago

Both list and tuples are evaluated left-to-right (as is all Python code) and are in the same order.

>>> (print(1), print(2))
1
2
(None, None)
>>> [print(1), print(2)]
1
2
[None, None]

BUILD_LIST could be improved in a similar way to BUILD_TUPLE by moving the items en masse.

lpereira commented 1 year ago

I misunderstood the code. Will send a PR making the change to BUILD_LIST, then.

lpereira commented 1 year ago

PR: https://github.com/python/cpython/pull/100147