BinaryWriterPool class final
Object pool for reusing BinaryWriter instances to reduce GC pressure.
This pool maintains a cache of BinaryWriter instances with their internal buffers, allowing efficient reuse without allocating new memory for each write operation.
Features
- Automatic reuse: acquire gets a pooled writer or creates a new one
- Memory bounds: Only reuses writers with
buffers ≤
maxReusableCapacity - Size limits: Maintains max
maxPoolSizepooled instances - Safe: Prevents double-release and handles edge cases
- Instantiable: Create custom pools for specific use cases or use the default global pool.
Usage Pattern
Using the default global pool:
final writer = BinaryWriterPool.acquire();
try {
writer.writeUint32(42);
writer.writeString('Hello');
final bytes = writer.toBytes();
} finally {
BinaryWriterPool.release(writer);
}
Creating a custom pool:
final myPool = BinaryWriterPool(
maxPoolSize: 10,
initialBufferSize: 512,
maxReusableCapacity: 4096,
);
final writer = myPool.acquireInstance();
// ...
myPool.releaseInstance(writer);
Performance Considerations
- Pooling is beneficial for high-frequency write operations
- Overhead is minimal for single-use writers (use regular constructor)
- Large buffers (>64 KiB by default) are discarded to avoid memory waste
See also: BinaryWriter
Constructors
- BinaryWriterPool({int maxPoolSize = 32, int initialBufferSize = 1024, int maxReusableCapacity = 64 * 1024})
- Creates a new, isolated BinaryWriterPool.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- instanceStats → PoolStatistics
-
Returns statistics for this specific pool instance.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
acquireInstance(
[int? initialBufferSize]) → BinaryWriter - Acquires a BinaryWriter from this specific pool instance.
-
clearInstance(
) → void - Clears this specific pool instance.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reconfigure(
{int maxPoolSize = 32, int initialBufferSize = 1024, int maxReusableCapacity = 64 * 1024}) → void - Reconfigures the settings of this specific pool instance.
-
releaseInstance(
BinaryWriter writer) → void - Returns a BinaryWriter to this specific pool instance.
-
toString(
) → String -
A string representation of this object.
inherited
-
withWriterInstance<
T> (T action(BinaryWriter writer), [int? initialBufferSize]) → T -
Acquires a writer from this pool instance, executes the given
action, and automatically releases it.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- global → BinaryWriterPool
-
The default global instance of BinaryWriterPool.
final
- stats → PoolStatistics
-
Returns statistics for the default global pool.
no setter
Static Methods
-
acquire(
[int? initialBufferSize]) → BinaryWriter - Acquires a BinaryWriter from the default global pool.
-
clear(
) → void - Clears the default global pool.
-
configure(
{int maxPoolSize = 32, int initialBufferSize = 1024, int maxReusableCapacity = 64 * 1024}) → void - Configures the default global pool settings.
-
release(
BinaryWriter writer) → void - Returns a BinaryWriter to the default global pool.
-
withWriter<
T> (T action(BinaryWriter writer), [int? initialBufferSize]) → T -
Acquires a writer from the default global pool, executes the given
action, and automatically releases it.