Skip to content

Profile tfl_table and add token-width memoization#37

Merged
billdenney merged 1 commit into
mainfrom
claude/determined-feynman-37d83c
May 12, 2026
Merged

Profile tfl_table and add token-width memoization#37
billdenney merged 1 commit into
mainfrom
claude/determined-feynman-37d83c

Conversation

@billdenney
Copy link
Copy Markdown
Member

Profile harness (examples/profile_writetfl.R) identified .measure_text_width_in at wrap.R:160 as 73.7% of total time across the 18-demo wrap_demos sweep. Repeated tokens within paragraphs and across cells were paying the textGrob+grobWidth+convertWidth cost on every measurement.

Add per-call width caches in .wrap_string() (threaded through .wrap_paragraph) and .column_min_token_width_in(), plus a unique() dedup in .measure_max_string_width(). Pattern mirrors the existing memo envs at R/table_rows.R:38 and R/wrap.R:713.

Measured speedup (medians, 15 iterations):
core_wrap 334 ms -> 225 ms (~33% faster)
wrap_demos 9.88 s -> 3.43 s (~65% faster)
core_small / core_paginate / figure_multi: within run-to-run noise

Full devtools::test() green before and after.

Documented as D-43 in design/DECISIONS.md. Profiling and benchmark scripts live under examples/ (already in .Rbuildignore).

Profile harness (examples/profile_writetfl.R) identified
.measure_text_width_in at wrap.R:160 as 73.7% of total time across the
18-demo wrap_demos sweep.  Repeated tokens within paragraphs and across
cells were paying the textGrob+grobWidth+convertWidth cost on every
measurement.

Add per-call width caches in .wrap_string() (threaded through
.wrap_paragraph) and .column_min_token_width_in(), plus a unique() dedup
in .measure_max_string_width().  Pattern mirrors the existing memo envs
at R/table_rows.R:38 and R/wrap.R:713.

Measured speedup (medians, 15 iterations):
  core_wrap     334 ms -> 225 ms  (~33% faster)
  wrap_demos    9.88 s ->  3.43 s (~65% faster)
  core_small / core_paginate / figure_multi: within run-to-run noise

Full devtools::test() green before and after.

Documented as D-43 in design/DECISIONS.md.  Profiling and benchmark
scripts live under examples/ (already in .Rbuildignore).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@billdenney billdenney merged commit 4206e93 into main May 12, 2026
9 checks passed
@billdenney billdenney deleted the claude/determined-feynman-37d83c branch May 12, 2026 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant