Repository navigation
Comparison: one run per stretch, one read per parent, references and resplit controls (#446 case 1) - #450
Open
hadim wants to merge 5 commits into
Open
Comparison: one run per stretch, one read per parent, references and resplit controls (#446 case 1)#450hadim wants to merge 5 commits into
hadim wants to merge 5 commits into
Conversation
…ularity Since v0.17.0 a word comparison aligns logical words over characters, so that a word split across runs compares equal (F-X279b, issue 434 case 3). The character units then reached the output as they were: every unchanged character of a changed paragraph became its own w:r, and each letter of a revision its own w:delText or w:t inside the revision's run. For the 200 paragraphs of issue 446 case 1 that is 17,180 runs and 517 KiB of document.xml where v0.16.4 wrote 6,200 runs and 245 KiB. Character granularity has always written one run per character, and v0.16.4 wrote one run per word, space and punctuation mark at word granularity. The granularity now sets what is compared, never how the result is cut. Two rules, in the two helpers that the paragraph path and the inline control path share, apply to every granularity, story and revision kind: - coalesced_granular_alignment keeps unchanged pairs together while they continue the same original and the same edited text element, as it already kept the pairs of one change together. Unchanged text is one run between two revisions. - merge_unit_runs joins consecutive fragments of one text element back into one text with one source selection, whole again when every fragment is there. A revision holds its text in one w:delText or w:t, and a fully unchanged element is published from its source as at run granularity. The same 200 paragraphs now give 600 runs and 76 KiB, one run per revision and one per stretch of text around them. Two cases still cut a stretch: under ignore_whitespace an ignored whitespace unit ends it, so unchanged text stays one run per word and per space, and a tab or a break ends it as it ends a text element. The alignment, the text revisions, the logical-word rule, the shared revision of a word split across runs, the custody markers and the refusals are unchanged. One visible difference besides the runs: a formatting change of a run is one w:rPrChange per unchanged stretch, as at run granularity, instead of one per word (v0.16.4) or per character (v0.17.0). The space-flag control of compare_producer_noise pinned the one-run space of the old shape. It now checks the same fact on the stretch that ends with the space. Part of tensorbee#446. Tests, issue446_* in regression_test.rs state the shape as invariants in the body, a table cell, an inline control, a hyperlink, a header and a footnote (one text element a run, no added run that repeats the properties of the sibling it follows, run count bounded by the revisions, run count and added size independent of the unchanged text), two unit tests in comparison.rs, test_issue446_* in test_core.py Harness, unchanged, the samples make no comparison
…candidate once A comparison became slower in v0.17.0 at every granularity, the default run granularity included (about 4 times on the documents of issue 446, with the same output), and quadratic in the children of one paragraph or table. Four causes, none of them in the alignment: - The source guard of F-X272 (af99e9d) read a parent from its start once per child. RunSource::capture did it for each run of a paragraph, publication_parent_at and source_child for each row of a table, cell of a row, child of a cell or a control and inline control of a paragraph, in the five source snapshots of a comparison and again in the compare and mark paths (compare_table, compare_row, compare_cell_from_xml, compare_granular_controls, compare_complex_paragraph, paragraph_children_xml, marked rows and cells, and the parent of each written piece in compare_granular_paragraph). capture_all, publication_parents_at and source_children read a parent once for all its children. Each answer, an error included, is the one a read for that child alone gives, and each is taken where that read was made, so the errors keep their order. ParentScopes reads the scopes a paragraph path may ask on its first question. - Each postcondition direction resolved the candidate twice, once for the package guard and once more inside verify_footnote_destination (F-X272a, 1c69b7e). resolved_candidate resolves it once and both checks read it. - The postcondition signature of an ordinary part is taken at character granularity so that run splits do not change it (F-X279b, 2d38731). It built one unit, one copy of the run properties and one token per character of all four documents. A run with no raw child and no deleted text is now read one stretch at a time, and consecutive plain characters are one token, in paragraphs and in the direct runs of a control. Two signatures read equal exactly when their character tokens did. - A paragraph snapshot computed its run spans twice. It computes them once. The package guard still reads each cleaned story a second time, as v0.17.0 does. That read refuses a drawing that shares its run with the begin mark of a complex field, whatever else changes ("comparison could not correlate split field run 2"), and a test pins that refusal: a speed change must not change which inputs a guard refuses. compare() CPU time, median of 5 runs alternated between builds, release wheels (0.16.4 and v0.17.0 from PyPI with fat LTO, this branch with LTO off and 16 codegen units, which favours the older wheels), measured on the final tree of this series: case granularity 0.16.4 v0.17.0 branch issue 446, 200 changed run 0.11 s 0.44 s 0.31 s issue 446, 200 changed word 0.64 s 6.65 s 0.45 s issue 446, 200 changed character 1.74 s 6.95 s 0.76 s issue 446, 400 changed run 0.21 s 0.89 s 0.62 s issue 446, 400 changed word 1.38 s 14.40 s 0.94 s 1000 paragraphs, 100 chg. run 0.33 s 1.14 s 0.81 s 1000 paragraphs, 100 chg. word 0.73 s 4.33 s 0.94 s 1000 paragraphs, 100 chg. character 1.57 s 4.62 s 1.01 s one paragraph of 400 runs run 0.06 s 0.53 s 0.12 s one paragraph of 400 runs word 0.09 s 9.29 s 0.15 s table of 100 rows run 0.21 s 1.74 s 0.88 s table of 100 rows word 0.33 s 2.34 s 0.95 s Growth for 4 times the rows of a table is 4.2 (0.16.4 4.2, v0.17.0 6.9) and for 4 times the runs of one paragraph 4.4 at run and 4.6 at word granularity (v0.17.0 13.2 at run). Run granularity stays 2.5 to 3 times 0.16.4 on paragraphs and 4.2 on a table. Restoring the guard's second read accounts for part of it. A release profile of the issue 446 script at run granularity (400 paragraphs) attributes 24 percent of a comparison to the footnote custody validation in a document with no note, 18.5 percent to canonicalize_drawing_ids in a document with no drawing, 19 percent to the package guard and 15 percent to compare_body. Checked on the final tree of this series against the v0.17.0 release wheel, with a debug build and every debug oracle on. The 1,080 cases of a refusal map around revisions, notes, comments, moves, controls, fields and bookmarks give the same refusals and messages and leave refused inputs intact. 792 guard cases give the same refusals, messages and run-granularity bytes. 500 random pairs give the same accepted and rejected text and formatting, and the same insertions, deletions and run bytes except 3 pure resplits inside an inline control, which a later commit of this series compares as equal. Part of tensorbee#446. Tests, comparison.rs units compare the one-read answers with lone reads, errors included, check the stretch tokens, and count the source bytes a comparison reads for few and many runs, rows, hyperlink runs and inserted paragraphs and tables at the three granularities, issue446_comparison_keeps_refusing_a_drawing_in_a_field_begin_run in regression_test.rs, no timing threshold Harness, unchanged, the samples make no comparison
A word or character comparison of a changed paragraph whose w:t holds a reference refused with "split text has opaque mixed events", in the body and in every related story. That covers &, <, > and ", which rdocx writes itself on save, and numeric references such as &tensorbee#233;: saving and reopening "Smith & Sons pay now." and comparing it with "... later." refused. 0.16.4 compared it. source_text_fragment (F-X272, af99e9d) checks that the source of a split text is one element holding text, and took exactly one text event. The XML reader reports a reference as an event of its own between two pieces of text. Any number of text and reference events now pass at the text's depth. A nested element, CDATA, a comment or a processing instruction still refuses. The value written comes from the parsed model and is escaped again on output, as before. Part of tensorbee#446. Tests, issue446_comparison_splits_text_that_holds_references in regression_test.rs, with the reference in the changed token, at word and character granularity, in the body and a header, accepted and rejected back to each input Harness, unchanged, the samples make no comparison
A paragraph whose runs only split the same text differently compares equal since v0.17.0 (F-X279b). An inline content control did not: its runs were aligned one by one, so one run against three with the same text and formatting gave 4 revisions at run granularity, as in 0.16.4. A word redline of one sentence inside a control, accepted or rejected and compared again with its input, gave 25 revisions at word granularity and 70 at character granularity on 0.16.4 and v0.17.0, and 4 and 8 before this commit. It now gives none. When every child of both controls is a run and the runs differ, compare_control_from_xml now reads them with the equality the paragraph path trusts, coalesced_paragraphs_equal, and keeps the original source when they are equal. It does not apply under leaf capture, to a control holding a raw child or a revision, or to runs holding a field, whose source facts it cannot read from copies, which keep the path below. Part of tensorbee#446. Tests, issue446_comparison_reads_a_resplit_inline_control_as_equal in regression_test.rs at the three granularities, with a real change kept as a revision Harness, unchanged, the samples make no comparison
The rdocx archive grows with the issue 446 comparison changes and their tests, on main eb980e9. It is the only crate whose member bytes change. It was measured on 2026-11-16, which its row and the remeasurement date now record. Tests, scripts/readme_doctests.py build_package_archive and archive_measurement for rdocx, member bytes stable on a second pass Harness, unchanged, measurements only
This was referenced Nov 16, 2026
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #446 (case 1). Five commits on
maineb980e9 (v0.17.0): the four changes below, then the archive row.Context: why a change of this size
The report. Case 1 of #446:
compare(..., granularity="word")in 0.17.0 writes every character of a changedparagraph as its own
w:r, and each deleted or inserted letter as its ownw:delTextorw:t. For the issue's 200paragraphs that is 17,180 runs and 517 KiB of
document.xmlwhere 0.16.4 wrote 6,200 runs and 245 KiB, and thecomparison takes 10 times as long. The comment on the issue adds that the default
rungranularity is about 4 timesslower than 0.16.4 with the same output, so the slowdown has a cause of its own.
Four causes from three v0.17.0 rewrites. None of them is in the alignment, and each is a reasonable step of
the feature that introduced it:
Why one pull request. The first cause decides the shape of the output and the other three its cost. Fixing the
shape alone left one long paragraph quadratic in its runs and the default run granularity 4 times slower than 0.16.4.
Fixing the reads alone left the output twice the size of 0.16.4's. Each commit stands on its own and states its own
guard.
The size is exceptional. Most of the diff is in
comparison.rsand it is in two families: the two helpers everygranular path shares (
coalesced_granular_alignment,merge_unit_runs), and the readers that answer every child ofa parent from one read (
RunSource::capture_all,publication_parents_at,source_children,ParentScopes),applied where the old code read once per child. It is not a pattern for other changes.
Two fixes ride along, both found while checking the siblings of the paths above: word and character comparisons
refused any changed text holding a reference such as
&(also since af99e9d), and an inline content controlwhose runs only split the same text differently was compared run by run (since 0.16.4).
The causes and the fixes
1. Character units reach the output (commit 1)
Since v0.17.0 a word comparison aligns logical words over character units, so that a word split across runs compares
equal (
compare_granular_paragraph,coalesced_words). Nothing joined the units again before writing:coalesced_granular_alignmentgrouped the pairs of a change but never the unchanged ones, andmerge_unit_runswroteone text element per unit.
The granularity now sets what is compared, never how the result is cut. Two rules, in the two helpers that the
paragraph path and the inline-control path share, so they hold at every granularity, in every story and for
inserted and deleted text alike:
coalesced_granular_alignmentkeeps unchanged pairs together while they continue the same original and the sameedited text element, as it already kept the pairs of one change together.
merge_unit_runsjoins consecutive fragments of one text element into one text with one source selection, wholeagain when every fragment is there, so a fully unchanged element is published from its source as at run
granularity.
2. The postcondition signature reads one token per character (commit 2)
The acceptance and rejection postconditions compare normalized packages. For an ordinary part the signature is taken
at character granularity so that run splits do not change it (
coalesced_postcondition_body). It built one unit, onecopy of the run properties and one token per character of the candidate resolved both ways, the original and the
edited document.
A run with no raw child and no deleted text is now read one stretch at a time, and consecutive plain characters are
one token (
signature_run_units,signature_scalars,ParagraphToken::Scalars), in paragraphs and in the directruns of a control. A run holding a raw child or deleted text keeps its units per character, because those belong to
single characters.
3. The source guard reads a parent again for each child (commit 2)
F-X272 checks the comparison against the physical source. Its helpers read a parent from its start for each child:
RunSource::capturefor each run of a paragraph,publication_parent_atandsource_childfor each row of a table,cell of a row, child of a cell or a control, inline control and field of a paragraph. This happens in the five source
snapshots of a comparison and again in the compare and mark paths:
compare_table,compare_row,compare_cell_from_xml,compare_granular_controls,compare_complex_paragraph,paragraph_children_xml, the markedrows and cells of an inserted or deleted table, and the parent of each written piece in
compare_granular_paragraph.The cost is quadratic in the children: growth for 4 times the rows of a table was 7.0 on v0.17.0, and 8.8 at word
and 10.2 at character for 4 times the runs of one paragraph on the first version of this branch.
RunSource::capture_all,publication_parents_atandsource_childrenread a parent once for all the positionsasked. Each keeps the loop of the single-position function it replaces, so the answer for each position, an error
included, is the one a read for that position alone gives, and each answer is taken where that read was made, so
errors keep their order.
ParentScopesaskspublication_parents_atfor every run edge of a paragraph on its firstquestion, and reads a position outside that set alone.
publication_parent_atandsource_childremain as theone-position form of the same loop.
4. The footnote destination check resolves the candidate again (commit 2)
Each postcondition direction resolved the candidate for the package guard, then
verify_footnote_destinationclonedand resolved it again.
resolved_candidateresolves it once per direction and both checks read that document.Ride-along A: text holding a reference (commit 3)
source_text_fragment(af99e9d) checks that the source of a split text is one element holding text, and acceptedexactly one text event. quick-xml 0.41 reports
&oréas aGeneralRefevent between two text events,so every word or character comparison of a changed paragraph holding a reference refused with "split text has opaque
mixed events", in the body and in related stories. That includes
&,<,>and", which rdocxwrites itself on save: saving and reopening "Smith & Sons pay now." and comparing it with "... later." refused.
0.16.4 compared it. Any number of text and reference events now pass at the text's depth. Nested elements, CDATA,
comments and processing instructions still refuse. The value comes from the parsed model and is escaped again on
output, as before.
Ride-along B: a resplit inline control (commit 4)
A paragraph whose runs only split the same text differently compares equal since v0.17.0. An inline content control
did not, since its runs were aligned one by one: one run against three with the same text and formatting gave 4
revisions at run granularity, as in 0.16.4. A word redline of one sentence inside a control, accepted or rejected and
compared again with its input, gave 25 revisions (word) and 70 (character) on 0.16.4 and v0.17.0. When every child of
both controls is a run and the runs differ,
compare_control_from_xmlnow applies the equality the paragraph pathalready trusts (
coalesced_paragraphs_equal) and keeps the original source when it holds.How the guarantees are kept
resolved_package, resolve it again inverify_footnote_destinationresolved_candidateonce per direction, both checks read itRunSource::captureper runRunSource::capture_all, one readpublication_parent_atandsource_childper childpublication_parents_atandsource_children, one read per parentproject_source_bodycoalesced_paragraphs_equalon the control's runsOn top of the unit tests, three differential sets against the v0.17.0 release wheel, all on a debug build with every
debug oracle on:
stories: same refusals, same messages, inputs intact.
ignore_formatting): same refusals, same messages, same bytes at run granularity.per-character formatting. Insertions, deletions and run-granularity bytes are the same except in 3 pairs, each a
pure resplit inside an inline control where v0.17.0 wrote a revision (ride-along B).
Behaviour changes
whatever the unit, in every story. Each revision holds its text in one
w:torw:delText.w:rPrChangeper unchanged stretch, againstone per word in 0.16.4 and one per character in v0.17.0.
ignore_whitespacean ignored whitespace unit ends a stretch, so unchangedtext is one run per word and per space (as in 0.16.4), and a tab or a break ends a stretch as it ends a text
element.
same text differently, which keeps its source where v0.17.0 wrote a revision.
refused.
p.runsof a word or character redline returns the stretches and revisions, not one run per character.Numbers
Method: CPU time of
compare()alone, median of 5 runs alternated call by call between the three builds, on onemachine (Apple M5 Max, macOS). 0.16.4 and v0.17.0 are the PyPI release wheels (fat LTO, one codegen unit). This branch
is a release build with LTO off and 16 codegen units, which makes its ratios pessimistic. Cases are the issue's
scripts and the cases of the review (
bench_worker.py): one paragraph of 400 runs with one word changed, and a tableof 100 rows of 6 cells with one cell in 10 changed.
Growth of the time for 4 times the children, within one build, median of 3:
v0.17.0 was not timed at word and character on the long paragraph, where one comparison of 2000 runs takes over a
minute and a half. Output of the issue's 200 paragraphs:
Word and character granularity are faster than 0.16.4 on the issue's documents. Run granularity, the default, stays
2.5 to 3 times 0.16.4 on paragraphs and 4.2 times on the table: see finding 2.
Findings in no issue
its run with the begin mark of a complex field, whatever else changes: "comparison could not correlate split field
run 2", or "comparison could not correlate paragraph run owner 2: 1 physical runs from cursor 6, 6 available". The
first read of the guard, on the source-faithful model, passes. The second,
project_source_bodyon the cleanedclone, runs after
normalize_drawing_relationshipshas setdocPrids to 0 and rewrittenr:embedto asignature, so
field_source_run_fragments_atsees the outside run differ from the parsed one and the correlationfails. On a probe of 792 cases, 90 go from refused to accepted when the second read is skipped, with sound output
(accept and reject round trip, no analysis id leaked, embeds resolve). 0.16.4 accepted 90 of the 108 inputs with
this construct. This pull request keeps the refusal and pins it in
issue446_comparison_keeps_refusing_a_drawing_in_a_field_begin_run, since a speed change must not decide whichinputs a guard refuses.
sample)attributes 24 % of a comparison to
validate_footnote_custodyin a document with no note (note_markers16 %,footnote_reference_sites9 %), 18.5 % tocanonicalize_drawing_idsin a document with no drawing, 19 % to thepackage guard (
normalized_package, including the second read this pull request keeps) and 15 % tocompare_body. The custody work belongs to the pull request on compare fails when a footnote is added or removed #430, which changes that engine.canonicalize_drawing_idsalso renumbers relationship ids read from the serialized opaque stories and sorts headerand footer references, so "the model holds no drawing" is not a proof that it has nothing to do, and it was left
as it is. Restoring the guard's second read cost about 3.5 % at run granularity on the issue's script: against
v0.17.0 the first version of this branch, which skipped that read, measured 0.67 in the review and this one 0.70.
revisions (a whole-shape replacement) in a VML text box at every granularity, and refuses with "comparison
acceptance does not reproduce the edited stories" in a DrawingML text box, a complex field result and a simple
field result, on v0.17.0 and on this branch. 0.16.4 gave 0 revisions for both field results and refused both text
boxes ("comparison correlated 1 of 2 physical paragraph runs").
at run granularity, since 0.16.4: accepting the redline does not reproduce the edited formatting.
accept_allcan leave an emptyw:rPrthat a later comparison reads as a formatting change, so comparing anaccepted redline with its edited input is not always empty (since 0.16.4).
alignis O(N x M) in time and memory per shell segment,so one long paragraph costs the square of its characters at character granularity: growth for 4 times the runs is
6.2 on 0.16.4 and 7.7 on this branch. A common prefix and suffix trim before the table would make one edit
linear. It is left for a change of its own.
restore_resolved_leaf_occurrences), where text boxes aresmall. They were left as they are.
Gates run locally
On the final commit, macOS arm64, Rust 1.97.1, one suite at a time. Poppler 26.10 through
pixi exec -s poppler(CI pins 26.01), LibreOffice 26.8 (CI pins 26.2.5.2), no
hb-subset14.4.0.cargo fmt --all -- --check,cargo clippy --workspace --all-targets --all-features -- -D warnings,scripts/prose_check.py,scripts/sync_agent_skills.py --check: clean.scripts/hash_harness.py --check: 49 entries match.scripts/readme_doctests.py: pass, with therdocxarchive rowre-recorded in the last commit.
cargo check --locked --target wasm32-unknown-unknown -p rdocx-wasm -p rpptx-wasm: pass.cargo test -p rdocx(RUST_MIN_STACK=8388608, aftercargo build -p rdocx-cli):large_word_and_presentation_pdfs_preserve_logical_reading_order(pdftotext 26.01.0),word_and_powerpoint_chart_pixels_are_identical(at the rasterizer version assert),fx195_variable_svg_paint_matches_independent_static_instances(hb-subset14.4.0 absent).section_page_semantics_match_pinned_libreoffice_render,every_conditional_table_region_matches_word,odt_reader_matches_pinned_libreoffice_structure,sanitized_public_authoring_fixture_passes_every_conformance_stage,public_authored_theme_and_fonts_match_pinned_word_resolution.cargo test -p rdocx-cli: 4 and 116 passed.pytest crates/rdocx-py/tests:1400 passed, 61 failed, 2 skipped. 3 failures are version pins (
test_issue_253_pdf_text_viewon pdftotext,test_colour_inputs_and_nested_font_formats_reach_actual_renderingon pdftoppm,test_fx200_python_style_saved_output_paints_exact_expected_colours_with_deterministic_native_cliwithoutRDOCX_CLI). 58 are intest_rendering_threads.py: 1 at the pdfinfo 26.01.0 pin and 57 on the missingF_X195_*inputs that the CI step "Prepare independent font acceptance inputs" builds withhb-subset.mypy --strict crates/rdocx-py/tests/typing_smoke.py crates/rdocx-py/python/rdocxandmypy.stubtest rdocxon arelease wheel (mypy 2.3.0, Python 3.12): no issues.