Hello!
I use the Guava/Caffeine CacheLoader class, and have seen massive speed improvements by calling the loadAll()
method instead of calling load()
in a loop. Is there any interest in implementing a Sonar rule that checks to see if load()
is called in a loop? Should there also be a rule to check that every LoadingCache implements loadAll()
?
Why is there an issue?
Massive performance improvement.
Snippet of Noncompliant Code
List<Integer> allIDs = {{ some large list of IDs }};
CacheLoader<Integer, String> cache = new DatabaseOrRESTCache();
String names = allIDs.stream()
.map(id -> cache.load(id))
.collect(Collectors.joining(","));
Snippet of Compliant Code
List<Integer> allIDs = {{ some large list of IDs }};
CacheLoader<Integer, String> cache = new DatabaseOrRESTCache();
Map<Integer, String> idToNameMap = cache.loadAll(allIDs);
String names = allIDs.stream()
.map(id -> idToNameMap.get(id))
.collect(Collectors.joining(","));
External References
https://guava.dev/releases/22.0/api/docs/com/google/common/cache/CacheLoader.html#loadAll-java.lang.Iterable-
Type: performance
Thanks!