Improve statement cache perf by using rustc-hash
instead of the std
hasher.
#234
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.
This is just a POC PR to show how the implementation could be done.
This only affects PostgreSQL, I couldn't find statement caches for the other drivers.
AFAIK, this change is non-breaking and doesn't have any API repercussions.
FxHashMap
is just a convenience type alias, it usesHashMap
under the hood.I'm not familiar enough with the internals of the crate to know if the current benchmarks take statement cache hashing into account, so it's hard to tell the actual real-use gains. If you think this would be necessary and could provide guidance, I could improve the PR with proper benchmarking.
I have benchmarked the hashing algorithms alone head-to-head, and
rustc-hash
performs 20% to 50% better than the std algorithm on plausible SQL queries. FxHash is particularly faster on smaller inputs.rust-hash
is well-known to be faster, but it doesn't have DoS-safety like the std hasher has. This shouldn't be a problem since usually the app controls which queries are hashed.Why
Improve statement cache hashing performance without changing the API.
Alternatives
Keep using the std hasher.
Future work
The hashing algorithm is a small change with a small impact, but there are other ways to speed up the statement caches.
I noticed that there's a lot of
RwLock
andMutex
going on in the internals of the statement caches implementation. I reckon we could make significant gains using some sort of lock-free/concurrent datastructures. Most async projects are multithreaded (e.g. tokio distributes tasks on many cores if possible), so this should provide significant performance gains. I'd like to explore this in a future PR.