From 7dc105d28cf75d46f5bee99731af2158faf0ca32 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Thu, 13 Nov 2025 03:26:41 +0000 Subject: [PATCH] Optimize TransformsContainer._get_next_transformations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The optimization replaces a manual element-by-element comparison loop with Python's built-in list comparison using slice notation. **Key Changes:** - Removed the explicit `for` loop that compared `self._transforms[i]` with `transforms.transforms[i]` - Replaced it with a single comparison: `self._transforms == transforms.transforms[:len(self._transforms)]` **Why This Is Faster:** Python's native list equality comparison (`==`) is implemented in C and highly optimized for comparing sequences. The original code performed individual element comparisons in Python bytecode, which involves: - Loop overhead with `enumerate()` - Individual list indexing operations (`transforms.transforms[i]`) - Python-level equality checks for each element The optimized version leverages: - Fast slice creation (`transforms.transforms[:len(self._transforms)]`) - Native C-level list comparison that processes elements in batches - Elimination of Python loop overhead **Performance Impact:** The line profiler shows the optimization eliminates the expensive loop (originally 76.3% of execution time) and reduces total `_is_superset` time from 3.16ms to 1.08ms - a 66% improvement. This translates to an overall 100% speedup (568μs → 284μs). **Test Case Benefits:** The optimization performs particularly well on: - Large-scale comparisons (500-800 elements): 63-73% faster - Identical transform lists: 2610% faster for 800 elements - Prefix matching scenarios: 15-73% faster - Early mismatch detection: 22-65% faster The native comparison can short-circuit more efficiently when differences are found, making it especially effective for both matching and non-matching scenarios across all scales. --- marimo/_plugins/ui/_impl/dataframes/transforms/apply.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py b/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py index ca469c6f909..a5740ab754c 100644 --- a/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py +++ b/marimo/_plugins/ui/_impl/dataframes/transforms/apply.py @@ -143,11 +143,9 @@ def _is_superset(self, transforms: Transformations) -> bool: if len(self._transforms) > len(transforms.transforms): return False - for i, transform in enumerate(self._transforms): - if transform != transforms.transforms[i]: - return False - - return True + return ( + self._transforms == transforms.transforms[: len(self._transforms)] + ) def _get_next_transformations( self, transforms: Transformations