Table of Contents

Class ObjectPools

Namespace
CryptoHives.Foundation.Memory.Pools
Assembly
CryptoHives.Foundation.Memory.dll

Provides ObjectPools for efficient memory usage.

public static class ObjectPools
Inheritance
ObjectPools
Inherited Members

Methods

GetStringBuilder()

Gets a pooled StringBuilder instance.

public static ObjectOwner<StringBuilder> GetStringBuilder()

Returns

ObjectOwner<StringBuilder>

Remarks

Ensure that the following usage pattern is applied to appropriately dispose the object and return it to the pool.

using var owner = ObjectPools.GetStringBuilder();
StringBuilder sb = owner.PooledObject;
...

RentBufferWriter<T>()

Rents an ArrayPoolBufferWriter<T> with default settings from the shared pool for its element type.

public static ArrayPoolBufferWriter<T> RentBufferWriter<T>()

Returns

ArrayPoolBufferWriter<T>

A writer in its just-constructed state.

Type Parameters

T

The element type the writer accepts.

Remarks

Dispose the writer as usual and it returns itself to the pool. No ObjectOwner<T> wrapper is needed here, unlike GetStringBuilder(), because the writer is disposable in its own right:

using var writer = ObjectPools.RentBufferWriter<byte>();
data.CopyTo(writer.GetSpan(data.Length));
writer.Advance(data.Length);

Consume(writer.GetReadOnlySequence());       // borrowed, valid in this scope

To let the payload leave the scope instead, return writer.LeaseSequence() and do not dispose the writer here — the lease carries it and releases it later. For anything other than the default settings, declare an ArrayPoolBufferWriterProvider<T> once and rent from it. It draws from this same pool, so configuring a use case costs no extra instances.

Do not use the writer after disposing it. A returned instance does not throw; it silently becomes whatever the next renter is doing.