Skip to content

Commit

Permalink
Merge pull request #931 from soot-oss/simplify/LRUCache
Browse files Browse the repository at this point in the history
simplify LRUCache implementation - make use of LinkedHashmap+3rd ctor
  • Loading branch information
stschott authored Apr 29, 2024
2 parents 5d87b16 + d9f4fd5 commit 730cd83
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions sootup.core/src/main/java/sootup/core/cache/LRUCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,21 @@
* specified amount, the lest recently used class will be overwritten.
*/
public class LRUCache implements ClassCache {
private final int cacheSize;
private final Map<ClassType, SootClass> cache = new HashMap<>();
private final LinkedList<ClassType> accessOrder = new LinkedList<>();
private final LinkedHashMap<ClassType, SootClass> cache;

public LRUCache(int cacheSize) {
this.cacheSize = cacheSize;
cache =
new LinkedHashMap<ClassType, SootClass>(cacheSize, 1, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<ClassType, SootClass> eldest) {
return size() > cacheSize;
};
};
}

@Override
public synchronized SootClass getClass(ClassType classType) {
SootClass sootClass = cache.get(classType);
if (sootClass != null) {
accessOrder.remove(classType);
accessOrder.addFirst(classType);
}

return sootClass;
return cache.get(classType);
}

@Nonnull
Expand All @@ -59,12 +57,6 @@ public synchronized Collection<SootClass> getClasses() {

@Override
public void putClass(ClassType classType, SootClass sootClass) {
if (accessOrder.size() >= cacheSize) {
ClassType leastAccessed = accessOrder.removeLast();
cache.remove(leastAccessed);
}

accessOrder.addFirst(classType);
cache.putIfAbsent(classType, sootClass);
}

Expand Down

0 comments on commit 730cd83

Please sign in to comment.