Skip to content

Commit

Permalink
Cache map type creation
Browse files Browse the repository at this point in the history
  • Loading branch information
heshanpadmasiri committed Feb 8, 2025
1 parent 29a15d3 commit 9b9388a
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static TupleType createTupleType(String name, Module pkg,
* @return the new map type
*/
public static MapType createMapType(Type constraint) {
return logAndReturn(new BMapType(constraint));
return logAndReturn(MapTypeCache.get(constraint));
}

/**
Expand Down Expand Up @@ -578,4 +578,19 @@ public record TypeIdentifier(String typeName, Module pkg) {
assert pkg != null;
}
}

private static final class MapTypeCache {

private static final Map<Type, MapType> cache = new ConcurrentHashMap<>();

static MapType get(Type constraint) {
MapType cached = cache.get(constraint);
if (cached != null) {
return cached;
}
MapType type = new BMapType(constraint);
cache.put(constraint, type);
return type;
}
}
}

0 comments on commit 9b9388a

Please sign in to comment.