You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
free-threading: Poor scaling with module level objects that are not tracked by the GC #163216
importgcimportsysimportthreadingimporttimeALLOWED_METHODS=frozenset({"GET", "HEAD", "POST", "PUT", "DELETE"})
LOOKUPS=2_000_000deflookup(n):
for_inrange(n):
"POST"inALLOWED_METHODSdefthroughput(nthreads):
"""Total lookups per second over nthreads worker threads."""threads= [threading.Thread(target=lookup, args=(LOOKUPS,))
for_inrange(nthreads)]
start=time.perf_counter()
fortinthreads:
t.start()
fortinthreads:
t.join()
returnnthreads*LOOKUPS/ (time.perf_counter() -start)
defmain():
nthreads=int(sys.argv[1]) iflen(sys.argv) >1else4# Worker threads only, even for 1 thread: the main thread created the# constant, and only other threads use the shared reference count.one=throughput(1)
many=throughput(nthreads)
print(f"1 thread: {one/1e6:6.1f}M lookups/s")
print(f"{nthreads} threads: {many/1e6:6.1f}M lookups/s total"f" = {many/one:.2f}x")
print()
print(f"Expected on a free-threaded build: close to {nthreads}x.")
if__name__=="__main__":
main()
This poor scaling is due to frozenset not being enabled for deferred reference counting. Interestingly it was enabled as part of #142843 and with that PR we get close to 4x scaling here. However two related changes broke that scaling. #140234 ("Do not track frozenset objects with immutables") and #144081 ("No deferred reference counting for untracked")
Those two changes combined re-introduced the overhead of accessing a module level variable for frozensets.
A proposed fix is to enable deferral and tracking module level fields via the the original module level specialisation path. Believe this would retain the origonal intention of the above two fixes whilst maintaining the scaling of module level objects in FT.
Activity