Phoenix.SocketClient.BinaryPool (phoenix_socket_client v0.8.1)
View SourceHigh-performance binary pooling for commonly used JSON patterns.
This module implements a binary pooling mechanism to reduce memory allocation and garbage collection pressure for frequently used JSON patterns in Phoenix Channel messages. It provides:
- Binary pooling for common message templates
- Memory-efficient storage of repeated patterns
- Fast lookup and retrieval of pooled binaries
- Automatic cleanup of unused patterns
Usage
The pool is automatically started by the socket supervisor and can be used directly in message encoding/decoding operations.
Performance Benefits
- Reduces memory allocation for repeated patterns
- Lowers GC pressure from frequent binary creation
- Improves encoding/decoding performance for common messages
- Provides consistent memory usage patterns
Summary
Functions
Returns a specification to start this module under a supervisor.
Creates common Phoenix Channel message patterns for warm-up.
Gets a pooled binary without storing if not found.
Gets a pooled binary if available, otherwise stores the provided binary.
Starts the binary pool server.
Gets pool statistics for monitoring.
Pre-populates the pool with common message patterns.
Types
@type opts() :: [ pool_size: non_neg_integer(), cleanup_interval: non_neg_integer(), max_age: non_neg_integer(), registry_name: atom() ]
Pool configuration options
@type t() :: %Phoenix.SocketClient.BinaryPool{ cleanup_interval: non_neg_integer(), max_age: non_neg_integer(), patterns: %{ required(binary()) => %{ count: non_neg_integer(), last_used: integer(), size: non_neg_integer() } }, pool_size: non_neg_integer(), registry_name: atom() | nil, total_count: non_neg_integer(), total_memory: non_neg_integer() }
Pool state structure
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec common_patterns() :: [binary()]
Creates common Phoenix Channel message patterns for warm-up.
Returns a list of JSON binaries for common message types.
Gets a pooled binary without storing if not found.
Returns {:ok, binary} if found, :error otherwise.
Gets a pooled binary if available, otherwise stores the provided binary.
Returns the pooled binary reference.
@spec start_link(opts()) :: GenServer.on_start()
Starts the binary pool server.
Options
:pool_size- Maximum number of patterns to pool (default: 1000):cleanup_interval- Cleanup interval in milliseconds (default: 30000):max_age- Maximum age for patterns in milliseconds (default: 300000):registry_name- Registry name for process registration
Gets pool statistics for monitoring.
Pre-populates the pool with common message patterns.
Useful for warming up the pool with known patterns.