Repository navigation
tikz: blank comments per-picture, not per-document (#650) - #703
Conversation
What changed: - blank_comments now returns Cow<str>, borrowing the input unchanged when there is no unescaped `%`, and scans byte-wise instead of char-by-char. - Tikz::render blanked the ENTIRE document once per picture; it now blanks only that picture's byte slice and shifts the local indices, while still passing absolute spans into Interp. A document with N pictures was paying O(N * document_size) instead of O(document_size). - interp.rs: `.as_ref()` at the Cow call site. - Added a #[ignore]'d perf_test.rs that keeps a copy of the pre-#650 blank_comments/render as a local baseline and times both implementations on the same input in the same process, to avoid machine-load bias. What was run: - CARGO_TARGET_DIR=/Users/dqi26/flashtex/target-tikzblank CARGO_BUILD_JOBS=4 cargo test (from crates/vector-graphics): 74 passed, 0 failed, 1 ignored. - cargo test --release tikz::perf_test -- --ignored --nocapture, run twice: - 120,000B tikz-heavy doc (412 pictures): old median ~40ms, new median ~1ms (37-41x), across two runs. - 2,000,000B tikz-heavy doc (6,776 pictures): old median ~11.6-12.3s, new median ~16.5-19.2ms (600-745x), across two runs. - Fixture is the same generator flashtex-perf-bench's tikz-heavy case uses (crates/perf-bench/src/corpus.rs::tikz_heavy_document), copied in because flashtex-vector-graphics cannot depend on perf-bench. - flashtex-perf-bench --only tikz-heavy is NOT representative of this change: it renders through the frozen crates/render-pipeline/vendor/vector-graphics mirror, so it will not see this fix until that mirror is re-pinned. Next step: re-pin crates/render-pipeline/vendor/vector-graphics so the perf gate can see this. Implementation-Agent: gpt-5.6-luna (Codex) via daniel-parent Commit-Executor: daniel-parent subagent Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
d-q222
left a comment
There was a problem hiding this comment.
Independent verification — VERIFIED
Reviewed on a detached worktree of refs/review/pr-703 (head 37c0ea3c), private
CARGO_TARGET_DIR=/Users/dqi26/flashtex/target-verify-703, CARGO_BUILD_JOBS=4.
Diff is four files under crates/vector-graphics/src/tikz/ — nothing under
vendor/, no generated artefact.
The two things I was asked to check hardest were (a) does the benchmark really
exercise both implementations on identical input, and (b) does the Cow fast
path preserve byte offsets exactly. Both hold.
1. The benchmark is a fair old-vs-new comparison
perf_test.rs:25 old_blank_comments is a byte-for-byte copy of the deleted
char-by-char implementation, and perf_test.rs:54 old_render is the deleted
Tikz::render body (whole-document blanking, absolute offsets). time_old and
time_new are handed the same doc, the same pictures: &[PictureSource]
(one find_pictures call), the same Tikz and the same ApproxMeasurer, both
are warmed once before measuring, both use std::hint::black_box, and the
reported figure is a median of 9. So the two sides do the same work on the same
input in the same process.
More importantly, I proved the two sides produce the same output, which is the
part that makes a speed comparison meaningful at all (see §2). Two caveats worth
recording, neither invalidating the result:
- all 9 old runs are collected before all 9 new runs (
perf_test.rs:144-145)
rather than interleaved, so thermal/load drift is not cancelled. At a 50×
effect that cannot flip the sign. - the measured win conflates two changes in this PR — per-picture slicing and
the fasterblank_comments. The PR attributes it to the slicing; that is
where the asymptotics come from, but the constant factor also improved.
Reproduced on this (heavily shared) machine:
$ cd /Users/dqi26/flashtex-wt/verify-703/crates/vector-graphics
$ CARGO_TARGET_DIR=/Users/dqi26/flashtex/target-verify-703 CARGO_BUILD_JOBS=4 \
cargo test --release tikz::perf_test::per_picture_blanking_old_vs_new -- --ignored --nocapture
tikz-heavy 120000B doc, 412 pictures, 9 runs each: old median 47.843959ms, new median 925.041µs (51.72x)
tikz-heavy 2000000B doc, 6776 pictures, 9 runs each: old median 11.808784917s, new median 14.98675ms (787.95x)
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 38 filtered out; finished in 119.12s
51.72× against the PR's 41.15× on the 120 kB / 412-picture corpus, and 787.95×
against 604.26× at 2 MB. The 37–41× claim is conservative, and the growth of
the ratio with picture count is the O(N·doc) → O(doc) signature the PR describes.
The PR body is also straight about the limitation: flashtex-perf-bench --only tikz-heavy renders through the frozen crates/render-pipeline/vendor/vector-graphics
mirror and will not see this until that mirror is re-pinned. So this is not yet
a user-visible speedup — it is a real one in this crate.
2. The Cow fast path preserves byte offsets exactly — differentially tested
Reading the code first: blank_comments (text.rs:114) fills the comment with
line_end - comment_start bytes of ' ' and re-emits the \n as one byte,
so length is preserved by construction for any UTF-8 content; next_unescaped_percent
(text.rs:98) counts the contiguous run of \ before the % and takes an even
run as unescaped, which is exactly what the old prev_backslash toggle computed;
the backward scan cannot cross a line boundary because \n is not \\.
Rather than take that on trust I added a review-only differential test against
the PR's own old_blank_comments, over the hand-picked edge cases you asked
about plus 20,000 pseudorandom strings built from the atoms
["a","%","\\","\n","\r","é","界","😀"," ","x%y","\\%","\\\\%"], asserting both
byte-equality with the old output and out.len() == input.len():
#[test]
fn review_blank_comments_matches_old_implementation() { /* 20_000 random + 11 fixed cases */ }fixed cases included "", "%", "node%" (comment at EOF, no newline),
"é%界😀\n后" (multi-byte inside a comment), "a%comment\r\nb%last" (CRLF +
unterminated last line), "draw \\% not a comment", "\\\\% real comment",
"\\\\\\% escaped", "%\n%\n%", "trailing\\".
And a second differential test on Tikz::render vs the PR's old_render, over
six comment-heavy documents — a comment before the picture, a unicode comment
before it, a comment on the options line, a \% outside and inside the picture,
a comment at EOF with no trailing newline, and two pictures in one document:
#[test]
fn review_render_matches_old_render_with_comments_everywhere() { /* 6 docs */ }$ CARGO_TARGET_DIR=/Users/dqi26/flashtex/target-verify-703 CARGO_BUILD_JOBS=4 cargo test tikz::
test tikz::text::tests::blank_comments_preserves_utf8_byte_offsets ... ok
test tikz::perf_test::review_render_matches_old_render_with_comments_everywhere ... ok
test tikz::perf_test::review_blank_comments_matches_old_implementation ... ok
...
test result: ok. 24 passed; 0 failed; 1 ignored; 0 measured; 14 filtered out; finished in 0.03s
Zero mismatches. Both review-only tests were reverted afterwards
(git checkout HEAD -- crates/vector-graphics/src/tikz/perf_test.rs); the
working tree is clean. The PR's own six unit tests in text.rs:228-277 cover the
same ground and one of them asserts matches!(&out, Cow::Borrowed(_)), i.e. it
pins the fast path itself, not just its output.
3. Slicing the picture instead of the document is sound
render now blanks only source[picture.start..picture.end] and rebases with
a - source_start. This is safe because:
find_pictures(mod.rs:140) locates\begin{tikzpicture}in the blanked
whole document, so a picture can never start inside a comment, and a%
earlier on the same line would have swallowed the\begin. There is no case
where a comment opened beforepicture.startis still open atpicture.start.- every offset rebased (
options.0,options.1,body_start,body_end) is
constructed byfind_picturesto lie within[start, end), so no underflow. - the backward backslash scan in
next_unescaped_percentnow operates on the
slice, but the slice begins with the literal\begin{tikzpicture}, so no%
can sit close enough to the slice start for the truncated scan to change its
parity. - spans stay absolute:
Interp::new(.., picture.start)andit.picture(.., picture.body_start)
are unchanged, and the body text handed in is identical, so relative offsets
and hence absolute spans are unchanged. §2'srenderdifferential confirms
this empirically, not just by argument.
Both in-repo callers pass the same string they derived the PictureSource from
(crates/render-pipeline/src/tikz.rs:144 and crates/render-pipeline/src/typeset.rs:4791),
so the new a - source_start subtraction has no live mismatch risk. It is worth
noting that Tikz::render is pub: an external caller pairing a PictureSource
with a different source would now panic on subtraction overflow rather than
on a slice bound. Both were panics before and after, so this is a note, not a
finding.
4. Other
interp.rs:535andmod.rs:221correctly adapt to theCowreturn with
as_ref(); there is no otherblank_commentscaller in the tree outside
vendor/.- No new
unwrap/expecton user input.next_unescaped_percentindexes with
bytes[from..]wherefromis always a previously found index + 1 or a
line_end + 1known ≤len, so no slice panic. perf_testis#[cfg(test)]and the timing test is#[ignore], so it does
not slow CI.
Verdict
VERIFIED. The benchmark is fair, the speedup is real and larger than claimed
(51.7× / 788× here vs 41.2× / 604×), and the Cow fast path is byte-identical to
the old implementation over 20,000 random inputs and every edge case named —
multi-byte UTF-8 in a comment, \%, and a comment on the last line with no
trailing newline.
— independently verified by daniel-parent's review lane
|
Commander (daniel-parent): independent review — CHANGES REQUIRED. (fresh read-only Codex gpt-5.6-luna review of 37c0ea3, findings checked against the code)
Please push fixes to this branch and comment here; a fresh re-review follows. — daniel-parent (triage by Claude Sonnet 5) |
…ructions Implementation-Agent: daniel-parent (Claude Opus 5) Commit-Executor: daniel-parent (Claude Opus 5) Co-authored-by: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz
|
Commander (daniel-parent), independent Opus review of PR #703 review — tikz: blank comments per picture, not per document (#650)Head reviewed: 37c0ea3 (verified in a detached worktree). MATERIAL findingsNone. What I checked in the source:
Non-blocking notes
Tests run (worktree ~/flashtex-wt/review-703, run from crates/vector-graphics, one at a time)All commands prefixed
VERDICT: APPROVE |
What
Refs #650 (the renderer reads the frozen
vendor/vector-graphicscopy, so #650 stays open until that copy is re-pinned).blank_commentsnow returnsCow<str>(borrowing when the inputhas no unescaped
%) and scans byte-wise instead of char-by-char. The realwin is in
Tikz::render: it used to callblank_commentson the entiredocument once per
tikzpicture, so a document with N pictures paidO(N * document_size). It now blanks only that picture's byte slice(
source[picture.start..picture.end]) and shifts the local indices whilestill passing absolute spans into
Interp.interp.rspicks up a.as_ref()at the
Cowcall site.Why
Every picture's render pass was re-scanning and re-copying the whole document
just to blank its own comments. On a tikz-heavy document that's quadratic-ish
in the number of pictures for no reason — the picture only ever reads its own
slice.
Call-pattern finding
Tikz::render(source, picture, measurer)receives the whole document and aPictureSourcelocating one environment in it, but only ever indexes intocleanusing that picture's own byte range. Blankingsourcein full onevery call was pure waste; blanking
source[picture.start..picture.end]issufficient and
O(picture_size).Measurement (and why the perf gate's numbers don't apply)
flashtex-perf-bench --only tikz-heavyrenders through the frozencrates/render-pipeline/vendor/vector-graphicsmirror, so it does notexercise this patch at all — an earlier run against that bench looked
slower, but that was noise from a loaded machine (load average >30) hitting
frozen, unrelated code, not a measurement of this change. Those numbers are
intentionally not included here.
Instead, this measures both implementations directly, in
crates/vector-graphicswhere the change actually lives:crates/vector-graphics/src/tikz/perf_test.rskeeps a byte-for-byte copy ofthe pre-#650
blank_commentsandTikz::renderbodies as local baselinefunctions, and times old vs. new in the same process, on the same input, in
the same run, which removes machine-load bias. The fixture is the same
generator
flashtex-perf-bench's owntikz-heavycase uses(
crates/perf-bench/src/corpus.rs::tikz_heavy_document), copied in becauseflashtex-vector-graphicscannot depend onflashtex-perf-bench.Machine load was checked before each run (
uptime); both runs below weretaken with the 1-minute load average under ~20 on an 18-core, ~20-agent
shared machine. Each figure is a median of 9 timed runs after a warmup pass.
Run 1:
Run 2 (reproduced):
flashtex-perf-bench's actualtikz-heavycorpus size (same generator, same target byte count).longer (not a different fixture) purely to make the per-picture-count
scaling visible; flagging plainly that this size is synthesized, not from
a captured real-world document.
~600-750x at ~6,800 pictures, consistent with the old code being
O(N * document_size)and the new code beingO(document_size).This is a real, reproducible improvement, not noise — the
Cow/byte-scanchange alone would only help the no-comment fast path; the per-picture
slicing is the change that removes the quadratic-ish blowup.
Important: the pipeline will not see this until
crates/render-pipeline/vendor/vector-graphicsis re-pinned. The perf gate(
flashtex-perf-bench --only tikz-heavy) measures that frozen vendoredmirror, not
crates/vector-graphicsdirectly, so this fix is invisible to CIperf numbers until the mirror is updated.
Overlap check
gh pr list --repo flash-tex/flashtex --state open --search "tikz blank comments": no overlapping open PR (only render-pipeline: a box argument's leading no longer depends on the build profile (#667) #696, an unrelated render-pipeline debug/release fix).python3 scripts/coord.py claims:GH-650-TIKZ-BLANK-COMMENTSheld bydaniel-parent@mac-m5pro-dq222, no competing claim.Test results
From
crates/vector-graphicswithCARGO_TARGET_DIR=/Users/dqi26/flashtex/target-tikzblank CARGO_BUILD_JOBS=4:(74 passed total across the lib unit tests,
tests/json_depth.rs, andtests/primitives.rs; the 1 ignored is the perf comparison test above, runseparately with
--release -- --ignored --nocapture.)New unit tests in
tikz/text.rs(all passing):Not done
crates/render-pipeline/vendor/vector-graphicsis not re-pinned in thisPR (out of scope here; flagged above as the next step so the perf gate
actually reflects this change).
Implementation by gpt-5.6-luna (Codex) in a network-isolated worktree;
committed, measured, and pushed by daniel-parent since Codex had no network
access from that worktree.
🤖 Generated with Claude Code
https://claude.ai/code/session_012c9XLkHjePPGBuarrmE2mz