This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: sum() relies on C signed overflow behaviour
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8, Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, miss-islington, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-05-04 05:54 by serhiy.storchaka, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13080 merged serhiy.storchaka, 2019-05-04 05:57
PR 13262 merged miss-islington, 2019-05-12 09:18
Messages (5)
msg341374 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-04 05:54
sum() assumes that an arithmetic operation on signed longs will wrap modulo 2**(bits_in_long) on overflow.  However, signed overflow causes undefined behaviour according  to the C standards (e.g., C99 6.5, para. 5), and gcc is known to assume that signed overflow never occurs in correct code, and to make use of this assumption when optimizing.

See also issue7406.
msg341384 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-04 13:52
I tested few cases (all positive, all negative, mixed), and did not found any performance difference after this change.

./python -m perf timeit -s "a = list(range(10**4))" -- "sum(a)"
./python -m perf timeit -s "a = [-i for i in range(10**4)]" -- "sum(a)"
./python -m perf timeit -s "a = [i*(-1)**i for i in range(10**4)]" -- "sum(a)"
msg341430 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-05-05 11:26
New changeset 29500737d45cbca9604d9ce845fb2acc3f531401 by Serhiy Storchaka in branch 'master':
bpo-36791: Safer detection of integer overflow in sum(). (GH-13080)
msg342248 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-05-12 09:12
Can this be closed, or does the fix need to be backported?
msg342250 - (view) Author: miss-islington (miss-islington) Date: 2019-05-12 09:37
New changeset b7e483b6d07081d5f81860258e95785975a7cbf8 by Miss Islington (bot) in branch '3.7':
bpo-36791: Safer detection of integer overflow in sum(). (GH-13080)
Date User Action Args
2022-04-11 14:59:14adminsetgithub: 80972
2019-05-12 09:39:22serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-12 09:37:19miss-islingtonsetnosy: + miss-islington
messages: + msg342250
2019-05-12 09:18:10miss-islingtonsetpull_requests: + pull_request13172
2019-05-12 09:12:33mark.dickinsonsetmessages: + msg342248
2019-05-05 11:26:27serhiy.storchakasetmessages: + msg341430
2019-05-04 13:52:41serhiy.storchakasetmessages: + msg341384
2019-05-04 05:57:28serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request12995
2019-05-04 05:54:49serhiy.storchakacreate