Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify LRUCache implementation - make use of LinkedHashmap+3rd ctor #931

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading