Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Open
Description
Bug report
Bug description:
Using a trace function with sys.settrace, performance suffers when importing a large dictionary literal. The performance exhibits quadratic time complexity on 3.12 and up. It is fine on 3.11.
This originated with a coverage.py bug report:coveragepy/coveragepy#1906
To reproduce, create make_mapping.py:
"""Create a really large dict to import."""importrandomprint("MAPPING = {")forninrange(25_000):print(f" '{n:08d}':{n},")print("}")
and trace_it.py:
importlinecache,sys,timet0=tlast=time.monotonic_ns()deftrace(frame,event,arg):globaltlastif"mapping.py"inframe.f_code.co_filename:lineno=frame.f_linenonow=time.monotonic_ns()print("{:12d} ({}): {} {:05d}".format(now-t0,now-tlast,event[:4],lineno, ))tlast=nowreturntraceprint(sys.version)sys.settrace(trace)importmapping
Then, create the large mapping, and run trace_it.py on multiple versions, and look at the timings:
python3 make_mapping.py > mapping.pypython3.11 trace_it.py > 11.outpython3.12 trace_it.py > 12.outpython3.13 trace_it.py > 13.outpython3.14 trace_it.py > 14.outgrep 'line \d[05]000' *.outThese are the results I see. The parenthetical numbers are nanoseconds since the last output line:
11.out: 76131833 (1917): line 0500011.out: 86116875 (917): line 1000011.out: 96115250 (917): line 1500011.out: 106099958 (958): line 2000011.out: 116161416 (875): line 2500012.out: 803728958 (148833): line 0500012.out: 3009835500 (293042): line 1000012.out: 6750690750 (444833): line 1500012.out: 11966781625 (596292): line 2000012.out: 18720473667 (739000): line 2500013.out: 443250333 (75125): line 0500013.out: 1565451416 (147583): line 1000013.out: 3447408625 (220625): line 1500013.out: 6130466958 (294458): line 2000013.out: 9455595416 (370500): line 2500014.out: 445441625 (89291): line 0500014.out: 1556189584 (152250): line 1000014.out: 3410512125 (220625): line 1500014.out: 6007919334 (366584): line 2000014.out: 9369414792 (375083): line 25000CPython versions tested on:
3.11, 3.12, 3.13, 3.14
Operating systems tested on:
macOS