Skip to content

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

Open
Afloat16 wants to merge 2 commits into
Unstructured-IO:mainfrom
Afloat16:linear-chunk-metadata-consolidation-20261114
Open

Afloat16 wants to merge 2 commits into
Unstructured-IO:mainfrom
Afloat16:linear-chunk-metadata-consolidation-20261114

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

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 fixture Baseline median New median
20 elements, one value per metadata list 0.340 ms 0.296 ms
500 elements, 16 values per metadata list 33.36 ms 20.26 ms
2,000 elements, 32 values per metadata list 860.98 ms 352.98 ms
2,000 elements, one unique provenance record each 1,841.30 ms 340.44 ms
500 elements with lists and provenance, originals included 101.47 ms 23.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.

View guided diff

@cragwolfe cragwolfe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static review of 124560b7a785f10b9db22b77d71c15d3b6c68a1c against 5390c569625e8e66254a55a6446757b2d2abe492: I found no actionable Critical, High, or Medium defects in the supported list-valued metadata paths.

There is a compatibility decision for inputs outside the declared list types. Previously, a string or tuple passed as a concatenated metadata field raised an error; the new comprehension accepts it, and a string is split into characters. The constructor does not validate these types. Please decide whether accepting such inputs is acceptable or whether the prior fail-fast behavior should be retained. This is not a finding against the documented list contract.

The review was static; I did not run tests or benchmarks. CI status is separate from this assessment.

(authored by codex)

Afloat16 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

I chose to preserve the prior fail-fast behavior. Commit 3e92e833 checks that each concatenated metadata value is a list before flattening, so strings and tuples still raise TypeError; list subclasses continue to work. The linear list assembly and provenance deduplication are unchanged.

The new public-API cases cover both chunking strategies and all four concatenated fields, including empty/nonempty strings and tuples, list subclasses, duplicates, absent/empty lists and source ownership. Validation:

  • Original base 5390c569: all 40 new compatibility cases pass.
  • Reviewed head 124560b7: 32 invalid-input regressions fail; 16 controls pass.
  • Revised source: 436 passed, 16 skipped across the metadata and base chunking suites; all 48 metadata cases pass. The skips are optional pandas table-geometry checks in this focused environment. Ruff and formatting pass.

The tested source/test blob SHAs match the new PR head exactly. The validation workflow remains on a separate fork branch. I also updated the PR body to distinguish current validation from the initial benchmark and full-suite results.

The new-head upstream CI, CodeQL, Partition Benchmark, and Security workflows are awaiting approval (action_required). Could a maintainer approve those runs so upstream validation can proceed?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants