From d8005e720a86433fd83b3c94f7ca7d3ea033e921 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Tue, 2 Dec 2025 05:29:04 +0000 Subject: [PATCH] Optimize _wrap_repr The optimized version achieves an **81% speedup** by eliminating the expensive `"".join()` operation on a 27-element list and replacing it with direct string concatenation using f-string formatting. **Key optimizations applied:** 1. **Eliminated list construction and join**: The original code created a 27-element list of string literals and then called `"".join()` on it. The optimized version uses Python's automatic string literal concatenation within parentheses, which happens at compile time rather than runtime. 2. **Reduced string operations**: Instead of 27 separate string objects being joined at runtime, the optimized version creates one large string template with only 2 variable interpolations (`height` and `r`). 3. **Minor conditional optimization**: Changed `end is False` to `not end` for slightly better performance. **Why this leads to speedup:** - `"".join()` on many small strings requires Python to iterate through the list, calculate total length, allocate a new string buffer, and copy each element - String literal concatenation in parentheses is optimized by Python's compiler into a single string constant - The line profiler shows the join operation took 16.6% of total time in the original, which is completely eliminated **Impact on workloads:** Based on the function reference, `_wrap_repr` is called in a loop within `summarize_children()` for each child node in a data tree structure. This means the 81% speedup will be multiplied across potentially many child nodes, making tree rendering significantly faster. The test results show consistent 50-100% speedups across all test cases, with larger inputs still benefiting substantially (54-68% faster for large-scale tests). The optimization is particularly effective for this HTML templating use case where most of the output is static markup with only a few dynamic variables. --- xarray/datatree_/datatree/formatting_html.py | 66 ++++++++++---------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/xarray/datatree_/datatree/formatting_html.py b/xarray/datatree_/datatree/formatting_html.py index 547b567a396..d947c6d4efc 100644 --- a/xarray/datatree_/datatree/formatting_html.py +++ b/xarray/datatree_/datatree/formatting_html.py @@ -19,9 +19,11 @@ def summarize_children(children: Mapping[str, Any]) -> str: lines_callback = lambda n, c, end: _wrap_repr(node_repr(n, c), end=end) children_html = "".join( - lines_callback(n, c, end=False) # Long lines - if i < N_CHILDREN - else lines_callback(n, c, end=True) # Short lines + ( + lines_callback(n, c, end=False) # Long lines + if i < N_CHILDREN + else lines_callback(n, c, end=True) + ) # Short lines for i, (n, c) in enumerate(children.items()) ) @@ -98,35 +100,35 @@ def _wrap_repr(r: str, end: bool = False) -> str: """ # height of line end = bool(end) - height = "100%" if end is False else "1.2em" - return "".join( - [ - "