Proposed change
Optimize the dynamic metadata generation path within setDynamicStreamMetadata and setDynamicConsumerMetadata. The implementation introduces:
Package-level string globals for JSApiLevel and VERSION to eliminate the overhead of strconv.Itoa calls and heap allocations during each metadata generation.
Map capacity pre-allocation using make(map[string]string, len(cfg.Metadata)+2) to avoid map evacuations and bucket rehashing as the versioning keys are added.
Use case
In large-scale NATS deployments, monitoring tools and sidecars frequently poll the JetStream API for stream and consumer information. Currently, each request triggers redundant allocations and map resizing. This proposal reduces unnecessary heap churn and GC pressure, particularly in environments with thousands of assets or high-frequency polling.
Contribution
Yes, I have already implemented these changes and I am opening a PR for this issue.
Proposed change
Optimize the dynamic metadata generation path within
setDynamicStreamMetadataandsetDynamicConsumerMetadata. The implementation introduces:Package-level string globals for
JSApiLevelandVERSIONto eliminate the overhead ofstrconv.Itoacalls and heap allocations during each metadata generation.Map capacity pre-allocation using
make(map[string]string, len(cfg.Metadata)+2)to avoid map evacuations and bucket rehashing as the versioning keys are added.Use case
In large-scale NATS deployments, monitoring tools and sidecars frequently poll the JetStream API for stream and consumer information. Currently, each request triggers redundant allocations and map resizing. This proposal reduces unnecessary heap churn and GC pressure, particularly in environments with thousands of assets or high-frequency polling.
Contribution
Yes, I have already implemented these changes and I am opening a PR for this issue.