Skip to content

Trim CSV blank runs without repeatedly shifting the row list - #2450

Merged
afourney merged 2 commits into
microsoft:mainfrom
ROTl24:perf/csv-blank-row-trimming
Sep 11, 2026
Merged

Trim CSV blank runs without repeatedly shifting the row list#2450
afourney merged 2 commits into
microsoft:mainfrom
ROTl24:perf/csv-blank-row-trimming

Conversation

@ROTl24

Copy link
Copy Markdown
Contributor

Fixes #2449.

CSV conversion repeatedly shifts the row list when removing blank lines before the header or immediately after it. A long blank run therefore takes quadratic time even though the resulting table is tiny.

This change finds the boundaries first and deletes each run in one slice. The helper still mutates the original list and preserves the header, internal blank rows, and existing row objects.

Four conversion tests cover 100,000 blank lines before the header, after the header, at the end, and throughout the file. The table cases also check that an internal blank row and a wider data row survive. There is no timing assertion in the tests.

Validation on Windows / Python 3.13.15:

  • 37 CSV tests passed; pre-commit run --all-files passed.
  • An exhaustive comparison of 511 blank/nonblank row arrangements matched the original helper, including row identity.
  • Using the reproduction in CSV conversion scales quadratically with leading blank lines #2449, the median for 100,000 leading blank lines dropped from 0.6953 s to 0.0084 s across three runs. This is a synthetic case, not a general document-conversion benchmark.

The full optional-format and remote-service test suite was not run locally.

Prepared with OpenAI Codex assistance.

CSV 清理函数逐次 pop(0) 或 pop(1),每次都移动剩余元素,导致大量前导空行或表头后空行的转换耗时呈平方级增长。

改为先定位表头、数据起点和末尾,再逆序删除空行区间;保留内部空行、表头及行对象,不改变列宽或转义逻辑。新增四个十万空行场景,覆盖前导、表头后、尾部与全空输入,同时检查内部空行和宽行保留行为。

验证:37 项 CSV 测试通过;511 种空行排列与基线函数输出和对象身份一致;pre-commit 全文件检查及 diff 空白检查通过。Windows / Python 3.13.15 的合成输入测量中,十万前导空行的真实转换入口三次中位数从 0.6953 秒降至 0.0084 秒。

未运行整个项目的可选格式、远程服务或付费模型测试。性能测量针对合成空行输入,不代表普通文档的整体提速。
@ROTl24

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Copilot AI 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.

🟢 Approval recommended

The implementation preserves existing semantics, resolves the quadratic behavior, and includes focused regression coverage.

Pull request overview

Optimizes CSV blank-row trimming from quadratic to linear behavior while preserving conversion output and row identity.

Changes:

  • Deletes blank-row runs using slices rather than repeated list shifts.
  • Adds regression coverage for long leading, post-header, trailing, and all-blank inputs.
File summaries
File Description
packages/markitdown/src/markitdown/converters/_csv_converter.py Implements efficient in-place blank-run trimming.
packages/markitdown/tests/test_csv_blank_runs.py Tests long blank runs and content preservation.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@afourney
afourney merged commit f0c5d01 into microsoft:main Sep 11, 2026
20 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.

CSV conversion scales quadratically with leading blank lines

3 participants