Skip to content

Threaded dispatcher with backpressure support#582

Merged
shikokuchuo merged 14 commits intomainfrom
dispatcher
Apr 9, 2026
Merged

Threaded dispatcher with backpressure support#582
shikokuchuo merged 14 commits intomainfrom
dispatcher

Conversation

@shikokuchuo
Copy link
Copy Markdown
Member

@shikokuchuo shikokuchuo commented Apr 8, 2026

Closes #454. Closes #581.

Summary

  • Dispatcher moves from a separate R process to an in-process thread (Threaded dispatcher #581). The dispatcher logic, already reimplemented in C (nanonext 1.8.0), now runs as a thread within the host process. This eliminates inter-process communication overhead for dispatching and status queries, using inproc:// transport instead of IPC sockets. info() reads dispatcher state directly rather than sending a query-response round trip.

  • New capacity argument for daemons() (Gating dispatcher based on uncollected results #454). Sets the maximum number of tasks (queued + executing) at the dispatcher. When the limit is reached, mirai() blocks until an existing task completes, providing backpressure to prevent unbounded memory growth. NULL (default) preserves existing unlimited queuing behavior.

Benefits

  • Lower dispatch overhead by eliminating the separate process and IPC round trips
  • Direct memory access for info() instead of send/recv query protocol
  • Backpressure prevents unbounded memory growth when producers outpace consumers (ML dataloaders, DB write pipelines, Shiny promise chains)
  • Simpler startup: no system2() call to spawn a dispatcher process, no multi-step synchronization handshake
  • Removes the exported dispatcher() function and R/dispatcher.R — net deletion of ~80 lines

fyi @lionel- this is a significant milestone for mirai

@wlandau
Copy link
Copy Markdown
Contributor

wlandau commented Apr 8, 2026

This is fantastic, @shikokuchuo! I notice a speedup in my long tests, and there info() no longer shows up in any of my profiling data. Really exciting work!

I do have a couple unit tests to update in crew because there are no longer any dispatcher processes, but that should be quick.

Copy link
Copy Markdown
Member Author

@shikokuchuo shikokuchuo left a comment

Choose a reason for hiding this comment

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

Revdeps coming in clean

@shikokuchuo shikokuchuo merged commit 3b07b6c into main Apr 9, 2026
12 checks passed
@shikokuchuo shikokuchuo deleted the dispatcher branch April 9, 2026 10:18
shikokuchuo added a commit that referenced this pull request Apr 9, 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.

Threaded dispatcher Gating dispatcher based on uncollected results

2 participants