Class MessagePool<T>
A thread-safe pool of IMessage instances of type T
that reduces garbage allocation by recycling message objects.
Implements
Inherited Members
Namespace: Unity.SimulationPro.Foundation
Assembly: Unity.SimulationPro.Foundation.dll
Syntax
public class MessagePool<T> : IMessagePool where T : IMessage, new()
Type Parameters
| Name | Description |
|---|---|
| T | The IMessage type managed by this pool. Must have a public parameterless constructor. |
Remarks
Pass an instance to SetMessagePool(IMessagePool) to enable pooling on a publisher. Call GetOrCreateMessage() to obtain a message instance, fill it in, then publish it. After delivery the subscriber or publisher can return it to the pool via AddMessage(T).
Constructors
MessagePool(int)
Creates a new MessagePool<T> with the specified maximum capacity.
Declaration
public MessagePool(int maxSize = 10)
Parameters
| Type | Name | Description |
|---|---|---|
| int | maxSize | The maximum number of messages retained in the pool. Excess recycled messages are discarded. |
Methods
AddMessage(T)
Returns a typed message to the pool for reuse.
Declaration
public void AddMessage(T messageToRecycle)
Parameters
| Type | Name | Description |
|---|---|---|
| T | messageToRecycle | The message instance to recycle. |
GetOrCreateMessage()
Get a message from the pool, or create one if necessary.
Declaration
public T GetOrCreateMessage()
Returns
| Type | Description |
|---|---|
| T | A new or recycled message of type T, which may contain garbage data. Be sure to fill it in. |