perf: reduce SQL round-trips and CPU overhead in scheduler hot paths#60
Merged
Conversation
- Use result.last_insert_rowid() in insert_history instead of separate SELECT query, saving 1 round-trip per task completion - Replace generic chrono datetime parser with fast fixed-position byte parser for the known SQLite format - Add has_hierarchy flag to skip active_children_count query when no parent-child tasks exist - Batch dependency resolution into 2 queries (DELETE RETURNING + single UPDATE RETURNING) instead of 2+2N - Add fast dispatch path using pop_next() (1 SQL) instead of peek_next + gate + claim_task (2 SQL) when no groups, pressure sources, or module caps are configured bench_dispatch_no_groups_500: ~166ms → ~128ms (-23%)
Merged
deepjoy
pushed a commit
that referenced
this pull request
Mar 19, 2026
## 🤖 New release * `taskmill`: 0.5.1 -> 0.5.2 (✓ API compatible changes) <details><summary><i><b>Changelog</b></i></summary><p> <blockquote> ## [0.5.2](v0.5.1...v0.5.2) - 2026-03-19 ### Other - reduce SQL round-trips and CPU overhead in scheduler hot paths ([#60](#60)) - coalesce task completions into batched transactions ([#59](#59)) - reduce SQL round-trips in scheduler hot paths ([#57](#57)) </blockquote> </p></details> --- This PR was generated with [release-plz](https://github.com/release-plz/release-plz/). Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
dispatch_no_groups_500latency by ~23% (166ms → 128ms)chronodatetime parser with a fixed-position byte parser for the known SQLite formathas_hierarchyfast-path flag (following existinghas_tagspattern) to skip theactive_children_countquery when no parent-child tasks existDELETE … RETURNING+ singleUPDATE … RETURNINGfast_dispatchmode that usespop_next()(1 SQL) instead ofpeek_next()+ gate +claim_task()(2 SQL) when no groups, pressure sources, or module caps are configuredDetails
1. Inline
last_insert_rowid(store/lifecycle/mod.rs)insert_historywas issuing a separateSELECT last_insert_rowid()query after every INSERT intotask_history. TheSqliteQueryResultalready carries this value — useresult.last_insert_rowid()directly, matching the existing pattern incomplete_inner.2. Fast datetime parsing (
store/row_mapping.rs)parse_datetimewas callingchrono::NaiveDateTime::parse_from_strwith a fallback — a generic parser invoked 2-4× perrow_to_task_record. Replaced with a fixed-position byte parser that handles both"YYYY-MM-DD HH:MM:SS"and"YYYY-MM-DD HH:MM:SS.fff…".3.
has_hierarchyflag (store/mod.rs,scheduler/spawn/completion.rs)handle_successwas callingactive_children_count(aSELECT COUNT(*)query) for every task completion, even when no tasks use parent-child hierarchy. Added anArc<AtomicBool>flag toTaskStore— set totruewhen a task withparent_idis submitted — and skip the query whenfalse. Follows the existinghas_tagspattern exactly.4. Batched dependency resolution (
store/dependencies.rs)resolve_dependents_innerpreviously used SELECT + DELETE + per-dependent COUNT + UPDATE (2+2N queries). Now usesDELETE FROM task_deps … RETURNING task_idfollowed by a singleUPDATE tasks … WHERE id IN (…) AND NOT EXISTS (SELECT 1 FROM task_deps …) RETURNING id— always 2 queries regardless of fan-out. Applied the sameDELETE … RETURNINGoptimization tofail_dependents_inner.5. Fast dispatch path (
scheduler/run_loop.rs,scheduler/builder.rs)When no groups, pressure sources, resource monitoring, or module caps are configured,
try_dispatchnow usespop_next()(atomic UPDATE+RETURNING, 1 SQL) instead ofpeek_next()+gate.admit()+claim_task()(2 SQL). Added an expiry filter topop_next()'s inner SELECT so expired tasks are safely skipped. The slow path is preserved unchanged as a fallback.Benchmark results
dispatch_no_groups_500dispatch_one_group_500dispatch_group_scaling/100dep_fan_in_dispatch/50dep_fan_in_dispatch/100