perf(chunking): reduce repeated work when combining title sections

AF
9h ago
+275-0

Summary

Combining many short sections in chunk_by_title repeatedly constructs and scans the growing prefix. Each admission builds a trial combined pre-chunk, then builds another combined pre-chunk after acceptance. Normalizing text, checking accumulated elements for tables, and copying element lists therefore take quadratic work within a large combined chunk.

Accumulate the accepted sections' elements and character lengths incrementally, then form the combined pre-chunk once when it is emitted. Count separators only between nonempty text segments and keep the first section's overlap prefix. Table isolation uses an accumulated flag. The existing combine() method performs the final combination, preserving its handling of the first pre-chunk's state.

Token budgets and custom measurements continue to use the existing complete-text path. Custom pre-chunk types and different option objects also retain their original combination behavior. This preserves chunk boundaries, text, metadata, table handling, code whitespace, and the list/lazy entry points.

Validation

  • Added 27 cases covering work bounds through both public title entry points, table isolation, original metadata, overlap, empty sections, code whitespace, custom separators, token budgets, custom measurements, and transitions from accumulated ordinary pre-chunks to custom pre-chunks or different option objects. On the unchanged base, 8 work-bound cases fail and 19 compatibility cases pass.
  • The new cases plus existing pre-chunk, combiner, title, lazy and table suites: 189 passed, 2 expected skips. The skips are the existing lazy-suite baseline option sets used by other parametrizations for comparison.
  • Compared the exact upstream accumulator against this change through public APIs over 96 combinations of character/token budgets, overlap, original-element metadata, tables, empty elements, Unicode, code and custom separators. Text, categories and metadata agree. Input IDs are normalized by their source identities, generated IDs by traversal order, and table IDs by first appearance; shared table relationships and chunk order are retained. Encoded orig_elements are compared as decoded elements.
  • With 800 short headings in one character-budget chunk, normalized text-segment visits fall from 640,799 to 2,399; elements visited by pairwise combination fall from 640,798 to 800. These are work counts through chunk_by_title, with table isolation enabled.
  • Full-repository make check and git diff --check pass.

Local chunk_by_title timings with Python 3.13.5, using seven alternating base/change pairs and identical emitted content:

InputOriginal elementsBase medianThis changeSpeedup
1,000 report sections, default 500-character windowIncluded (default)19.13 ms13.82 ms1.38×
Same report, default windowExcluded22.41 ms15.59 ms1.44×
Same report, 8,000-character windowExcluded110.65 ms12.92 ms8.56×
1,000 short headings, 20,000-character windowExcluded412.26 ms6.74 ms61.16×

Peak Python allocations measured once with tracemalloc are effectively unchanged for the default report case: 611,028 → 611,156 bytes with original elements and 545,836 → 545,964 bytes without them. The 8,000-character report case falls from 143,774 to 134,403 bytes; the single large heading chunk is 2,304,761 → 2,304,889 bytes. Input construction and native allocations are excluded. These measurements cover chunking, rather than document parsing or complete ingestion.

Focused command:

python -m pytest -q test_unstructured/chunking/test_title_combination_work.py test_unstructured/chunking/test_base.py::DescribePreChunk test_unstructured/chunking/test_base.py::DescribePreChunkCombiner test_unstructured/chunking/test_base.py::Describe_PreChunkAccumulator test_unstructured/chunking/test_title.py test_unstructured/chunking/test_lazy.py test_unstructured/chunking/test_table_isolation.py test_unstructured/chunking/test_table_detection_work.py

Full make test, coverage and hosted CI gates remain pending.

Checklist

  • The PR title is a user-facing release-note entry with a conventional commit prefix.
  • Please apply the appropriate performance category label.
  • The public API and output contract are preserved; there is no migration step.