Skip to content

perf: avoid allocating hashmapStats when stats are disabled - #437

Closed
AdamMagued wants to merge 1 commit into
allegro:mainfrom
AdamMagued:perf-omit-hashmap-stats-when-disabled
Closed

AdamMagued wants to merge 1 commit into
allegro:mainfrom
AdamMagued:perf-omit-hashmap-stats-when-disabled

Conversation

@AdamMagued

Copy link
Copy Markdown

When StatsEnabled is false (the default configuration), hashmapStats was still being allocated for each cache shard in initNewShard and reset. In configurations with many shards and cache instances, this incurred unnecessary heap allocation and bucket memory overhead at startup.

This change:

  • Allocates hashmapStats in initNewShard and reset only when config.StatsEnabled is true.
  • Leaves hashmapStats as nil when stats are disabled.
  • Guards hashmapStats accesses in hit, hitWithoutLock, del, removeOldestEntry, and getKeyMetadata to safely handle nil maps without panics.
  • Adds regression tests in bigcache_test.go verifying hashmapStats is unallocated when StatsEnabled: false and allocated when StatsEnabled: true.

Fixes #422

When StatsEnabled is false (the default configuration), hashmapStats was still allocated for every shard via make(map[uint64]uint32, ...). In setups with many shards and instances, this incurred unnecessary heap allocation and bucket overhead at startup.

Only allocate hashmapStats during initNewShard and reset when StatsEnabled is true, and safely guard hashmapStats operations.

Fixes allegro#422
@AdamMagued

Copy link
Copy Markdown
Author

Closing in favor of earlier community PR #423 to keep the review queue clean.

@AdamMagued AdamMagued closed this Oct 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hashmapStats allocated unconditionally regardless of StatsEnabled

1 participant