perf(chunking): avoid repeated metadata prefix copying and rescans

AF
17d ago
+286-3

Problem

Chunk metadata consolidation repeatedly copies the prefix of each accumulated link/emphasis list via sum(values, []). It also rebuilds all previously seen enrichment-origin keys for every next element. Large metadata-bearing chunks therefore do quadratic copying or rescanning even when their text is short.

Changes

Flatten concatenated lists once, retaining order and duplicates. For provenance, keep one set of record identities per output field key for the duration of consolidation. The identity rule, first-seen order, output dictionaries/lists, and downstream metadata copying remain the same.

This addresses metadata consolidation, independently of #4744's repeated table detection during element admission. The benchmark retains the unchanged admission code on both sides.

Validation

Review follow-up (3e92e833)

Retain the previous rejection of string and tuple values in the four concatenated metadata fields. Validate that each present value is a list before the linear flattening; list subclasses, empty lists, absent fields, duplicates and source ownership remain supported.

The focused fork validation checks the same two source/test blobs now on this PR:

  • On the original base 5390c569, all 40 new compatibility cases pass.
  • On the reviewed revision 124560b7, the 32 invalid string/tuple cases fail and 16 other metadata cases pass.
  • With the follow-up, test_base.py and test_metadata_consolidation_work.py report 436 passed, 16 skipped, including all 48 metadata cases. The skips are existing optional pandas table-geometry checks; pandas was not installed in this focused environment.
  • Changed-file Ruff, formatting and source integrity checks pass on Python 3.12.16.

The validation workflow stays on a separate fork branch and is not part of this PR. Upstream CI for the new head is tracked separately.

Initial revision (124560b7)

  • Eight new public-API cases cover both chunking strategies, aligned URL/text and emphasis lists, duplicates, differently ordered equivalent dictionaries, empty/absent fields, metadata-only image runs, source preservation, and mutable output isolation.
  • On the unchanged base, both work-bound cases fail: 200 provenance records cause 20,300 record inspections. The six semantic controls pass. After the change, those cases satisfy a linear bound including output copying.
  • Complete chunking suite: 695 passed, 2 skipped. The skips are the existing default-option comparison baselines.
  • Ruff check/format and whitespace checks pass for all changed files.

Measured impact (initial revision)

scripts/benchmark_chunk_metadata.py --baseline-ref 5390c569625e8e66254a55a6446757b2d2abe492 compares the actual baseline consolidation method with the new method inside public chunk_elements(). Each fixture uses two warm-up pairs and seven alternating measured pairs with GC before each call. Complete serialized chunk outputs compare equal after removing the newly generated output UUID; serialization and fixture creation are outside the timed call.

Generated fixtureBaseline medianNew median
20 elements, one value per metadata list0.340 ms0.296 ms
500 elements, 16 values per metadata list33.36 ms20.26 ms
2,000 elements, 32 values per metadata list860.98 ms352.98 ms
2,000 elements, one unique provenance record each1,841.30 ms340.44 ms
500 elements with lists and provenance, originals included101.47 ms23.56 ms

These are local measurements on metadata-heavy generated chunks, with timing variation between runs. Small inputs show no material demonstrated improvement; these results do not estimate document ingestion, OCR or embedding throughput. Input metadata and output lists are still materialized, so this change does not impose a total-memory bound.

Requested category: performance.