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.

classification
Title: list.count performance regression
Type: performance Stage: resolved
Components: Versions: Python 3.9, Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: corona10 Nosy List: corona10, methane, miss-islington, pablogsal, tekknolagi, vstinner
Priority: normal Keywords: patch

Created on 2020-01-22 17:16 by corona10, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 18119 merged corona10, 2020-01-22 17:17
PR 18120 merged miss-islington, 2020-01-22 17:37
PR 18121 merged miss-islington, 2020-01-22 17:37
PR 18130 closed vstinner, 2020-01-22 22:12
PR 30036 closed tekknolagi, 2021-12-10 18:34
Messages (8)
msg360488 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2020-01-22 17:16
./python.exe -m pyperf timeit -s 'a = [1]*100'  'a.count(1)'

Current Master: Mean +- std dev: 1.05 us +- 0.03 us
My patch: Mean +- std dev: 423 ns +- 11 ns

This is the side-effect of pr 17022.
msg360491 - (view) Author: miss-islington (miss-islington) Date: 2020-01-22 17:36
New changeset 14d80d0b605d8b148e14458e4c1853a940071462 by Miss Islington (bot) (Dong-hee Na) in branch 'master':
bpo-39425: Fix list.count performance regression (GH-18119)
https://github.com/python/cpython/commit/14d80d0b605d8b148e14458e4c1853a940071462
msg360492 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-01-22 18:11
New changeset fdb21609d944941f0732df72dc3d07a7a9a7efea by Pablo Galindo (Miss Islington (bot)) in branch '3.8':
bpo-39425: Fix list.count performance regression (GH-18119) (GH-18120)
https://github.com/python/cpython/commit/fdb21609d944941f0732df72dc3d07a7a9a7efea
msg360493 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-01-22 18:11
New changeset 9e06d61af30bac4fcacb7973f826147ccc010392 by Pablo Galindo (Miss Islington (bot)) in branch '3.7':
bpo-39425: Fix list.count performance regression (GH-18119) (GH-18121)
https://github.com/python/cpython/commit/9e06d61af30bac4fcacb7973f826147ccc010392
msg360494 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-01-22 18:11
Thanks for the PR, Dong-hee Na :)
msg360507 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-22 21:43
The following result is a little bit surprising:

>>> nan=float("nan"); ([nan]*5).count(nan)
5
>>> nan == nan
False

But in fact, the optimization doesn't change the value. It was already 5 previously.

In fact, PyObject_RichCompareBool() has a fast path if the two object pointers are equal:

    /* Quick result when objects are the same.
       Guarantees that identity implies equality. */
    if (v == w) {
        if (op == Py_EQ)
            return 1;
        else if (op == Py_NE)
            return 0;
    }

In short, the optimization is good: thank you ;-)
msg360508 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-01-22 21:45
> In fact, PyObject_RichCompareBool() has a fast path if the two object pointers are equal

It's documented:

https://docs.python.org/dev/c-api/object.html?highlight=pyobject_richcomparebool#c.PyObject_RichCompareBool
msg360510 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2020-01-22 21:50
> The following result is a little bit surprising:

haha, I went to exactly the same journey when reviewing this PR :)
History
Date User Action Args
2022-04-11 14:59:25adminsetgithub: 83606
2021-12-10 18:34:29tekknolagisetnosy: + tekknolagi

pull_requests: + pull_request28262
2020-01-22 22:12:38vstinnersetpull_requests: + pull_request17516
2020-01-22 21:50:20pablogsalsetmessages: + msg360510
2020-01-22 21:45:44vstinnersetmessages: + msg360508
2020-01-22 21:43:36vstinnersetmessages: + msg360507
2020-01-22 18:11:58pablogsalsetstatus: open -> closed
resolution: fixed
messages: + msg360494

stage: patch review -> resolved
2020-01-22 18:11:35pablogsalsetmessages: + msg360493
2020-01-22 18:11:25pablogsalsetmessages: + msg360492
2020-01-22 17:37:28miss-islingtonsetpull_requests: + pull_request17508
2020-01-22 17:37:18miss-islingtonsetpull_requests: + pull_request17507
2020-01-22 17:36:57miss-islingtonsetnosy: + miss-islington
messages: + msg360491
2020-01-22 17:35:01corona10setversions: + Python 3.7
2020-01-22 17:17:47corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request17506
2020-01-22 17:17:30corona10setnosy: + vstinner, methane, pablogsal
2020-01-22 17:16:03corona10create