refactor: Keep parsed sqlx-data.json
in a cache instead of reparsing
#1684
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.
I didn't delve too much into how things are used, but from what I can tell when using the offline data cache each compile-time verified query will parse the entire
sqlx-data.json
file. This change switches to caching the parsedsqlx-data.json
files in a two-layer cache where the first layer is keyed by the path tosqlx-data.json
(since there can be multiple) and the second layer is keyed by the query hash.This did involve changing some of the types returned for errors since this now uses
String
s instead ofserde::de::Err::custom
. I didn't think this was a huge issue since the error type is aBox<dyn std::error::Error>
.Comparison
Note: the numbers here are ballpark around what I see at work. We have a workspace with over 200 queries that uses
--merged
to create a workspace levelsqlx-data.json
file. This file ends up being a hair over 500 KiBSetup
Cargo.toml
main.rs
is generated with a python file calledgen_main.py
I used 200 queries for this test so
./gen_main.py 200 > main.rs
makes the main. Here's an example of./gen_main.py 1
and then here is the table for the sqlite database, just created on the command line with
$ sqlite3 repro.db
Running
With all the setup out of the way here are some of the numbers. Both of the check times are from
$ hyperfine --warmup 1 --prepare 'touch src/main.rs' 'cargo check'
sqlx-data.json
size: 305 KiBcargo check
onmaster
:(mean ± σ): 6.298 s ± 0.035 s
cargo check
on this branch:(mean ± σ): 574.8 ms ± 3.5 ms