Repository navigation
Conversation
Member
Author
|
@DinoV @nascheme @daanx (For mimalloc upstream report) Since mimalloc 2.5.2, abandoned segments of all sub-processes are tracked in one shared arena bitmap, so a reclaim in one sub-process can temporarily clear the bit of a segment that belongs to another sub-process. A full visit read that bitmap before taking abandoned_visit_lock, so it could see a temporarily cleared bit and skip that segment. As a downstream change, a full visit now takes the lock before reading the bitmap. |
Member
Author
|
I also reproduced the reported issue and confirmed that this fix resolves it. |
Member
Author
|
In my local benchmark, no overhead is observed in free-threaded multithreading and multiple subinterpreters. (only noisy) import pyperf
from concurrent import interpreters
CODE = """
import collections, gc, threading, time
gc.disable()
window = collections.deque(maxlen=8)
def churn():
t = threading.Thread(target=lambda: window.append([{} for _ in range(2000)]))
t.start()
t.join()
for _ in range(8):
churn()
op = gc.collect if role == "gc" else churn
ready.put(None)
if background:
while stop.empty():
op()
else:
while (loops := cmd.get()) is not None:
t0 = time.perf_counter()
for _ in range(loops):
op()
out.put(time.perf_counter() - t0)
"""
class Pool:
def __init__(self, roles, bg_roles):
self.ready, self.out, self.stop = (
interpreters.create_queue() for _ in range(3))
self.cmds, self.threads = [], []
for role, background in ([(r, False) for r in roles]
+ [(r, True) for r in bg_roles]):
cmd = interpreters.create_queue()
interp = interpreters.create()
interp.prepare_main(role=role, background=background, cmd=cmd,
ready=self.ready, out=self.out, stop=self.stop)
thread = threading.Thread(target=interp.exec, args=(CODE,))
thread.start()
self.threads.append(thread)
if not background:
self.cmds.append(cmd)
for _ in self.threads:
self.ready.get()
def run(self, loops):
for cmd in self.cmds:
cmd.put(loops)
return sum(self.out.get() for _ in self.cmds) / len(self.cmds)
def close(self):
self.stop.put(None)
for cmd in self.cmds:
cmd.put(None)
for thread in self.threads:
thread.join()
pool = None
def bench(loops, roles, bg_roles):
global pool
if pool is None:
pool = Pool(roles, bg_roles)
return pool.run(loops)
if __name__ == "__main__":
runner = pyperf.Runner()
try:
# name, measured interpreters, background interpreters
runner.bench_time_func("gc_collect_1interp", bench, ["gc"], [])
runner.bench_time_func("gc_collect_4interp", bench, ["gc"] * 4, [])
runner.bench_time_func("thread_churn", bench, ["churn"] * 2, [])
runner.bench_time_func("thread_churn_gc_loop", bench, ["churn"] * 2, ["gc"])
finally:
if pool is not None:
pool.close()import collections
import gc
import threading
import time
import pyperf
window = collections.deque(maxlen=8)
stop = threading.Event()
background = []
def churn():
t = threading.Thread(target=lambda: window.append([{} for _ in range(2000)]))
t.start()
t.join()
def run_until_stopped(op):
while not stop.is_set():
op()
def bench(loops, op, bg_ops):
if not window:
gc.disable()
for _ in range(8):
churn()
for bg_op in bg_ops:
thread = threading.Thread(target=run_until_stopped, args=(bg_op,))
thread.start()
background.append(thread)
t0 = time.perf_counter()
for _ in range(loops):
op()
return time.perf_counter() - t0
if __name__ == "__main__":
runner = pyperf.Runner()
try:
# name, measured operation, background threads
runner.bench_time_func("gc_collect", bench, gc.collect, [])
runner.bench_time_func("gc_collect_churn", bench, gc.collect, [churn] * 2)
runner.bench_time_func("thread_churn", bench, churn, [])
runner.bench_time_func("thread_churn_gc_loop", bench, churn, [gc.collect])
finally:
stop.set()
for thread in background:
thread.join() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.