Optimizing [sqlx:test] rebuild time
3 days ago
- #SQLX
- #Optimization
- #Rust
- The author experienced slow incremental rebuild times (~8-10s) for tests in the bors project, partly due to SQLX test compilation.
- Investigation revealed that #[sqlx::test] macros generate large amounts of code by embedding migration data (like SQL and checksums) directly into each test, increasing compilation time and code size.
- With many tests (~350) and migrations (~30), this led to significant bloat—cargo expand output was 32 MiB, slowing rebuilds.
- A solution was found: using a shared migrator variable (defined via sqlx::migrate!()) and referencing it in tests with migrator = "crate::MIGRATOR" to avoid duplicating migration data.
- This reduced rebuild time from ~7.5s to ~5s and cut expanded code size to ~6 MiB, though the author suggests making the shared migrator a default via sqlx.toml configuration.
- The post advises using the migrator trick for projects with many SQLX tests to optimize rebuild times and notes that cargo expand size correlates with compilation speed.