Conversation
Signed-off-by: Ryan Leckey <leckey.ryan@gmail.com>
Signed-off-by: Ryan Leckey <leckey.ryan@gmail.com>
Signed-off-by: Ryan Leckey <leckey.ryan@gmail.com>
…into its own file Signed-off-by: Ryan Leckey <leckey.ryan@gmail.com>
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
| closed | ||
| } | ||
|
|
||
| fn ensure_capacity_lfu(&mut self, count: usize) -> Vec<Close> { |
There was a problem hiding this comment.
This looks to be O(n * log * n) runtime and expected(n) memory and runs on each server check-in (once per transaction). This may not work if the number of prepared statements is high, which is common. Why not use a real lfu data structure, e.g. https://docs.rs/lfu/latest/lfu/index.html (don't know if the crate is maintained, just googled it).
Meanwhile LRU is O(1)!
There was a problem hiding this comment.
I'd suggest checking this library https://docs.rs/cachekit/0.8.0/cachekit/index.html - because it's a ton of caches and data structures. And it could be even easier if some of the library traits would fit our usecases.
There was a problem hiding this comment.
Thanks for mentioned this one. That one didn't come up when I searched. I still think a purpose built Cache type is best because we don't need most of what a LRU/LFU crate offers, we explicitly do not want automatic eviction and most libraries are built around providing it.
As far as I can tell, automatic eviction can't be turned off in cachekit. Capacity is allocated up-front so there isn't room for spill over. We can't say "no" when told about a statement coming in.
LfuCache is thread-safe internally when we don't need that for the per connection type. It internally wraps things in Arcs and requires &K not &Q where Q: Borrow<K> which makes working with string keys complicated.
Closes #1530
Add
prepared_statements_evictionto[general]takinglru(default) orlfu(also available throughPGDOG_PREPARED_STATEMENTS_EVICTIONandSET ...)Add use tracking to
LocalStatement. The existingensure_capacitymethod now branches based on eviction policy. The data structure is stillLruCache::unbounded().Make a LocalCache wrapper to extract some of the code from prepared_statements.rs into a separate file.
Add several tests that assert behavior of both LRU and LFU policies.