Credence.Rule.NoMapUpdateThenFetch
(credence v0.2.0)
Copy Markdown
Performance rule: Detects calling Map.update/4 (or Map.update!/3) on a
map variable and then immediately reading the same key back with
Map.fetch!/2 or Map.get/2.
Map.update/4 traverses the map to apply the new value. Following it with
Map.fetch!/2 or Map.get/2 on the same variable performs a second
independent traversal. Calculate the new value first, then use Map.put/3
so both the value and the updated map are available without a second lookup.
Bad
map = Map.update(map, key, 1, &(&1 + 1))
val = Map.fetch!(map, key)Good
count = Map.get(map, key, 0) + 1
map = Map.put(map, key, count)
# `count` is already available — no second lookup needed