From 57d46d6fe1fa6f394c35e255a01fabd18d17584f Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 28 Oct 2025 22:52:21 +0000 Subject: [PATCH 1/3] Optimize _pop_renderer_args The optimization replaces the original's `kwargs.pop('source', ColumnDataSource())` pattern with an explicit conditional check. In the original code, `pop()` with a default argument **always** constructs the `ColumnDataSource()` object, even when 'source' exists in kwargs. The optimized version only creates the expensive `ColumnDataSource()` object when 'source' is actually missing. From the line profiler, the original code spent 99.4% of its time (25.2ms) on the `kwargs.pop('source', ColumnDataSource())` line, while the optimized version only spends 98.2% on `ColumnDataSource()` creation but with significantly less total time (8.8ms) because it's only called when needed. The test results show this optimization is most effective when: - **'source' is present in kwargs** (10,000-20,000% speedup in many tests) - **Large datasets or complex scenarios** where avoiding unnecessary object creation matters most - **Cases without 'source'** see minimal improvement since `ColumnDataSource()` still gets called The 172% speedup comes from eliminating wasteful object construction - a common Python performance anti-pattern where default arguments in `pop()` are eagerly evaluated regardless of necessity. --- src/bokeh/plotting/_renderer.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bokeh/plotting/_renderer.py b/src/bokeh/plotting/_renderer.py index 214f745f6ac..4041459189a 100644 --- a/src/bokeh/plotting/_renderer.py +++ b/src/bokeh/plotting/_renderer.py @@ -10,7 +10,10 @@ #----------------------------------------------------------------------------- from __future__ import annotations +from bokeh.models import ColumnDataSource + import logging # isort:skip + log = logging.getLogger(__name__) #----------------------------------------------------------------------------- @@ -255,7 +258,10 @@ def _convert_data_source(kwargs: Attrs) -> bool: def _pop_renderer_args(kwargs: Attrs) -> Attrs: result = {attr: kwargs.pop(attr) for attr in RENDERER_ARGS if attr in kwargs} - result['data_source'] = kwargs.pop('source', ColumnDataSource()) + if 'source' in kwargs: + result['data_source'] = kwargs.pop('source') + else: + result['data_source'] = ColumnDataSource() return result def _process_sequence_literals(glyphclass: type[Glyph], kwargs: Attrs, source: ColumnarDataSource, is_user_source: bool) -> list[str]: From 641ef0ec6a7bbe233f13a93bff8bda67cb91ce4e Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Fri, 7 Nov 2025 00:24:00 -0800 Subject: [PATCH 2/3] cleanup Signed-off-by: Saurabh Misra --- src/bokeh/plotting/_renderer.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/bokeh/plotting/_renderer.py b/src/bokeh/plotting/_renderer.py index 4041459189a..e00e72c7926 100644 --- a/src/bokeh/plotting/_renderer.py +++ b/src/bokeh/plotting/_renderer.py @@ -10,10 +10,7 @@ #----------------------------------------------------------------------------- from __future__ import annotations -from bokeh.models import ColumnDataSource - import logging # isort:skip - log = logging.getLogger(__name__) #----------------------------------------------------------------------------- From 532fd98811e371fdf506459f50c6641f9a34518e Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Fri, 7 Nov 2025 11:40:56 -0800 Subject: [PATCH 3/3] cleanup Signed-off-by: Saurabh Misra --- src/bokeh/plotting/_renderer.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/bokeh/plotting/_renderer.py b/src/bokeh/plotting/_renderer.py index e00e72c7926..d223ffcd7b8 100644 --- a/src/bokeh/plotting/_renderer.py +++ b/src/bokeh/plotting/_renderer.py @@ -255,10 +255,7 @@ def _convert_data_source(kwargs: Attrs) -> bool: def _pop_renderer_args(kwargs: Attrs) -> Attrs: result = {attr: kwargs.pop(attr) for attr in RENDERER_ARGS if attr in kwargs} - if 'source' in kwargs: - result['data_source'] = kwargs.pop('source') - else: - result['data_source'] = ColumnDataSource() + result['data_source'] = kwargs.pop('source') if 'source' in kwargs else ColumnDataSource() return result def _process_sequence_literals(glyphclass: type[Glyph], kwargs: Attrs, source: ColumnarDataSource, is_user_source: bool) -> list[str]: