diff --git a/mercurial/worker.py b/mercurial/worker.py index 69ed2cff42775dc3855df702342d0a31921d5dc2_bWVyY3VyaWFsL3dvcmtlci5weQ==..ef3838a47503d125a45bd1dfb675f36ead62cb05_bWVyY3VyaWFsL3dvcmtlci5weQ== 100644 --- a/mercurial/worker.py +++ b/mercurial/worker.py @@ -63,5 +63,9 @@ if pycompat.isposix or pycompat.iswindows: _STARTUP_COST = 0.01 + # The Windows worker is thread based. If tasks are CPU bound, threads + # in the presence of the GIL result in excessive context switching and + # this overhead can slow down execution. + _DISALLOW_THREAD_UNSAFE = pycompat.iswindows else: _STARTUP_COST = 1e30 @@ -66,3 +70,4 @@ else: _STARTUP_COST = 1e30 + _DISALLOW_THREAD_UNSAFE = False @@ -68,4 +73,4 @@ -def worthwhile(ui, costperop, nops): +def worthwhile(ui, costperop, nops, threadsafe=True): '''try to determine whether the benefit of multiple processes can outweigh the cost of starting them''' @@ -70,7 +75,11 @@ '''try to determine whether the benefit of multiple processes can outweigh the cost of starting them''' + + if not threadsafe and _DISALLOW_THREAD_UNSAFE: + return False + linear = costperop * nops workers = _numworkers(ui) benefit = linear - (_STARTUP_COST * workers + linear / workers) return benefit >= 0.15 @@ -72,9 +81,9 @@ linear = costperop * nops workers = _numworkers(ui) benefit = linear - (_STARTUP_COST * workers + linear / workers) return benefit >= 0.15 -def worker(ui, costperarg, func, staticargs, args): +def worker(ui, costperarg, func, staticargs, args, threadsafe=True): '''run a function, possibly in parallel in multiple worker processes. @@ -88,5 +97,9 @@ args - arguments to split into chunks, to pass to individual workers + + threadsafe - whether work items are thread safe and can be executed using + a thread-based worker. Should be disabled for CPU heavy tasks that don't + release the GIL. ''' enabled = ui.configbool('worker', 'enabled') @@ -91,6 +104,6 @@ ''' enabled = ui.configbool('worker', 'enabled') - if enabled and worthwhile(ui, costperarg, len(args)): + if enabled and worthwhile(ui, costperarg, len(args), threadsafe=threadsafe): return _platformworker(ui, func, staticargs, args) return func(*staticargs + (args,)) diff --git a/tests/test-simple-update.t b/tests/test-simple-update.t index 69ed2cff42775dc3855df702342d0a31921d5dc2_dGVzdHMvdGVzdC1zaW1wbGUtdXBkYXRlLnQ=..ef3838a47503d125a45bd1dfb675f36ead62cb05_dGVzdHMvdGVzdC1zaW1wbGUtdXBkYXRlLnQ= 100644 --- a/tests/test-simple-update.t +++ b/tests/test-simple-update.t @@ -65,7 +65,7 @@ $ cat <<EOF > forceworker.py > from mercurial import extensions, worker - > def nocost(orig, ui, costperop, nops): + > def nocost(orig, ui, costperop, nops, threadsafe=True): > return worker._numworkers(ui) > 1 > def uisetup(ui): > extensions.wrapfunction(worker, 'worthwhile', nocost)