DefaultObjectPool "devirtualizes" calls to IPooledObjectPolicy #317
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.
Continuation from #314 (comment)
Description
I tried several things to "devirtualize" the calls to
IPooledObjectPolicyOn this I focused on the perf of
Return, as this is a common case.Createis cold, so I didn't investigate it, although the same optimization are applied.The above mentioned tries results in (Default is the state after merging #314), done with benchmarks that use
StringBuilderPooledObjectPolicy.Code for benchmark.
For completeness the results with Original before merging #314:
Discussion
Abstract base class
This variant requires an abstract base class
and to take advantage of, the PoolProvider-classes have to be adapted. This isn't ideal, because there may be classes out in the wild the can't be updated or this takes a lot of effort to update all classes on several places.
Therefore this variant is discontinued, although perf is super.
Expression
Expressions compiled to delegates incur a security check at runtime, that can be clearly seen in the numbers --> discontinued.
IL code gen
Combination of abstract class and IL code gen
As prototype I tried a combination of the approach with abstract base class, and when the PoolProvider does just implement the interface (not the base class), so per fallback IL code is generated.
Here the perf is on par with the two others, so no real win. Because of code complexity this variant is discontinued and not checked in the repo -- I just prototyped it.