Skip to content

Commit cf4dd1e

Browse files
murray-dsclaude
andcommitted
perf(plots): drop the redundant post-bake draw when freezing chrome text wrap
_place_header_text already draws before calling _freeze_text_wrap (that draw is what computes the soft wrap), so the freeze helper's own canvas.draw() was a second full render per header element. The post-bake measurement uses get_window_extent, which re-lays out the baked text from the renderer without needing another draw. Removes the extra draws (header elements + source) with no change in output. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent fe04190 commit cf4dd1e

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

openretailscience/plots/styles/styling_helpers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,20 @@ def _resolve_end_of_line_legend_args(
9595
return False, None, False
9696

9797

98-
def _freeze_text_wrap(text: Text, fig: Figure) -> None:
98+
def _freeze_text_wrap(text: Text) -> None:
9999
"""Bake matplotlib's soft wrap into ``text`` and stop it re-wrapping on later draws.
100100
101101
``wrap=True`` recomputes line breaks every draw from the figure width, so a later resize (or
102102
the extra draw ``bbox_inches='tight'`` triggers) can change the line count and invalidate the
103103
layout positioned against the measured height. Baking the wrapped lines as explicit newlines
104-
and turning wrap off freezes the count. Requires a prior draw so the wrap is computed; no-op
105-
when wrapping is already off.
104+
and turning wrap off freezes the count. Requires a prior draw so the wrap is already computed;
105+
the caller measures the baked text afterwards via ``get_window_extent``, which re-lays it out
106+
without another draw. No-op when wrapping is already off.
106107
"""
107108
if not text.get_wrap():
108109
return
109110
text.set_text(text._get_wrapped_text())
110111
text.set_wrap(False)
111-
fig.canvas.draw()
112112

113113

114114
def _place_header_text(
@@ -141,7 +141,7 @@ def _place_header_text(
141141
)
142142
t.set_gid(gid)
143143
fig.canvas.draw()
144-
_freeze_text_wrap(t, fig)
144+
_freeze_text_wrap(t)
145145
renderer = fig.canvas.get_renderer()
146146
return t.get_window_extent(renderer=renderer).y0 / fig.bbox.height
147147

@@ -504,7 +504,7 @@ def apply_chart_chrome(
504504
fig.canvas.draw()
505505
# Freeze the wrap like the header: an unfrozen source re-wraps to a different line count
506506
# on a width resize, invalidating the axes_bottom reserved here and crowding the axes.
507-
_freeze_text_wrap(source_t, fig)
507+
_freeze_text_wrap(source_t)
508508
source_top_px = source_t.get_window_extent(renderer=fig.canvas.get_renderer()).y1
509509
axes_bottom = source_top_px / fig.bbox.height + _CHROME_GAP_SOURCE_TO_AXES_IN / fig_h
510510

0 commit comments

Comments
 (0)