Skip to content

turbo-tasks-backend: batch find_and_schedule_dirty using for_each_task_meta - #91497

Merged
sokra merged 7 commits into
canaryfrom
sokra/batch-find-and-schedule
Mar 19, 2026
Merged

sokra merged 7 commits into
canaryfrom
sokra/batch-find-and-schedule

Conversation

@sokra

@sokra sokra commented Mar 17, 2026 •

Copy link
Copy Markdown
Member

What?

Batch-process find_and_schedule_dirty in aggregation_update.rs by collecting all queued jobs (up to FIND_AND_SCHEDULE_BATCH_SIZE = 10 000) into a SmallVec and pre-fetching their task metadata with a batched ctx.for_each_task_meta(...) call.

Why?

find_and_schedule can accumulate thousands of tasks during invalidation cascades. The previous implementation issued one ctx.task(...) call per task inside process(), serializing backing-storage fetches one at a time.

ctx.for_each_task_meta triggers a batched fetch of all task metadata from the backing store: keys are sorted by hash for cache-friendly sequential access to the storage layer. The callback is invoked per-task once data is ready, with the task guard handed directly to the callback — no second lock acquisition needed.

The batch limit is set to 10 000, because find-and-schedule jobs are cheap (metadata read + optional schedule) compared to aggregation-update jobs, so yielding less often is safe and beneficial.

How?

fn process — find_and_schedule branch:

  • Replace the one-at-a-time loop with a single drain(..FIND_AND_SCHEDULE_BATCH_SIZE) that collects up to 10 000 FindAndScheduleJob structs into a SmallVec, then calls find_and_schedule_dirty once with the whole batch.

fn find_and_schedule_dirty:

  • Change parameter from task_id: TaskId to jobs: SmallVec<[FindAndScheduleJob; 4]>.
  • Call ctx.for_each_task_meta(...) to batch-prefetch all task metadata (sorted by hash for cache-friendly access) and process each task in the callback.

prepare_tasks_with_callback fix:

  • Release TaskLockCounter before calling prepared_task_callback instead of after. This ensures the counter is 0 when the callback runs, so callbacks that drop their task guard and then call ctx.task() (like find_and_schedule_dirty_internal → ctx.schedule()) no longer trigger the "Concurrent task lock acquisition detected" panic in debug builds.
  • for_each_task now uses acquire() instead of reacquire() since counter is guaranteed 0 at callback entry. reacquire() is removed.

Cleanup:

  • Use FxHashMap (already imported) instead of std::collections::HashMap.
  • Combine consecutive #[cfg(trace_find_and_schedule)] let bindings for clarity.
  • Add FIND_AND_SCHEDULE_BATCH_SIZE = 10_000 as a self-contained constant (not derived from MAX_COUNT_BEFORE_YIELD).

sokra and others added 2 commits March 17, 2026 07:59
Instead of calling find_and_schedule_dirty once per task in a loop,
collect up to MAX_COUNT_BEFORE_YIELD jobs into a SmallVec and process
them in a single batched call via ctx.for_each_task_meta, allowing the
backend to fetch all task data in one operation.

Also fixes a pre-existing bug where ctx.get_task_description(task_id)
was called on the ExecuteContext instead of the TaskGuard.

Co-Authored-By: Claude <noreply@anthropic.com>
…ocks

- Replace std::collections::HashMap with FxHashMap (already imported,
  consistent with the rest of the codebase)
- Move the Meta/All performance comment next to the for_each_task_meta call
- Combine two consecutive #[cfg(trace_find_and_schedule)] let bindings into
  a single tuple assignment so both span guards live until the end of the
  closure (clearer drop semantics, one cfg attribute instead of two)

Co-Authored-By: Claude <noreply@anthropic.com>
@nextjs-bot nextjs-bot added created-by: Turbopack team PRs by the Turbopack team. Turbopack Related to Turbopack with Next.js. labels Mar 17, 2026
@nextjs-bot

nextjs-bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Tests Passed

@nextjs-bot

nextjs-bot commented Mar 17, 2026 •

Copy link
Copy Markdown
Contributor

Stats from current PR

🟢 1 improvement

Metric Canary PR Change Trend
node_modules Size 484 MB 483 MB 🟢 394 kB (0%) █████
📊 All Metrics
📖 Metrics Glossary

Dev Server Metrics:

  • Listen = TCP port starts accepting connections
  • First Request = HTTP server returns successful response
  • Cold = Fresh build (no cache)
  • Warm = With cached build artifacts

Build Metrics:

  • Fresh = Clean build (no .next directory)
  • Cached = With existing .next directory

Change Thresholds:

  • Time: Changes < 50ms AND < 10%, OR < 2% are insignificant
  • Size: Changes < 1KB AND < 1% are insignificant
  • All other changes are flagged to catch regressions

⚡ Dev Server

Metric Canary PR Change Trend
Cold (Listen) 455ms 456ms ✓ ▁▁█▁▁
Cold (Ready in log) 440ms 439ms ✓ ▁▁▇▁▁
Cold (First Request) 1.117s 1.116s ✓ ▂▁█▂▁
Warm (Listen) 457ms 456ms ✓ ▁▁█▁▁
Warm (Ready in log) 443ms 446ms ✓ ▁▁█▁▁
Warm (First Request) 344ms 343ms ✓ ▁▁█▁▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 456ms 454ms ✓ █▁▁▁█
Cold (Ready in log) 438ms 438ms ✓ ▁▁▁▁▁
Cold (First Request) 1.874s 1.889s ✓ ████▃
Warm (Listen) 457ms 455ms ✓ ▅▅▅▅▁
Warm (Ready in log) 438ms 437ms ✓ ▁▁▁▁▁
Warm (First Request) 1.904s 1.894s ✓ ▇██▇▃

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 3.817s 3.832s ✓ ▁▁█▁▁
Cached Build 3.807s 3.751s ✓ ▁▁█▁▁
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 14.384s 14.378s ✓ ▄▄▄▄▃
Cached Build 14.458s 14.461s ✓ ▅▅▄▄▄
node_modules Size 484 MB 483 MB 🟢 394 kB (0%) █████
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
0-a3rz67ec0it.js gzip 157 B N/A -
0~lwfcrlb4v_9.css gzip 115 B 115 B ✓
00h0nz7r436~l.js gzip 13.3 kB N/A -
00ivb_iunbucu.js gzip 13 kB N/A -
02ku7edzc_wf7.js gzip 450 B N/A -
03~yq9q893hmn.js gzip 39.4 kB 39.4 kB ✓
037mxw~u2_79t.js gzip 154 B N/A -
08wow2p6zxy.b.js gzip 7.61 kB N/A -
092lcb3fqrrf9.js gzip 8.52 kB N/A -
0aj~xs1l1g8tg.js gzip 8.53 kB N/A -
0gob4q88vgv3u.js gzip 156 B N/A -
0h35gmp9u328z.js gzip 8.54 kB N/A -
0h6fkavebp.iz.js gzip 8.47 kB N/A -
0hcg0snee_9wv.js gzip 156 B N/A -
0i8jiw50w3l38.js gzip 154 B N/A -
0ino_yf1k3h6k.js gzip 10.4 kB N/A -
0kkeoe2n.293z.js gzip 160 B N/A -
0l~j-k_rjuult.js gzip 70.8 kB N/A -
0mc16gv2x1bet.js gzip 13.7 kB N/A -
0mcszt6vwd60_.js gzip 154 B N/A -
0mgzv7x.0y719.js gzip 169 B N/A -
0moy~uao4dl.m.js gzip 9.19 kB N/A -
0n5ln2l5jgra~.js gzip 152 B N/A -
0ovzdapbcjgc7.js gzip 65.7 kB N/A -
0p2fwrxw124by.js gzip 160 B N/A -
0q50rtpusjy90.js gzip 2.28 kB N/A -
0smgy2grrrlka.js gzip 8.58 kB N/A -
0t1dzhdfh0txh.js gzip 215 B 215 B ✓
0vt7pofxnk8in.js gzip 10.1 kB N/A -
0zid7o0-vupvp.js gzip 225 B N/A -
1030wmumq.hbq.js gzip 156 B N/A -
11yo3xfd6b147.js gzip 12.9 kB N/A -
13.84hqxl_1p7.js gzip 9.76 kB N/A -
13ddjl2tc8beg.js gzip 153 B N/A -
14_hwphcs58-s.js gzip 48.6 kB N/A -
1554wr-t7p6z-.js gzip 8.55 kB N/A -
15pd.z8aymtma.js gzip 155 B N/A -
15tjst79~qy3_.js gzip 1.46 kB N/A -
15z_v00ne4ud0.js gzip 8.47 kB N/A -
17d_m3p4j9w6r.js gzip 5.62 kB N/A -
17yu~3yiu7d2m.js gzip 8.52 kB N/A -
turbopack-0-..rr~~.js gzip 4.15 kB N/A -
turbopack-01..zoj7.js gzip 4.16 kB N/A -
turbopack-01..cl9..js gzip 4.16 kB N/A -
turbopack-0d..r9ub.js gzip 4.15 kB N/A -
turbopack-0f..1w4v.js gzip 4.15 kB N/A -
turbopack-0g..5lrl.js gzip 4.16 kB N/A -
turbopack-0l..d~my.js gzip 4.14 kB N/A -
turbopack-0l..aco3.js gzip 4.16 kB N/A -
turbopack-0p..4qy0.js gzip 4.17 kB N/A -
turbopack-0p..y0cg.js gzip 4.16 kB N/A -
turbopack-0q..cwm4.js gzip 4.15 kB N/A -
turbopack-0z..uyvd.js gzip 4.16 kB N/A -
turbopack-10..ov9~.js gzip 4.16 kB N/A -
turbopack-15..lg62.js gzip 4.16 kB N/A -
03t__~.5lvgeu.js gzip N/A 5.62 kB -
04d6ll75jqx3r.js gzip N/A 9.19 kB -
0583exyh-yhc7.js gzip N/A 9.76 kB -
072lv63r8dcz~.js gzip N/A 8.58 kB -
07ck-m4a5-5so.js gzip N/A 168 B -
07k6dcww5s4pu.js gzip N/A 13.7 kB -
0ar1~bwpezfgw.js gzip N/A 13.3 kB -
0c99mq1ez2bke.js gzip N/A 450 B -
0cbo6rhjf7w-6.js gzip N/A 156 B -
0cq-cmde_ws6u.js gzip N/A 8.47 kB -
0fwf102w10o9~.js gzip N/A 8.52 kB -
0gtmn.q_j1v5r.js gzip N/A 10.4 kB -
0h5~v-tahitcf.js gzip N/A 10.1 kB -
0j3.53ftqt4y3.js gzip N/A 155 B -
0ko7npzyoqvxn.js gzip N/A 156 B -
0l4d~z7ixj3wz.js gzip N/A 12.9 kB -
0le9gtymglk8m.js gzip N/A 65.7 kB -
0nclq9z6yzzm5.js gzip N/A 1.46 kB -
0nkn_f7bxhnni.js gzip N/A 153 B -
0nzumcogektg7.js gzip N/A 8.55 kB -
0s.c-cn5eebrx.js gzip N/A 8.47 kB -
0s6.ndj~wg5qp.js gzip N/A 70.8 kB -
0tj0jb~karm2q.js gzip N/A 157 B -
0tna7lg6q4zne.js gzip N/A 12.9 kB -
0vgt1zz9fdplj.js gzip N/A 160 B -
0votdfxr5fb5u.js gzip N/A 2.28 kB -
0wta5m-wk8slp.js gzip N/A 156 B -
0y6di8fz_8qyy.js gzip N/A 48.4 kB -
0y6r7p5ybjw0c.js gzip N/A 155 B -
0ykl9bs_qj.5..js gzip N/A 8.52 kB -
0zfen0tnxp4gh.js gzip N/A 8.55 kB -
10wkq1h9jzkg..js gzip N/A 225 B -
12u_his5dkcg..js gzip N/A 163 B -
13x7j-eiy1i9a.js gzip N/A 158 B -
149ndfh8zfcaz.js gzip N/A 8.53 kB -
168xlbkju2cj0.js gzip N/A 7.6 kB -
182e911.xpa71.js gzip N/A 156 B -
18c1kf~drwh4e.js gzip N/A 156 B -
turbopack-0....5ku.js gzip N/A 4.16 kB -
turbopack-04..oqi6.js gzip N/A 4.16 kB -
turbopack-05..6cp7.js gzip N/A 4.16 kB -
turbopack-09..mwo1.js gzip N/A 4.16 kB -
turbopack-0a..ba9o.js gzip N/A 4.16 kB -
turbopack-0c..0f0j.js gzip N/A 4.16 kB -
turbopack-0h..n9zu.js gzip N/A 4.16 kB -
turbopack-0p.._66z.js gzip N/A 4.16 kB -
turbopack-0q..rbnk.js gzip N/A 4.14 kB -
turbopack-0u..x~nx.js gzip N/A 4.16 kB -
turbopack-0v..hq2f.js gzip N/A 4.16 kB -
turbopack-0z..szyb.js gzip N/A 4.16 kB -
turbopack-10..qhh7.js gzip N/A 4.17 kB -
turbopack-13..lfek.js gzip N/A 4.16 kB -
Total 463 kB 463 kB ✅ -199 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 711 B 711 B ✓
Total 711 B 711 B ✓
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 433 B 429 B ✓
Total 433 B 429 B ✅ -4 B

📦 Webpack

Client

Main Bundles
Canary PR Change
5528-HASH.js gzip 5.54 kB N/A -
6280-HASH.js gzip 60.4 kB N/A -
6335.HASH.js gzip 169 B N/A -
912-HASH.js gzip 4.59 kB N/A -
e8aec2e4-HASH.js gzip 62.7 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB ✓
main-app-HASH.js gzip 255 B 253 B ✓
main-HASH.js gzip 39.3 kB 39.2 kB ✓
webpack-HASH.js gzip 1.68 kB 1.68 kB ✓
262-HASH.js gzip N/A 4.59 kB -
2889.HASH.js gzip N/A 169 B -
5602-HASH.js gzip N/A 5.55 kB -
6948ada0-HASH.js gzip N/A 62.7 kB -
9544-HASH.js gzip N/A 60.9 kB -
Total 234 kB 235 kB ⚠️ +445 B
Polyfills
Canary PR Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB ✓
Total 39.4 kB 39.4 kB ✓
Pages
Canary PR Change
_app-HASH.js gzip 194 B 194 B ✓
_error-HASH.js gzip 183 B 180 B 🟢 3 B (-2%)
css-HASH.js gzip 331 B 330 B ✓
dynamic-HASH.js gzip 1.81 kB 1.81 kB ✓
edge-ssr-HASH.js gzip 256 B 256 B ✓
head-HASH.js gzip 351 B 352 B ✓
hooks-HASH.js gzip 384 B 383 B ✓
image-HASH.js gzip 580 B 581 B ✓
index-HASH.js gzip 260 B 260 B ✓
link-HASH.js gzip 2.51 kB 2.51 kB ✓
routerDirect..HASH.js gzip 320 B 319 B ✓
script-HASH.js gzip 386 B 386 B ✓
withRouter-HASH.js gzip 315 B 315 B ✓
1afbb74e6ecf..834.css gzip 106 B 106 B ✓
Total 7.98 kB 7.98 kB ✅ -1 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 125 kB 125 kB ✓
page.js gzip 269 kB 268 kB ✓
Total 394 kB 393 kB ✅ -650 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 616 B 615 B ✓
middleware-r..fest.js gzip 156 B 155 B ✓
middleware.js gzip 43.7 kB 44 kB ✓
edge-runtime..pack.js gzip 842 B 842 B ✓
Total 45.3 kB 45.6 kB ⚠️ +324 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 715 B 718 B ✓
Total 715 B 718 B ⚠️ +3 B
Build Cache
Canary PR Change
0.pack gzip 4.28 MB 4.26 MB 🟢 23.5 kB (-1%)
index.pack gzip 111 kB 110 kB ✓
index.pack.old gzip 110 kB 110 kB ✓
Total 4.5 MB 4.48 MB ✅ -23.6 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 333 kB 333 kB ✓
app-page-exp..prod.js gzip 181 kB 181 kB ✓
app-page-tur...dev.js gzip 333 kB 332 kB ✓
app-page-tur..prod.js gzip 181 kB 181 kB ✓
app-page-tur...dev.js gzip 329 kB 329 kB ✓
app-page-tur..prod.js gzip 179 kB 179 kB ✓
app-page.run...dev.js gzip 330 kB 329 kB ✓
app-page.run..prod.js gzip 179 kB 179 kB ✓
app-route-ex...dev.js gzip 76.1 kB 76 kB ✓
app-route-ex..prod.js gzip 51.8 kB 51.7 kB ✓
app-route-tu...dev.js gzip 76.1 kB 76 kB ✓
app-route-tu..prod.js gzip 51.8 kB 51.7 kB ✓
app-route-tu...dev.js gzip 75.7 kB 75.6 kB ✓
app-route-tu..prod.js gzip 51.6 kB 51.5 kB ✓
app-route.ru...dev.js gzip 75.7 kB 75.6 kB ✓
app-route.ru..prod.js gzip 51.5 kB 51.5 kB ✓
dist_client_...dev.js gzip 324 B 324 B ✓
dist_client_...dev.js gzip 326 B 326 B ✓
dist_client_...dev.js gzip 318 B 318 B ✓
dist_client_...dev.js gzip 317 B 317 B ✓
pages-api-tu...dev.js gzip 43.4 kB 43.3 kB ✓
pages-api-tu..prod.js gzip 33 kB 33 kB ✓
pages-api.ru...dev.js gzip 43.3 kB 43.3 kB ✓
pages-api.ru..prod.js gzip 33 kB 33 kB ✓
pages-turbo....dev.js gzip 52.7 kB 52.7 kB ✓
pages-turbo...prod.js gzip 38.6 kB 38.6 kB ✓
pages.runtim...dev.js gzip 52.7 kB 52.7 kB ✓
pages.runtim..prod.js gzip 38.6 kB 38.6 kB ✓
server.runti..prod.js gzip 62.4 kB 62.4 kB ✓
Total 2.95 MB 2.95 MB ✅ -3.58 kB
📝 Changed Files (21 files)

Files with changes:

  • app-page-exp..ntime.dev.js
  • app-page-exp..time.prod.js
  • app-page-tur..ntime.dev.js
  • app-page-tur..time.prod.js
  • app-page-tur..ntime.dev.js
  • app-page-tur..time.prod.js
  • app-page.runtime.dev.js
  • app-page.runtime.prod.js
  • app-route-ex..ntime.dev.js
  • app-route-ex..time.prod.js
  • app-route-tu..ntime.dev.js
  • app-route-tu..time.prod.js
  • app-route-tu..ntime.dev.js
  • app-route-tu..time.prod.js
  • app-route.runtime.dev.js
  • app-route.ru..time.prod.js
  • pages-api-tu..ntime.dev.js
  • pages-api.runtime.dev.js
  • pages-turbo...ntime.dev.js
  • pages.runtime.dev.js
  • ... and 1 more
View diffs
app-page-exp..ntime.dev.js
failed to diff
app-page-exp..time.prod.js
failed to diff
app-page-tur..ntime.dev.js
failed to diff
app-page-tur..time.prod.js
failed to diff
app-page-tur..ntime.dev.js
failed to diff
app-page-tur..time.prod.js
failed to diff
app-page.runtime.dev.js
failed to diff
app-page.runtime.prod.js
failed to diff
app-route-ex..ntime.dev.js

Diff too large to display

app-route-ex..time.prod.js

Diff too large to display

app-route-tu..ntime.dev.js

Diff too large to display

app-route-tu..time.prod.js

Diff too large to display

app-route-tu..ntime.dev.js

Diff too large to display

app-route-tu..time.prod.js

Diff too large to display

app-route.runtime.dev.js

Diff too large to display

app-route.ru..time.prod.js

Diff too large to display

pages-api-tu..ntime.dev.js

Diff too large to display

pages-api.runtime.dev.js

Diff too large to display

pages-turbo...ntime.dev.js

Diff too large to display

pages.runtime.dev.js

Diff too large to display

server.runtime.prod.js

Diff too large to display

📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/b74fb374002941f00bae956ec42396ef5116ae42/next

@codspeed

codspeed Bot commented Mar 17, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing sokra/batch-find-and-schedule (b74fb37) with canary (62393b0)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

sokra and others added 3 commits March 17, 2026 08:43
…sk() instead of for_each_task_meta

for_each_task_meta holds the TaskLockCounter elevated for the entire
duration of its callback. find_and_schedule_dirty_internal drops the task
guard and calls ctx.schedule(), which internally calls ctx.task() —
incrementing the counter a second time and triggering the "Concurrent task
lock acquisition detected" panic in debug builds.

Switch to ctx.prepare_tasks() for the parallel prefetch (preserving the
batch I/O benefit) and then iterate with individual ctx.task() calls so
the counter returns to zero between each task, matching the original
one-at-a-time access pattern that schedule() expects.

Co-Authored-By: Claude <noreply@anthropic.com>
find_and_schedule jobs are cheaper than aggregation updates — they only
read task metadata and optionally schedule a task — so we can process
10x more per process() call (10 000 vs 1 000) before yielding.

Co-Authored-By: Claude <noreply@anthropic.com>
Use a standalone literal value (10000) instead of deriving from
MAX_COUNT_BEFORE_YIELD, making the constant self-contained.

Co-Authored-By: Claude <noreply@anthropic.com>
@sokra
sokra marked this pull request as ready for review March 17, 2026 19:30
@sokra
sokra requested a review from lukesandberg March 17, 2026 19:30
Comment thread turbopack/crates/turbo-tasks-backend/src/backend/operation/aggregation_update.rs Outdated
sokra and others added 2 commits March 17, 2026 20:49
…or cache-friendly sequential access

Co-Authored-By: Claude <noreply@anthropic.com>
Release the TaskLockCounter before calling prepared_task_callback instead
of after. This ensures the counter is 0 when the callback runs, so
callbacks that drop their task guard and then call ctx.task() (like
find_and_schedule_dirty_internal → ctx.schedule()) no longer trigger the
"Concurrent task lock acquisition detected" panic.

This lets find_and_schedule_dirty use for_each_task_meta directly,
reverting the workaround from b576d8e that bypassed for_each_task_meta
and called ctx.task() individually after a manual ctx.prepare_tasks().

The for_each_task callback now uses acquire() instead of reacquire() since
the counter is guaranteed to be 0 at callback entry. reacquire() is removed
as it has no remaining callers.
@sokra
sokra enabled auto-merge (squash) March 19, 2026 07:58
@sokra
sokra merged commit c13536d into canary Mar 19, 2026
282 of 287 checks passed
@sokra
sokra deleted the sokra/batch-find-and-schedule branch March 19, 2026 08:43
@github-actions github-actions Bot added the locked label Apr 2, 2026
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

created-by: Turbopack team PRs by the Turbopack team. locked Turbopack Related to Turbopack with Next.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants