From cce8520efb09517211ea2c6aeb426d4a65bafa67 Mon Sep 17 00:00:00 2001 From: Kegan Dougal <7190048+kegsay@users.noreply.github.com> Date: Thu, 19 Mar 2026 13:45:28 +0000 Subject: [PATCH] Do not do opentracing wrapping if tracing is disabled This shows up in profiles as wasted CPU time. --- synapse/logging/opentracing.py | 9 +++++++++ synapse/util/caches/descriptors.py | 6 ++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/synapse/logging/opentracing.py b/synapse/logging/opentracing.py index 6e4e029163..3037097cb2 100644 --- a/synapse/logging/opentracing.py +++ b/synapse/logging/opentracing.py @@ -943,6 +943,10 @@ def _custom_sync_async_decorator( async def _wrapper( *args: P.args, **kwargs: P.kwargs ) -> Any: # Return type is RInner + # Short-circuit: if opentracing was disabled after decoration time + # (init_tracer sets opentracing=None), skip all wrapping overhead. + if opentracing is None: + return await func(*args, **kwargs) # type: ignore[unreachable] # type-ignore: func() returns R, but mypy doesn't know that R is # Awaitable here. with wrapping_logic(func, *args, **kwargs): # type: ignore[arg-type] @@ -953,6 +957,11 @@ def _custom_sync_async_decorator( # `@defer.inlineCallbacks` or that return a `Deferred` or other `Awaitable`. @wraps(func) def _wrapper(*args: P.args, **kwargs: P.kwargs) -> Any: + # Short-circuit: if opentracing was disabled after decoration time + # (init_tracer sets opentracing=None), skip all wrapping overhead. + if opentracing is None: + return func(*args, **kwargs) # type: ignore[unreachable] + scope = wrapping_logic(func, *args, **kwargs) scope.__enter__() diff --git a/synapse/util/caches/descriptors.py b/synapse/util/caches/descriptors.py index 48c8f8f043..d416ca97ac 100644 --- a/synapse/util/caches/descriptors.py +++ b/synapse/util/caches/descriptors.py @@ -455,9 +455,7 @@ class DeferredCacheListDescriptor(_CacheDescriptorBase): cache_entry.error_bulk(cache, missing, f) args_to_call = dict(arg_dict) - args_to_call[list_name] = { - cache_key_to_arg(key) for key in missing - } + args_to_call[list_name] = {cache_key_to_arg(key) for key in missing} # dispatch the call, and attach the two handlers missing_d = defer.maybeDeferred( @@ -710,4 +708,4 @@ def _get_cache_key_gen( pos += 1 else: if inc: - yield param_defaults[nm] \ No newline at end of file + yield param_defaults[nm]