New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Introduce IResettable to streamline object pool usage #44901
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
cc: @Tratcher |
I also met similar challenge to use instances returned from the object pool, cos they contain old property values and references to other objects, which may lead to problems, if not intialized. So far my solution is an extenstion method which cleans(resets).
For performance consideration: Anyway, I adopted the Reset() method, but the ObjectPoolPolicy model is not favorable for my case. |
Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:
|
Could the default policy call @JamesNK asks if Reset() should return a boolean to indicate success? Like TryReset? Could be useful for things like CancellationTokenSource. Another option is to keep the ResettablePooledObjectPolicy type, and add a public |
ah, this "default policy call I also suggest |
Having the default policy call Reset would require adding a type test in the existing paths, which would slow things down a teeny bit. Having the Reset function return a boolean is a reasonable idea, although it seems fairly niche. Nevertheless the semantics would be clear. If Reset returns false, then the object is dropped on the floor and not added to the pool. |
Is the current proposal: public interface IResettable
{
bool Reset();
} And a type check in PooledObjectPolicy.Return? |
Yes, I think so. |
API Review Notes:
API Approved! namespace Microsoft.Extensions.ObjectPool;
public interface IResettable
{
bool TryReset();
} Edit: The proposal was intended to be "TryReset" rather than "Reset" since IResettable returns a bool. |
Reference dotnet#44901.
@halter73 that should have been TryReset. |
Yeah. We should have discussed that. I figured we were trying to match @Tratcher can you send an email? I don't think it will be controversial, but it's good for visibility. |
We did discuss it, and even agree to it, it just didn't make it into the notes. |
I had to recheck the meeting video but @geeknoid did mention that "TryReset" was the new boolean-returning API name while I was distracted trying to copy the "current proposal. I never wrote it out or confirmed that we were all satisfied the name, but that's on me. I'll update the approval and make a note of it in the wrap up email. |
Reference #44901. Co-authored-by: Martin Taillefer <mataille@microsoft.com>
I'm just going to throw this out there. I do agree basic usage of the ObjectPool requires too much ceremony. But I don't think IResettable really helps with that, it is also a lot of ceremony. I had this proposal a while back to introduce a simple helper extension which you can pass a delegate to for doing reset (if needed): #36586 I think it accomplishes the goal with less 🤷 [It got viciously tossed because this public API is not for public usage? Dub-tee-eff? I'm not bitter about that at all 🤣] |
After experimenting similar resetting mechanism in my project, i found it a big performance disaster to generally reset every instance, especially if a Type owns too many properties, and if most of its properties r to be overwritten later. Now i've removed all experimental resetting implementations and adopted a more performance-friendly solution: The only problem here is how to manage different reuse-cases, better with code, as to make it more readable and managable.
I made a more complete demo here (we need a DefaultObjectPoolPolicy4ReuseCases<T, I> class) :
Summary (in contrast with IResettable solution) :
What do u guys think? |
Background and motivation
We use a lot of object pools in our environment and its clumsy to have to implement dedicated policy types when we generally want a single behavior applied to a large variety of object types. We adopted a solution of having all these objects declare themselves as IResettable and then a single policy suffices to deal with these objects
API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: