scylla-rust-driver icon indicating copy to clipboard operation
scylla-rust-driver copied to clipboard

Optimize CachinSession

Open wyfo opened this issue 1 year ago • 2 comments

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 PreparedStatements, which have to be cloned when retrieved (and PreparedStatement 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 use query: impl Into<Query> + QueryString in CachingSession::Execute (and others) signature; cache lookups are performed using query.query_string(), and query.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).

wyfo avatar Jul 18 '22 13:07 wyfo