Skip to content

bpo-38436: Improved performance for list addition. - #16705

Closed
brandtbucher wants to merge 2 commits into
python:masterfrom
brandtbucher:list-add
Closed

bpo-38436: Improved performance for list addition.#16705
brandtbucher wants to merge 2 commits into
python:masterfrom
brandtbucher:list-add

Conversation

@brandtbucher

@brandtbucher brandtbucher commented Oct 10, 2019

Copy link
Copy Markdown
Member

This PR adds a fast path for BINARY_ADD instructions involving two lists, where the left list has a refcount of exactly 1.

In this case, we instead do a PySequence_InPlaceConcat operation. This has the affect of avoiding quadratic complexity for list summations, by keeping only one intermediate result and extending it in-place.

For (potentially large) lists:

a + b + c + d ...

Currently executed as:

tmp = a + b
tmp = tmp + c
tmp = tmp + d
...

With this change:

tmp = a + b
tmp += c
tmp += d
...

Here are the pyperformance results (optimizations enabled):

EDIT: there are better measurements in the BPO discussion.

This is related to my earlier work in bpo-36229, however this is a much less invasive change that's limited to ceval.c, where our knowledge of context is much better.

https://bugs.python.org/issue38436

@pablogsal

Copy link
Copy Markdown
Member

Where the benchmarks executed with CPU isolation? I am a bit suspicious of the

- pickle_list: 3.63 us +- 0.08 us -> 3.78 us +- 0.10 us: 1.04x slower (+4%)

If not, can you repeat them with CPU isolation and affinity? Check this for more info

@brandtbucher

Copy link
Copy Markdown
Member Author

@pablogsal No isolation (I'm on a MacBook) but I can rerun with affinity on two cores.

@pablogsal

pablogsal commented Oct 10, 2019

Copy link
Copy Markdown
Member

@pablogsal No isolation (I'm on a MacBook) but I can rerun with affinity on two cores.

I can try to run the benchmarks on my machine or the speed.python.org server with isolation, as this is a very sensitive operation.

@brandtbucher

Copy link
Copy Markdown
Member Author

Thanks @pablogsal - that would probably be best!

@brandtbucher
brandtbucher deleted the list-add branch July 21, 2022 20:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review performance Performance or resource usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants