Open
Description
CachingSession
is currently suboptimal, especially when using it &'static str
(and I suppose it's the main use case), mostly for two reasons:
Into<Query>
implies a string cloning in case of&str
; however, making the query is not necessary to perform cache lookup, only the string reference is necessary- cache stores raw
PreparedStatement
s, which have to be cloned when retrieved (andPreparedStatement
is quite heavy to clone).
I've implemented an optimization to solve this issue, in which I've made the following modifications:
- introduce
trait QueryString {fn query_string(&self) -> &str;}
and usequery: impl Into<Query> + QueryString
inCachingSession::Execute
(and others) signature; cache lookups are performed usingquery.query_string()
, andquery.into()
is only used when the statement must be prepared - store
Arc<PreparedStatement>
into the cache
I've done some benchmarks (using my old i7 4650U MacBook Air), in which I've measured only one prepared statement retrieval (no serialization and database access) from &'static str
. Here are the results :
query string size | current | optimized |
---|---|---|
20 | 390 ns | 205 ns |
100 | 450 ns | 250 ns |
I let you judge if the optimization could be worth it. The implementation is just a draft and maybe adapted (I can open a PR).