Skip to content

perf(chunking): avoid repeated table scans while forming chunks - #4744

Merged
cragwolfe merged 1 commit into
Unstructured-IO:mainfrom
Afloat16:linear-chunk-table-detection-20261114
Nov 17, 2026
Merged

cragwolfe merged 1 commit into
Unstructured-IO:mainfrom
Afloat16:linear-chunk-table-detection-20261114

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Problem

With table isolation enabled (the default), PreChunkBuilder.will_fit() scans every accumulated element to decide whether a table is already present. Repeating this for every new element makes admission quadratic in the number of elements in a pre-chunk. Text-size limits do not bound a run of images without extracted text, and the default include_orig_elements=True retains those images individually.

For 300 empty images followed by one text element, the existing public chunking path performs 45,452 table-family checks.

Change

Record whether a table-family element has been added, update that flag during admission, and reset it with the rest of the builder state before flush() yields. The existing isinstance(Table) predicate also covers TableChunk subclasses. No original elements, metadata, chunk boundaries, overlap rules or table-isolation behavior are changed.

This is separate from #4517, which compacts excluded image payloads when include_orig_elements=False; the repeated scan still affects the default mode that preserves originals.

Validation

  • A deterministic linear-work budget exercises both the list and lazy public chunking APIs. On unchanged source: 2 failing work-bound regressions and 4 passing controls. The two failures perform 45,452 checks instead of meeting a 1,204-check bound.
  • Controls cover the existing compacted-image path, preserved source object identity, metadata and the reset timing for Table/TableChunk before a flush iterator is exhausted.
  • 510 tests pass across the new regressions and existing base, basic, title and table-isolation suites.
  • Ruff lint/format and git diff --check pass. An independent read-only review found no state-lifecycle regression. No complete repository suite was run.

Measurements

Local Python 3.12, seven alternating before/after pairs with collection before each call. The benchmark invokes public chunk_elements() using the exact baseline/current builder methods; input construction and result serialization are outside the timer. Images contain empty text and page-number metadata, followed by one visible text element.

Input Preserve originals Before median After median
100 images Yes (default) 1.162 ms 0.379 ms
1,000 images Yes (default) 78.745 ms 2.369 ms
5,000 images Yes (default) 2,029.488 ms 11.035 ms
5,000 images No 22.358 ms 21.071 ms
5,000 short text elements, 20,000-character window Yes 1,959.048 ms 11.632 ms

All serialized content and metadata fingerprints match after excluding only newly generated chunk UUIDs, which differ between identical unmodified calls too. Original-element metadata remains in the comparison. These synthetic measurements cover chunking already-partitioned elements, not file parsing or whole ingestion throughput.

Suggested label: enhancement.

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.

Reviewed the chunking change and its surrounding table, metadata, and lazy execution paths. I found no actionable Critical, High, or Medium issues. The cached table flag follows the existing table-family predicate and resets with the builder before a flushed chunk is yielded.

This is a static review of commit dd011e0. Workflow approval and application CI are still pending, so this review does not establish integration readiness.

(authored by codex)

Copy link
Copy Markdown
Contributor Author

Thanks for the review. The hosted checks have now completed on the approved revision dd011e05a66ac9c6033333693b95fb6914cefcae: CI, CodeQL, and Partition Benchmark are successful. All 50 reported check runs are successful, including the Python 3.11–3.13 unit-test matrix.

The separate Security workflow still reports action_required on the same revision. Could a maintainer approve this remaining workflow and apply the performance category label? The code is unchanged since your approval.

@cragwolfe
cragwolfe added this pull request to the merge queue Nov 17, 2026
Merged via the queue into Unstructured-IO:main with commit cb686ff Nov 17, 2026
52 checks passed
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