Skip to content

Fix wrangler d1 execute performance for local - #15557

Merged
NuroDev merged 4 commits into
cloudflare:mainfrom
tomekancu:fix-d1-local-execute-performance
Sep 8, 2026
Merged

NuroDev merged 4 commits into
cloudflare:mainfrom
tomekancu:fix-d1-local-execute-performance

Conversation

@tomekancu

@tomekancu tomekancu commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #15556.

Root cause

splitSqlIntoStatements() in packages/wrangler/src/d1/splitter.ts consumes quoted strings and comments character-by-character via consumeWhile() / consumeUntilMarker().
Each iteration appends to an ever-growing str and then evaluates the predicate against the whole accumulated string (e.g. str.endsWith(endMarker)), forcing the JS engine to flatten/copy the growing string on every character — O(n²) in the length of the quoted value or comment.
This only bites the local path because remote file execution never runs the client-side splitter.

Measurements

Timing splitSqlQuery() on a single 256 KB quoted value / a 256 KB -- comment:

Input before (wrangler 4.129) after fix
single ~256 KB quoted value ~17–24 s ~20–40 ms
~256 KB line comment ~18–19 s ~17 ms
5k small INSERTs ~40 ms ~40 ms

  • Tests
    • Tests included/updated
    • Automated tests not possible - manual testing has been completed as follows:
    • Additional testing not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: it doesn't change API

Devin Review

@changeset-bot

changeset-bot Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: df355ea

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
wrangler Patch
@cloudflare/vite-plugin Patch
@cloudflare/vitest-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-project-automation github-project-automation Bot moved this to Untriaged in workers-sdk Sep 7, 2026
@workers-devprod
workers-devprod requested review from a team and NuroDev and removed request for a team September 7, 2026 17:22
@workers-devprod

workers-devprod commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Codeowners approval required for this PR:

  • @cloudflare/d1
  • ✅ @cloudflare/wrangler
Show detailed file reviewers
  • packages/wrangler/src/tests/d1/splitter.test.ts: [@cloudflare/d1]
  • packages/wrangler/src/d1/splitter.ts: [@cloudflare/d1]

@devin-ai-integration devin-ai-integration Bot 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.

🔍 Devin Review: 2 flags

Not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

@pkg-pr-new

pkg-pr-new Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
@cloudflare/autoconfig

npm i https://pkg.pr.new/@cloudflare/autoconfig@15557

@cloudflare/build-output-utils

npm i https://pkg.pr.new/@cloudflare/build-output-utils@15557

@cloudflare/codemods

npm i https://pkg.pr.new/@cloudflare/codemods@15557

@cloudflare/config

npm i https://pkg.pr.new/@cloudflare/config@15557

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@15557

@cloudflare/deploy-helpers

npm i https://pkg.pr.new/@cloudflare/deploy-helpers@15557

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@15557

miniflare

npm i https://pkg.pr.new/miniflare@15557

@cloudflare/pages-functions

npm i https://pkg.pr.new/@cloudflare/pages-functions@15557

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@15557

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@15557

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@15557

@cloudflare/vitest-plugin

npm i https://pkg.pr.new/@cloudflare/vitest-plugin@15557

@cloudflare/workers-auth

npm i https://pkg.pr.new/@cloudflare/workers-auth@15557

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@15557

@cloudflare/workers-utils

npm i https://pkg.pr.new/@cloudflare/workers-utils@15557

wrangler

npm i https://pkg.pr.new/wrangler@15557

commit: df355ea

@NuroDev NuroDev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @tomekancu 🧡

@NuroDev
NuroDev enabled auto-merge (squash) September 8, 2026 14:00

@NuroDev NuroDev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Codeowners Bypass

@workers-devprod workers-devprod 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.

Codeowners reviews satisfied

@github-project-automation github-project-automation Bot moved this from Untriaged to Approved in workers-sdk Sep 8, 2026
@NuroDev
NuroDev merged commit 63c7ff1 into cloudflare:main Sep 8, 2026
82 of 86 checks passed
@github-project-automation github-project-automation Bot moved this from Approved to Done in workers-sdk Sep 8, 2026
@tomekancu
tomekancu deleted the fix-d1-local-execute-performance branch September 8, 2026 14:28
mittalpk added a commit to mittalpk/workers-sdk that referenced this pull request Sep 17, 2026
isCompoundStatementStart/End used [^A-Za-z0-9_] as the identifier boundary,
but SQLite's own unquoted-identifier syntax allows $ and any code point at
or above U+0080. An identifier like foo$CASE or ENDα was misread as the
BEGIN/CASE/END keyword, corrupting the compound-statement nesting stack for
the rest of the file.

Extract the boundary into a single SQL_IDENTIFIER_CHAR class shared by both
functions so they can't drift out of sync again -- that exact drift (one
regex broadened, the other left narrow) is what caused the CASE/END
regression this PR already fixes.

Precompile both regexes at module scope: isCompoundStatementStart runs on
every character of the input in the main split loop, so constructing a new
RegExp inside the function would recompile it per character.

Adds 2 regression tests. Also resolves the merge conflict with cloudflare#15557's
consumeWhile windowing, which touched the same test file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

wrangler d1 execute --file is slower with --local than --remote on large SQL files

3 participants