Skip to content

Speed up test_weakref in the free-threaded build #120065

@colesbury

Description

@colesbury

The test_weakref test is one of the slowest tests when running in the free-threaded build.

The problem is the default period for the collect_in_thread() function is too short (100 µs). This isn't too much of a problem for the default build because the GIL switch interval is 5 ms, which effectively makes the smaller period irrelevant.

With the free-threaded build, this the 100 µs period means that the test spends the majority of its time calling gc.collect() nearly non-stop.

We should increase the period to 5 ms or so. On my machine, this decreases up the overall test_weakref time from 1 minute to 8 seconds.

@contextlib.contextmanager
def collect_in_thread(period=0.0001):
"""
Ensure GC collections happen in a different thread, at a high frequency.
"""
please_stop = False
def collect():
while not please_stop:
time.sleep(period)
gc.collect()
with support.disable_gc():
t = threading.Thread(target=collect)
t.start()
try:
yield
finally:
please_stop = True
t.join()

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions