cpython icon indicating copy to clipboard operation
cpython copied to clipboard

Major slowdown on large listcomp

Open tim-one opened this issue 1 year ago • 3 comments

Bug report

Bug description:

That started within the last 48 hours, on the main branch. Here's test code; it's possible it could be simplified

if 1:
    class Obj:
        def __init__(self, i):
            self.val = i

    import sys
    print(sys.version)
    from time import perf_counter as now

    start = now()
    xs = [Obj(i) for i in range(2**21)]
    finish = now()
    print(finish - start)

Under 3.12.2 it takes about a second, but under current main about 21 seconds.

C:\Code\Python\PCbuild>py \MyPy\small.py
3.12.2 (tags/v3.12.2:6abddd9, Feb  6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]
0.9562742999987677

C:\Code\Python\PCbuild>amd64\python.exe \MyPy\small.py
3.13.0a5+ (heads/main:f4cc77d494, Mar 20 2024, 20:28:50) [MSC v.1939 64 bit (AMD64)]
20.99507549998816

If the size of the list is increased, it appears to suffer much worse than linear-time slowdown.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs

  • gh-117120

tim-one avatar Mar 21 '24 01:03 tim-one

It looks like the regression is due to 15309329b6 / #116206 cc @markshannon

hauntsaninja avatar Mar 21 '24 02:03 hauntsaninja

Thanks - at least the cause is obvious 😆.,

tim-one avatar Mar 21 '24 02:03 tim-one

Noting that, in the original program that triggered this, disabling gc restored its former speed.

tim-one avatar Mar 21 '24 18:03 tim-one

Has this been fixed by either #117120 or #117213?

pablogsal avatar Mar 26 '24 13:03 pablogsal

It was fixed by #117120

markshannon avatar Mar 26 '24 14:03 markshannon