New submission from Serhiy Storchaka: Since tuples are immutable, concatenating with empty tuple can be optimized by returning an opposite argument.
Microbenchmarks (the difference is larger for larger tuples): $ ./python -m perf timeit --duplicate=100 -s 'a = (1, 2)' 'a + ()' Unpatched: Median +- std dev: 288 ns +- 12 ns Patched: Median +- std dev: 128 ns +- 5 ns $ ./python -m perf timeit --duplicate=100 -s 'a = (1, 2)' '() + a' Unpatched: Median +- std dev: 285 ns +- 16 ns Patched: Median +- std dev: 128 ns +- 6 ns Non-empty tuples are not affected: $ ./python -m perf timeit --duplicate=100 -s 'a = (1, 2)' 'a + a' Unpatched: Median +- std dev: 321 ns +- 24 ns Patched: Median +- std dev: 317 ns +- 26 ns ---------- components: Interpreter Core messages: 289129 nosy: haypo, serhiy.storchaka priority: normal severity: normal stage: patch review status: open title: Optimize concatenating empty tuples type: performance versions: Python 3.7 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29737> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com