Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SustainableCode: Improve performance and reduce allocations with object pooling in .NET

7437b838338581b08ca72340b05475b9?s=47 Benjamin Abt
November 01, 2021

SustainableCode: Improve performance and reduce allocations with object pooling in .NET

In #dotnet, the #objectpool is a mechanism to increase #performance by reusing instances instead of constantly recreating them.
This lowers the CPU load and reduces #allocations to an absolute minimum.

The .NET implementation is done as follows:

First, an #ObjectPool of the respective type is created. The more this pool is used instead of creating objects independently, the higher the positive effect.

ObjectPool sbPool = ObjectPool.Create();

Therefore, it makes sense that this pool is used accordingly by many application parts instead of creating several object pools here as well. However, the latter can make sense in a modular software architecture in order to avoid dependencies.

After the pool has been created, an instance can be requested, which can then be used as usual, like a manually created instance.

StringBuilder sb = sbPool.Get();

Finally, the instance must be reset. So the instance must look like as if it is newly created.
In my #StringBuilder example, resetting is very easy via "Clear".


After resetting the instance, it is returned to the pool, so another part of the code can reuse this instance.


The pool itself manages the entire instance handling, so that the pool always has enough instances available.

#sustainablecode #sustainable #code #development #sustainability #softwarearchitecture



Benjamin Abt

November 01, 2021

More Decks by Benjamin Abt

Other Decks in Programming


  1. None
  2. The object pool is a pattern to re-use objects instead

    to create them all the time.
  3. In .NET the object pattern is built-in via ObjectPool<T> NuGet:

    Microsoft.Extensions.ObjectPool Code Flow: ▪ Create Pool ▪ Request instance from pool ▪ Use instance ▪ Reset instance ▪ Return instance to pool
  4. https://github.com/BenjaminAbt/SustainableCode No Pool vs. Pool Very little code overhead with

    a very large effect String Builder Sample
  5. Efficiency Results https://github.com/BenjaminAbt/dotnet-perf-stringbuilder-pooled The code without pool needs 1.8x more

    power and time and consumes more allocations in this very simple scenario! The standard string operations as a comparison require much more power and generate more allocations than the string builder.
  6. Conclusion / Advise Object Pooling is a real performance booster,

    makes applications more efficient and creates less allocations and thus requires less memory More power means higher performance and/or lower operating costs at the same time, since smaller CPU and memory instances can be used to reach the same performance. For string processing, the StringBuilder is worthwhile in 99% of cases. Some .NET string operations are already using a StringBuilder implementation under the hood (internal type ValueStringBuilder). However, manual string builder implementations can reuse instances through Object Pooling and thus be even more powerful. https://github.com/BenjaminAbt/SustainableCode Advise
  7. None