Repository navigation
Python edits: publish from the owner inventory, compare one block, edit properties without staging - #449
Open
hadim wants to merge 3 commits into
Open
Python edits: publish from the owner inventory, compare one block, edit properties without staging#449hadim wants to merge 3 commits into
hadim wants to merge 3 commits into
Conversation
set_core_properties, set_application_properties, set_custom_property and remove_custom_property cloned the whole document to protect one fallible step: reserving the properties part in the package graph and the identifier registry. The clone and the drop of the previous document cost in proportion to the body, so a properties setter was linear in a document it does not read. edit_properties saves the package graph, whose part bytes are shared, and the identifier registry, runs the same reservation and model write on the document, and puts both back on refusal. A success leaves what commit_staged_mutation leaves for a staged clone: the derived layout is dropped and a signed package records a package mutation. The cost follows the package graph and the registry, not the body. Debug builds run the staged edit on a clone as well and assert the same package graph, identifier registry, property models, part names and refusal. They count whole-document staging clones (take_staging_clones), outside the debug references, which run under without_staging_work, and can force the staged route (force_staged_properties), so a test of a binding can state that a call stages no document and compare both routes. The test covers a refusal before any reservation (a repeated relationship id) and one after the part name is reserved (no free relationship id), where the registry must come back as it was. Part of tensorbee#445.
Two of the whole-document passes a Python edit made when it published. Routes. Paragraph.add_run, add_hyperlink and add_equation, Run.text, Table.add_row, Table.remove_row, the checked fallback of Cell.text, Document.set_chart_legend and a text box ParagraphFormat resolved the route of their publication by a checked story walk, which serializes the main part. They take it from the owner inventory record the checked reader prepared, through paragraph_route (as Paragraph.text does since v0.17.0), table_route and run_route. Paragraph.add_internal_hyperlink does the same for its publication and keeps one walk for the address of its native edit. Debug builds resolve a body paragraph or body table route by the walk too and assert both agree, and assert that the paragraph at a run route holds the run path. A handle with no inventory record keeps the walk. Calls whose native edit takes a checked story address still walk for it: Run.add_picture, Run.replace_xml, Table.replace_xml, Cell.merge, the note calls. No-op check. Run.text compared two staged saves of the whole document to learn whether its typed edit changed anything. For a run of a direct body paragraph it reads that block: equal paragraph models publish nothing, and two plain paragraphs whose checked reader facts differ (direct_body_paragraph_facts) publish. Text XML 1.0 cannot carry, another paragraph shape, a body with a known save failure and facts that do not tell keep the staged comparison and its error, as every other publisher does. Debug builds assert the block answer against the staged comparison whenever that comparison answers. The block answer does not save the rest of the document: content elsewhere that no save accepts does not make Run.text raise, save reports it, as after Paragraph.text. Guards. Debug builds count document staging clones, route story walks, staged save comparisons and full inventory publications (_word_publication_work), outside the guards, and _word_force_checked_route restores the walk, the staged comparison and the staged properties edit. test_edit_publication_cost.py lists the calls that make no whole-document pass and do the same counted work in a body four times larger (FLAT), and the passes every other call makes (GROWING, a strict expected failure of the flat test), and checks that both routes of each call give the same result, saved parts and held handles. rdocx-py depends on oxml-core directly for is_xml_1_0_character, the XML 1.0 Char production, in place of a second copy of it. Part of tensorbee#445.
This was referenced Nov 16, 2026
Open
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 #445 (and of #437, #407, #398, #378 before it). Three commits on
maineb980e9 (v0.17.0).Context: why a change of this size
The history. The cost of Python edits that grows with the document has been reported five times: #378, #398
(fixed by #399), #407 (#415, then #417 to #422), #437 (#443) and #445. Each report named calls, each fix made those
calls cheaper, and the next benchmark found the same cost under other calls. v0.17.0 answers #437 and #445 for the
calls they name: the cell setters no longer grow with the body, and
Document.openno longer keeps an inventory.Both issues stay open for their numerical criteria.
The five whole-document steps. A plan of 2026-10-23 named what one Python edit does in proportion to the
document, whatever it edits: it rebuilds and matches the owner inventory, compares two staged saves to detect a
no-op, flushes the package, reopens it, and walks a whole story to address the element. Profiled on v0.17.0 (release
build, 1000 body paragraphs of three runs, macOS
sample), all five are still there:Paragraph.add_runParagraph.textRun.textTable.add_rowWhy per-call fixes did not close it. The unit of a publication is the document, and each fix qualifies one call
shape for a local route.
Paragraph.texttakes its route from the owner inventory since v0.17.0, whileappend_run,in the same file, still walked the story. The scaling test of rdocx-skills times every public mutating call at two
body sizes. 127 calls are on its list of costs accepted as linear. Run once each with timers around every step, 126
of them clone the document, 96 publish the whole inventory, 56 walk a story, 40 compare staged saves and 75 reopen
the package. A step that 40 or 126 calls share is not fixed by a route for one of them.
What this pull request changes. Three shared steps:
whose walk served only the publication:
Paragraph.add_run,add_hyperlinkandadd_equation,Run.text,Table.add_row,Table.remove_row, the checked fallback ofCell.text,Document.set_chart_legendand a textbox
ParagraphFormat.Paragraph.add_internal_hyperlinkdoes the same for its publication and keeps one walk.Run.textdecides whether its typed edit changed anything from the one plain body paragraph itedits, without comparing staged saves. Every other publisher,
Run.replace_xmlincluded, keeps the comparison.And one guard: a test that states, for each call, which whole-document passes it makes, read from debug counters and
not from a clock.
What it leaves.
add_run,Paragraph.textandRun.textafter this change.Paragraph.add_internal_hyperlink,Run.add_picture,Run.replace_xml,Table.replace_xml,Cell.merge(three walks),Document.add_footnoteandadd_endnote, a text box paragraph edit. The walk there is the address the native call asks for, not a route theinventory could give.
of the cell setters, and the two validation passes of open and replace. See "Later".
How the guarantees are kept.
checked reader produced. The no-op answer is read from
direct_body_paragraph_facts, the checked reader thepieces already use, on the block before and after. Nothing is inferred from what the edit meant to do.
before. A paragraph that is not plain, text that XML 1.0 cannot carry, a body whose save is known to fail and facts
that do not tell keep the staged save comparison. Debug builds check each direct answer: a body paragraph or body
table route against the story walk (
assert_indexed_route), a run route by reading the paragraph there for its runpath, the no-op answer against the staged comparison whenever that comparison answers, and a properties edit
against the staged edit on a clone (package graph, identifier registry, models, part names, refusal).
StaleElementError. The publication itself is untouched: the sameprepare_word_revisionruns with the same
WordPublicationvalue. Only how the route and the no-op answer are obtained changed.refusal. A refused
Run.textstill raises the staged save's error, from the staged save.routes: same result or error, same saved parts, same state of eleven held handles.
One change a caller can see. The staged comparison saved the whole candidate, so
Run.textraised when contentelsewhere in the document could not be saved, for instance a control character that
Paragraph.textacceptedearlier. The block answer reads one paragraph. In that state
Run.textpublishes andsaveraises, which is whatParagraph.textandParagraph.add_runalready do.Run.textstill refuses its own text, with the same error.test_run_text_publishes_beside_content_that_no_save_accepts_and_save_reports_itpins both routes. If the change isnot wanted,
PyRun::set_textcallspublish_owner_candidatein every case and the staged comparison is back.What changed
crates/rdocx/src/document.rs:edit_properties, used byset_core_properties,set_application_properties,set_custom_property,remove_custom_propertycrates/rdocx/src/document.rs:take_staging_clones,force_staged_properties.without_staging_workkeeps the clones of a debug reference out of the countcrates/rdocx-py/src/document.rs:paragraph_route(existing),table_route,run_route. Callers inparagraph.rs(append_run,append_internal_run,add_equation),run.rs(set_text),table.rs(add_row,remove_row,Cell.textfallback),formatting.rs(text box paragraph format),document.rs(set_chart_legend)crates/rdocx-py/src/document.rs:publish_block_candidate,direct_paragraph_saves_as,typed_run_edit_slot.publish_owner_candidateis unchanged but for its countersaves_aswhen it answerscrates/rdocx-py/src/document.rs:_word_publication_work,_word_force_checked_routeoxml-coreas a direct dependency ofrdocx-pycrates/rdocx-py/Cargo.toml,Cargo.lockis_xml_1_0_character, instead of a second copy of theCharproductioncrates/rdocx-py/tests/test_edit_publication_cost.py, one test incrates/rdocx/src/document.rsdocs/hld/10-bindings-spec.md, "Word checked XML owner authority"The test file is new. It can be folded into
test_core.pyif a new file is not wanted.Numbers
Median CPU time per call, release builds with LTO off, macOS arm64, v0.17.0 against the head of this branch. The
body holds N paragraphs of three runs and is opened from a file. Handles are taken before timing (
bench.py). Themachine was shared with other builds, so read the columns against each other: both builds were measured back to back,
and the calls this pull request does not touch (
Paragraph.text,Document.add_paragraph) agree within5 %.
Paragraph.add_runParagraph.add_hyperlinkRun.text (new text)Run.text (same text)Paragraph.textCoreProperties.titleAppProperties.companyCustomProperties.setDocument.add_paragraphTable.add_row (2 to 9 rows)Run.textwith the same text still clones the document before it finds out that nothing changed.Table.add_rowloses its story walk and keeps the rest. In an empty document, on one 2 x 5 table,
Table.add_rowcosts3.66 ms at rows 2 to 8 and 75.3 ms at rows 165 to 171 on v0.17.0
(4.75 ms and 66.9 ms here).
Whole-document passes per call, from the debug counters. The checked route is what the debug switch restores. It
walks wherever a route is asked for, which is one walk more than v0.17.0 for
add_run, since v0.17.0 already boundthe returned run from the inventory:
CoreProperties.titleParagraph.add_runRun.textRun.text, same textTable.add_rowDocument.set_chart_legendGuards
test_edit_publication_cost.pyreads the debug counters, so it runs in the "Python bindings" job as it is and isskipped on a release build. 107 tests pass and 16 are strict expected failures.
FLAT, 30 calls: no whole-document pass, and every counter equal in a body of 8 and of 32 paragraphs. The 21CorePropertiesandAppPropertiessetters, the fourCustomPropertieswrites, and five calls that were flatalready and are now guarded:
Font.bold,Paragraph.alignment,ParagraphFormat.left_indent,Cell.text,Cell.shading.GROWING, 16 calls, with the passes each one makes, asserted exactly:Paragraph.add_run,add_hyperlink,add_internal_hyperlink,add_equation,Paragraph.text,Run.text,Run.add_break,Run.add_tab,Table.style,Table.add_row,Table.remove_row,Cell.textof a cell that holds a table (the checkedfallback),
Document.set_chart_legend,Settings.track_revisions,Document.add_paragraph,Document.add_table. The flat test is a strict expected failure for them. A later change that makes one flat, orremoves one pass, fails until the entry moves or shrinks. The clone counts leave out the debug references of
rdocx, so they are what a release build does.breaks, empty text, a refused application version, a missing custom property). For
Cell.textandCell.shadingon a plain table this proves nothing yet: the route switch does not reach the local cell route, so both runs take
it. The stacked pull request for the cell setters makes the switch send them to the whole-document route and
compares the two.
Run.textbeside content that no save accepts: publishes on the direct route, raises on the checked route, andsaveraises.ParagraphFormat: two walks for its edit address on the direct route, three on the checked route, thesame saved parts.
rdocx: the four properties edits stage no document. A refusal before any reservation (a repeatedrelationship id) and one after the part name is reserved (no free relationship id) both leave the package graph,
the identifier registry and the models as they were. The test fails when the two restore lines of
edit_propertiesare removed.Findings that are in no issue
Run.textwas the most expensive frequent call, more than four timesParagraph.add_runin the table above.77 % of it was the save comparison, which serialized the main part nine times in one call. This pull request
removes that part.
Table.add_rowgrows with the table it extends, not only with the body: see the empty document figuresabove. The native
add_empty_table_rowclones the document, flushes and reopens the package, and the publicationreads the facts of the whole table again. Not changed here.
Paragraph.textaccepts text that no save accepts.paragraph.text = "a\x00b"publishes and the nextsaveraises, where
run.text = "a\x00b"is refused at once. Not changed here.Document.add_paragraphstill visits every owner record: its cost in the table above growsabout eight times from 1000 to 8000 paragraphs.
Later
Not in this pull request. The figures are measured on v0.17.0 unless marked as an estimate.
add_run,Paragraph.textandRun.text. One block transaction would remove both: clone the one body block (asdirect_body_table_cell_candidatedoes for a cell), read its facts with the checked reader, and splice its ownersinto the inventory (as the body splice of
add_paragraphdoes for an inserted block). Estimate, not measured:about the cost of
Document.add_paragraphin the table above.Table.add_row,clone_row,remove_row,insert_column,Cell.merge,Run.add_picture,Document.add_footnote,insert_contentand others. It is 52 % ofTable.add_row. The binding cannot remove it.Cell.shadingand 91 % ofCell.textin an empty document is the fact read of thewhole table (
word_block_ownersinapply_local_cell_edit): 5.7 ms for a 100-cell table in the profile run. Thelocal route also declines some cells and documents, which then take the whole-document route.
Document.open,try_replace_textandsavecost 1.4 timesrdocx replace. The bindingadds two validation passes whose results are dropped (
from_documentandvalidate_word_factsindormant_replacements), 37 % of its time. F-X283 keeps them so that no open error moves to the first handle, andtest_fx283_missing_external_link_still_refuses_openpins one. Left untouched: removing either would lose anerror a caller receives today.
Settings.track_revisions,update_fields_on_open,Table.style,Run.add_break,Run.add_tabstill clonethe document and do nothing else that grows.
Gates run locally
Run on e142eb0, macOS arm64, Poppler 26.09 through
pixi exec -s poppler.cargo fmt --all --checkandcargo clippy --workspace --all-targets --all-features -- -D warnings: clean.python3 scripts/prose_check.py: 0 violations.python3 scripts/sync_agent_skills.py --check: clean.python3 scripts/hash_harness.py --check: 49 entries match.python3 scripts/readme_doctests.py: passes, with therdocxarchive row re-recorded in its own commit.cargo test -p rdocx --no-fail-fast(debug, so the staged properties edit runs next to every direct one): lib 1048passed, 3 failed.
integration_test557 passed, 5 failed.regression_test2531 passed. The 8 failures pin toolversions this machine does not have: lib
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(HarfBuzz 14.4.0). Integrationodt_reader_matches_pinned_libreoffice_structure,public_authored_theme_and_fonts_match_pinned_word_resolution,section_page_semantics_match_pinned_libreoffice_renderandevery_conditional_table_region_matches_word(LibreOffice 26.2.5.2),
sanitized_public_authoring_fixture_passes_every_conformance_stage(cargo run --offline).cargo test -p rdocx-cli --no-fail-fast: 120 passed.maturin develop --locked, debug assertions on), fullcrates/rdocx-py/tests: 1505 passed, 2 skipped, 16 expected failures (the GROWING list), 61 failed. The 61 pintools:
test_issue_253_pdf_text_view(pdftotext 26.01.0),test_colour_inputs_and_nested_font_formats_reach_actual_rendering(pdftoppm 26.01.0),test_fx200_python_style_saved_output_paints_exact_expected_colours_with_deterministic_native_cli(RDOCX_CLIunset), and the 58 tests of
test_rendering_threads.py(Poppler 26.01.0 and installed fonts). No debug check fired.mypy --strictontyping_smoke.pyand the package, no issues in 9 source files.mypy.stubtest rdocx: no issues in 8 modules. The debug-only counters and switches are absent from it.issue445_properties_edits_stage_no_document_and_refuse_without_a_tracegoes red when the two restore lines ofedit_propertiesare removed.