Skip to content

Commit b926f5d

Browse files
authored
Merge pull request #628 from Fuyukai/instrument-speedup
Add checks for if instruments exist before running instrument functions.
2 parents ac4a028 + ff7bbc8 commit b926f5d

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

trio/_core/_run.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,8 @@ class Runner:
616616
def close(self):
617617
self.io_manager.close()
618618
self.entry_queue.close()
619-
self.instrument("after_run")
619+
if self.instruments:
620+
self.instrument("after_run")
620621

621622
# Methods marked with @_public get converted into functions exported by
622623
# trio.hazmat:
@@ -718,7 +719,8 @@ def reschedule(self, task, next_send=_NO_SEND):
718719
task._abort_func = None
719720
task.custom_sleep_data = None
720721
self.runq.append(task)
721-
self.instrument("task_scheduled", task)
722+
if self.instruments:
723+
self.instrument("task_scheduled", task)
722724

723725
def spawn_impl(self, async_fn, args, nursery, name, *, system_task=False):
724726

@@ -853,7 +855,8 @@ def _return_value_looks_like_wrong_library(value):
853855
for scope in nursery._cancel_stack:
854856
scope._add_task(task)
855857

856-
self.instrument("task_spawned", task)
858+
if self.instruments:
859+
self.instrument("task_spawned", task)
857860
# Special case: normally next_send should be a Result, but for the
858861
# very first send we have to send a literal unboxed None.
859862
self.reschedule(task, None)
@@ -879,7 +882,9 @@ def task_exited(self, task, result):
879882
self.system_nursery.cancel_scope.cancel()
880883
if task is self.init_task:
881884
self.init_task_result = result
882-
self.instrument("task_exited", task)
885+
886+
if self.instruments:
887+
self.instrument("task_exited", task)
883888

884889
################
885890
# System tasks and init
@@ -1087,6 +1092,9 @@ def abort(_):
10871092
################
10881093

10891094
def instrument(self, method_name, *args):
1095+
if not self.instruments:
1096+
return
1097+
10901098
for instrument in list(self.instruments):
10911099
try:
10921100
method = getattr(instrument, method_name)
@@ -1279,7 +1287,8 @@ def run(
12791287
def run_impl(runner, async_fn, args):
12801288
__tracebackhide__ = True
12811289

1282-
runner.instrument("before_run")
1290+
if runner.instruments:
1291+
runner.instrument("before_run")
12831292
runner.clock.start_clock()
12841293
runner.init_task = runner.spawn_impl(
12851294
runner.init,
@@ -1308,9 +1317,13 @@ def run_impl(runner, async_fn, args):
13081317
timeout = cushion
13091318
idle_primed = True
13101319

1311-
runner.instrument("before_io_wait", timeout)
1320+
if runner.instruments:
1321+
runner.instrument("before_io_wait", timeout)
1322+
13121323
runner.io_manager.handle_io(timeout)
1313-
runner.instrument("after_io_wait", timeout)
1324+
1325+
if runner.instruments:
1326+
runner.instrument("after_io_wait", timeout)
13141327

13151328
now = runner.clock.current_time()
13161329
# We process all timeouts in a batch and then notify tasks at the end
@@ -1359,7 +1372,9 @@ def run_impl(runner, async_fn, args):
13591372
while batch:
13601373
task = batch.pop()
13611374
GLOBAL_RUN_CONTEXT.task = task
1362-
runner.instrument("before_task_step", task)
1375+
1376+
if runner.instruments:
1377+
runner.instrument("before_task_step", task)
13631378

13641379
next_send = task._next_send
13651380
task._next_send = None
@@ -1413,7 +1428,8 @@ def run_impl(runner, async_fn, args):
14131428
# and propagate the exception into the task's spawner.
14141429
runner.task_exited(task, Error(exc))
14151430

1416-
runner.instrument("after_task_step", task)
1431+
if runner.instruments:
1432+
runner.instrument("after_task_step", task)
14171433
del GLOBAL_RUN_CONTEXT.task
14181434

14191435
return runner.init_task_result

0 commit comments

Comments
 (0)