mirror of
https://github.com/storytold/wordcraft.git
synced 2026-10-31 22:07:06 -06:00
Saving a document with many content controls was quadratic in the number of controls: 32,000 of them took four seconds to save, and doubling that took four times as long again. Two loops in the writer cost that. The plan that decides where each control is written collects the inline markers of every control in one set, and then, for each paragraph, looks for that paragraph's markers by filtering the whole set — so one paragraph's answer costs the size of the document's marker set, and the save costs paragraphs times markers. And to find the block-level controls that clash, every control was compared with every other control. The markers are now grouped by paragraph once, so a paragraph's markers are a lookup, and the block-level controls are grouped by the block they end in, each group keeping the two earliest closes, which answers the clash question the same way in one sweep. What is written does not change: the packages written for ten shapes of control nesting — inline, around paragraphs, chained, nested — are byte for byte the same as before. Tests: crates/docx/tests/content_control_save_cost.rs. The two timing tests fail without the fix (16.1x and 13.0x the time for four times the controls, against a bound of 8.0) and pass with it (4.6x). Fixes #3437. Co-Authored-By: opencode <noreply@opencode.ai>