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