Skip to content

[Proposal] Optimize dynamic metadata map and string allocations #8026

@Emin-ACIKGOZ

Description

@Emin-ACIKGOZ

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalEnhancement idea or proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions