Skip to content

Commit

Permalink
Merge pull request #2121 from MarcMil/devfixes
Browse files Browse the repository at this point in the history
Fix recursive array type cache load
  • Loading branch information
StevenArzt authored Nov 7, 2024
2 parents 21d341f + f40c5ce commit 151a0e6
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/main/java/soot/ArrayTypeCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

import heros.solver.Pair;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;

import soot.Singletons.Global;
Expand All @@ -36,7 +36,7 @@
* @author Marc Miltenberger
*/
public class ArrayTypeCache {
private final Map<Pair<Type, Integer>, ArrayType> cache = new ConcurrentHashMap<>();
private final Map<Pair<Type, Integer>, ArrayType> cache = new HashMap<>();

private final Function<Pair<Type, Integer>, ArrayType> mapping = new Function<Pair<Type, Integer>, ArrayType>() {

Expand All @@ -51,7 +51,7 @@ public ArrayType apply(Pair<Type, Integer> t) {
if (ret == null) {
int n = orgDimensions - numDimensions + 1;
if (n != orgDimensions) {
ret = cache.computeIfAbsent(new Pair<>(baseType, n), mapping);
ret = getArrayType(baseType, n);
} else {
ret = new ArrayType(baseType, n);
}
Expand All @@ -76,10 +76,19 @@ public ArrayTypeCache(Global g) {
* @param numDimensions the number of dimensions
* @return the array type
*/
public ArrayType getArrayType(Type baseType, int numDimensions) {
//We are doing this synchronized now to ensure correctness:
//Already creating a new ArrayType adds it to the type numberer, so we must not create
//the same array type twice. Furthermore, the ConcurrentHashMap's computeIfAbsent
//method does not allow the update of other keys in while a value is computed.
public synchronized ArrayType getArrayType(Type baseType, int numDimensions) {
Pair<Type, Integer> pairSearch = new Pair<>(baseType, numDimensions);
ArrayType res = cache.get(pairSearch);
if (res == null) {
res = mapping.apply(pairSearch);
cache.put(pairSearch, res);
}

return cache.computeIfAbsent(pairSearch, mapping);
return res;

}

Expand Down

0 comments on commit 151a0e6

Please sign in to comment.