[Discussion] Cache instances of MqttClientPublishResult #1325
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.
Continuing the profiling session from #1323 and #1324 this PR caches instances of
MqttClientPublishResult
, so that these 100.000 allocations are gone.Note: in order to make this work, the client user code needs to be adapted:
(Same test program for profiling as in the linked PR on top)
The same technique for caching -- or other maybe better caches -- could be expanded.
For "internal" types (they are all public) it shouldn't have any user visible effect. I tried it for MqttPublishPacket, but didn't know when the instances aren't needed anymore, so wasn't able to return them to the cache. Thus this comit got reverted.
Caching / pooling is quite hard to get right, and in essence the GC also can be seen as pool where we rent objects from. Or put in another view: with pooling we create a custom memory allocator.
So it's always a trade-off when we rent objects from our own pool -- especially as we have to return them to our pool to take advantage of pooling, which in case of "GC-pool" the GC does this on our behalf by scanning the objects-roots.
I'm not sure if this change is good or not, so I badged the title with "discussion" and made the PR a draft one, so we can see where it goes.