Skip to content

Fix quadratic CSVFormat.printWithEscapes(Reader) delimiter look-ahead - #631

Merged
garydgregory merged 1 commit into
apache:masterfrom
rootvector2:printwithescapes-reader-linear
Aug 8, 2026
Merged

Fix quadratic CSVFormat.printWithEscapes(Reader) delimiter look-ahead#631
garydgregory merged 1 commit into
apache:masterfrom
rootvector2:printwithescapes-reader-linear

Conversation

@rootvector2

Copy link
Copy Markdown
Contributor

the reader overload of printWithEscapes rebuilt a string from the whole accumulated output on every character to run the delimiter look-ahead, so escaping a large Reader value with no escapable characters ran in O(n^2). found while benchmarking format.print(Reader, out, true) in an escape format like MYSQL: a 0.8 MB field took ~28 s versus ~6 ms for the quoted path. the look-ahead only needs the current character and the peeked buffer, so test is built from those instead of the whole builder, which is the same match but linear (~19 ms at 0.8 MB).

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

rebuilding the test string from the whole accumulated builder made the loop O(n^2) for values without escapable characters; match the current char plus the look-ahead buffer instead.
@garydgregory garydgregory changed the title fix quadratic printWithEscapes(Reader) delimiter look-ahead Fix quadratic printWithEscapes(Reader) delimiter look-ahead Aug 8, 2026
@garydgregory
garydgregory merged commit 0c02827 into apache:master Aug 8, 2026
16 checks passed
@garydgregory

Copy link
Copy Markdown
Member

Good one @rootvector2 , thank you & merged 🚀

@garydgregory garydgregory changed the title Fix quadratic printWithEscapes(Reader) delimiter look-ahead Fix quadratic CSVFormat.printWithEscapes(Reader) delimiter look-ahead Aug 8, 2026
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