How to optimise compilation times with Rust

You can read the blog post on Lemmy (which is written in Rust).

You can comment here as well, I will do my best to answer any questions.

1 Like

Another thing you could try to bring down your cargo check time on diesel related code:

Box all queries as soon as possible. This will likely remove quite a bit of work for the type check part of rustc. So instead of for example:

users::table.filter(users::id.eq(42)).or_filter(users::name.eq("John"))

you would write

users::table.into_boxed().filter(users::id.eq(42)).or_filter(users::name.eq("John"))

Now this has not only advantages (otherwise it would be the default), so let me list potential disadvantages here:

  • This requires a few heap allocations for trait objects internally. Normal queries compile down to zero sized structs while boxed queries store something on the heap
  • Each query build step will involve now a few dynamic dispatches, while normal queries should compile down to just concatenating string parts
  • Prepared statement caching needs to build the SQL for the complete query each time the query is executed. For normal static queries this can sometimes be skipped, as those information are already part of the query type.

Now all of this sounds like a large loose of performance. I've done some benchmarks some time back and while those effects where measurable in some cases (like using a in memory sqlite db) most of this should not really matter for queries that are not called repeatably in a hot loop.

1 Like