Allow key type to be unsized and prefer str to String as key type #516
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.
Rationale
Currently, moka wraps the key type inside an
Arc
. Coupled with the usage ofToOwned
trait,this creates a two level pointer indirection and waste of some space if we use
String
as the key type, which is commonly used.This PR creates a new trait
ToOwnedArc
which converts the type to be optimally wrapped inside anArc
and then removes the
Sized
requirement on key type.Warning
Although in general,
Arc<String>
seems to be an anti-pattern. But for some workload that already has aString
,it could avoid the potential reallocation when converting that
String
intoArc<str>
.So this PR might bring some negative performance effects.Well, I have made theToOwnedArc
trait less breaking so that users can continue to useString
as they wish.And this PR of course introduces BREAKING changes.
Benchmark
Unfortunately, mokabench does not have benches for string key type. So I currently don't have any benchmark results to back this PR.
Running mokabench with this PR produces similar results without this PR, though.