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

SustainableCode: Improve performance and reduc...

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();
sb.Append("HelloWorld");

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".

sb.Clear();

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

sbPool.Return(sb);

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

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

https://github.com/BenjaminAbt/dotnet-perf-stringbuilder-pooled
https://github.com/BenjaminAbt/SustainableCode

Benjamin Abt

November 01, 2021
Tweet

More Decks by Benjamin Abt

Other Decks in Programming

Transcript

  1. 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
  2. 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.
  3. 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