feat(recycling): add customizable recycling policies#33
Merged
Conversation
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
9d94465 to
8fde8f6
Compare
Signed-off-by: Eliza Weisman <[email protected]>
8fde8f6 to
0071b81
Compare
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
Signed-off-by: Eliza Weisman <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation
Currently, all queues and channels in
thingbufrequire that items inthe queue/channel implement
Default, becauseDefaultis used to fillslots when they are initially allocated. Furthermore, when slots are
checked out for writing to, they are not cleared prior to being written
to --- the user code is responsible for clearing them if needed.
The
StringBuftype currently implements special behaviorspecifically for
Strings, where theStringis cleared in placeprior to writing to, but this only works for
Strings.StringBufalsoprovides an API for limiting the maximum capacity of "empty" strings, so
that they can be shrunk down to that capacity when returning them to the
pool. This allows introducing an upper bound on the capacity allocated
by unused strings. However, again, this only works with
Strings and isonly provided by the
StringBuftype.This isn't ideal --- users shouldn't have to be responsible for
clearing non-
Stringtypes when reusing allocations.Solution
This branch introduces a new
Recycle<T>trait that defines a policyfor how
T-typed pooled objects should be reused.Recycle<T>definestwo methods:
fn new_element(&self) -> Tcreates a new elementfn recycle(&self, element: &mut T)clears a pooled element for reuseThis allows a
Recycleimplementation to define the lifecycle of apooled item.
In addition, we define a couple of pre-made
Recycleimplementations:DefaultRecycle, which implementsRecyclefor all typesTwhereT: Default + Clone. This is used by allthingbuftypes by default.It creates new elements using
Default::default, and recycles themusing
element.clone_from(T::default()).Clone::clone_fromis not guaranteed to re-use existing capacity,but it's overridden by most array-based collections (such as the ones
in the standard library) to do so --- it should be equivalent to
.clear()when cloning from an empty collection. However, this policywill still work with types that don't have a clear-in-place
function.
WithCapacityimplementsRecycleonly for types that definewith_capacity,shrink_to, andclearmethods, like allarray-based collections in the Rust standard library. Unlike
DefaultRecycle, it is guaranteed to clear elements in place andretain any previously allocated capacity.
It can also be configured to add both upper and lower bounds on
capacity. When there is a lower bound, new elements are allocated with
that value as their initial capacity, rather than being allocated with
0 capacity. When an upper bound is set, it will call
shrink_topriorto clearing elements, to limit the total allocated capacity retained
by the pool.
WithCapacitycurrently implementsRecyclefor allallocandstdtypes that define the requisite methods:Vec,String,VecDeque, andBinaryHeapwhen theallocfeature is enabled, andHashMapandHashSetas well, when thestdfeature is enabled.Finally, I've modified the existing queue and channel types to allow
configuring them to use a
Recycleimplementation. TheStringBuftypeis removed, as it's now obviated by the new APIs.
Future Work
We may wish to factor out the
recyclingmodule into its own crate, sothat it can be used in other libraries.
Closes #30