-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Speed up the creation time of constant list and set literals. #82509
Comments
The attached PR contains a small change to the peephole optimizer that converts sequences of: LOAD_CONST(a), LOAD_CONST(b), ..., BUILD_LIST(n) to LOAD_CONST((a, b, ...)), BUILD_LIST_UNPACK(1) The improvement quickly becomes significant for lists larger than a few items (elements vs speedup): 5: ~5% This can be tested on any version of Python by comparing the performance of "[0, 1, 2, ...]" vs "[*(0, 1, 2, ...)]". The common cases of empty and single-element lists are not affected by this change. This is related to bpo-33325, but that was an invasive change for all collection literals that had an unknown affect on performance. I've limited this one to lists and kept it to a few lines in the peephole optimizer. |
Great! I withdrew the original proposition in bpo-33325 because the part of the optimization was not so good as I expected. But this part is good. $ ./python -m timeit "[$(seq -s, 10)]"
5000000 loops, best of 5: 75.5 nsec per loop
$ ./python -m timeit "[*($(seq -s, 10))]"
5000000 loops, best of 5: 57.2 nsec per loop Would you consider to optimize also creating a set of constants? $ ./python -m timeit "{$(seq -s, 10)}"
2000000 loops, best of 5: 186 nsec per loop
$ ./python -m timeit -s "a = frozenset(($(seq -s, 10)))" "{*a}"
2000000 loops, best of 5: 116 nsec per loop |
Yes, I was thinking about that (and BUILD_CONST_KEY_MAP as well). I'll open another PR with those as soon as this one is in. |
...unless you'd prefer that I add them to this PR. But I think it's a better idea to add and review them separately. |
Okay, but first I want to solve an issue with list overallocation. |
I have created a new patch (PR 17114) that performs this optimization directly in the compiler, rather than the peephole optimizer. I think I like the new one better. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: