Improve memory usage of "replicas by token" map in TokenMap


The easiest way to explain this is on an example: NetworkTopologyReplicationStrategyTest.should_compute_for_simple_layout_with_multiple_nodes_per_rack

The resulting map contains repeated value sets:

However, that information is not reused internally: we use an ImmutableSetMultimap that creates a new immutable set for each entry. When a cluster uses vnodes this is probably a huge waste of space.

We could fix this by dropping the multimap, and building a Map<Token, Set<Node>> manually instead. In NetworkTopologyReplicationStrategy, we can keep building replicas as a LinkedHashSet in the loop, but convert it to a unique immutable copy before we put it in the main map:

Side note: we can also declare replicas out of the loop, and clear it at the beginning of each iteration, instead of recreating a new instance every time.



Pull Requests



Olivier Michallat


Olivier Michallat

Fix versions