From 3b8d23686b866cbd47ed0b4600e2c2d9c2dd1af7 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 05:36:09 +0000 Subject: [PATCH] Optimize is_zero_gpu_space The optimization replaces `os.getenv("SPACES_ZERO_GPU")` with `os.environ.get("SPACES_ZERO_GPU")`, achieving a **6% speedup** by eliminating one function call layer. **What changed:** - `os.getenv()` internally calls `os.environ.get()`, so using `os.environ.get()` directly removes the intermediate wrapper function call - This reduces the call stack depth from 2 levels to 1 level per invocation **Why it's faster:** - Eliminates function call overhead - each call to `os.getenv()` adds the cost of an additional Python function dispatch - With 4,576 hits in the profiler, this micro-optimization compounds significantly - Per-hit time improved from 2,501.1ns to 2,277.3ns (about 224ns saved per call) **Impact on workloads:** The function reference shows `is_zero_gpu_space()` is called in `Blocks.queue()` - a core Gradio component setup method. This means the optimization benefits: - Every Gradio app initialization that uses queueing - Zero GPU space detection during app startup/configuration - The improvement is most noticeable when this function is called frequently, as shown in test cases with loops (5-6% improvement in bulk operations) **Test case performance:** The optimization shows consistent 15-28% improvements across individual test cases, with the best gains when the environment variable is unset (17-20% faster) or set to non-"true" values. Large-scale tests with 1000+ iterations show 5-6% improvements, indicating the optimization scales well with usage frequency. --- gradio/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradio/utils.py b/gradio/utils.py index bccb877289..0745ee414c 100644 --- a/gradio/utils.py +++ b/gradio/utils.py @@ -469,7 +469,7 @@ def get_space() -> str | None: def is_zero_gpu_space() -> bool: - return os.getenv("SPACES_ZERO_GPU") == "true" + return os.environ.get("SPACES_ZERO_GPU") == "true" def download_if_url(article: str) -> str: