From df27931f93cf0f3569e2664bf91492e8985cb8fd Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 11 Nov 2025 22:35:19 +0000 Subject: [PATCH] Optimize get_local_contexts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization eliminates a repeated import cost by moving the `from gradio.context import LocalContext` statement from inside the function to module scope. This simple change provides a **238% speedup** because: **Key Performance Issue**: The original code performs a module import and attribute lookup on every function call. The line profiler shows this import taking 49.8% of the total execution time (1.92ms out of 3.86ms total). **Optimization Applied**: By caching `LocalContext` as `_LocalContext` at module scope, we eliminate the repeated import overhead. The optimized version shows the import cost is completely removed from the function execution path. **Why This Matters**: Looking at the function references, `get_local_contexts()` is called from the `updateable` decorator wrapper, which is applied to component constructors. This means the function executes every time a Gradio component is created or updated, making it a hot path where even microsecond improvements compound significantly. **Performance Characteristics**: The test results show consistent 100-250% speedups across all scenarios: - Basic cases: ~200-250% faster - Edge cases with different context values: ~200-250% faster - Large-scale tests with loops: Up to 249% faster (337μs → 96.6μs) **Impact on Workloads**: Since this function is in the component creation/update path, applications that create many components or frequently update them will see the most benefit. The optimization maintains identical behavior while reducing overhead in what appears to be a frequently-called utility function. The optimization is particularly effective because Python's import mechanism involves dictionary lookups and module loading checks that add significant overhead when repeated thousands of times. --- gradio/component_meta.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/gradio/component_meta.py b/gradio/component_meta.py index 20e08ca344..f5a4ebb2b6 100644 --- a/gradio/component_meta.py +++ b/gradio/component_meta.py @@ -8,6 +8,7 @@ from jinja2 import Template +from gradio.context import LocalContext as _LocalContext from gradio.events import EventListener from gradio.exceptions import ComponentDefinitionError from gradio.utils import no_raise_exception @@ -84,9 +85,11 @@ def {{ event.event_name }}(self, def create_pyi(class_code: str, events: list[EventListener | str]): template = Template(INTERFACE_TEMPLATE) event_template = [ - e - if isinstance(e, EventListener) - else EventListener(event_name=e, event_specific_args=[]) + ( + e + if isinstance(e, EventListener) + else EventListener(event_name=e, event_specific_args=[]) + ) for e in events ] return template.render(events=event_template, contents=class_code) @@ -155,11 +158,10 @@ def create_or_modify_pyi( def get_local_contexts(): - from gradio.context import LocalContext return ( - LocalContext.in_event_listener.get(False), - LocalContext.renderable.get(None) is not None, + _LocalContext.in_event_listener.get(False), + _LocalContext.renderable.get(None) is not None, )