Skip to content

Commit

Permalink
Optimize ehcache's usage in the simulator
Browse files Browse the repository at this point in the history
This change results in the same hit rate and has a 37% faster runtime.
For the DS1 trace this reduces from 91 minutes to 67 minutes, which is
still abysmal compared 15-30 seconds of all other caches. Unfortunately
this issue has been known and rejected since 2015. A simple change is
to hash the key to improve the distribution, resulting in a 1.4 minute
runtime, but it reduces the hit rate in for their sampled lru policy.
  • Loading branch information
ben-manes committed Sep 17, 2022
1 parent c5f6c53 commit dc0fb48
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
8 changes: 4 additions & 4 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,17 @@ ext {
jmh: '1.35',
joor: '0.9.14',
jsr330: '1',
nullaway: '0.10.0',
nullaway: '0.10.1',
ohc: '0.6.1',
osgiComponentAnnotations: '1.5.0',
picocli: '4.6.3',
slf4j: '2.0.0',
slf4j: '2.0.1',
tcache: '2.0.1',
stream: '2.9.8',
univocityParsers: '2.9.1',
ycsb: '0.17.0',
xz: '1.9',
zstd: '1.5.2-3',
zstd: '1.5.2-4',
]
testVersions = [
awaitility: '4.2.0',
Expand All @@ -89,7 +89,7 @@ ext {
bnd: '6.3.1',
checkstyle: '10.3.3',
coveralls: '2.12.0',
dependencyCheck: '7.1.2',
dependencyCheck: '7.2.0',
errorprone: '2.0.2',
findsecbugs: '1.12.0',
jacoco: '0.8.7',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ public Ehcache3Policy(Config config) {

@Override
public void record(long key) {
Object value = cache.putIfAbsent(key, key);
var value = cache.get(key);
if (value == null) {
size++;
cache.put(key, Boolean.TRUE);
policyStats.recordMiss();
size++;
if (size > maximumSize) {
policyStats.recordEviction();
size--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
*/
@PolicySpec(name = "product.ExpiringMap")
public final class ExpiringMapPolicy implements KeyOnlyPolicy {
private final ExpiringMap<Object, Object> cache;
private final ExpiringMap<Long, Boolean> cache;
private final PolicyStats policyStats;

public ExpiringMapPolicy(ExpiringMapSettings settings, Eviction policy) {
Expand All @@ -60,12 +60,12 @@ public static Set<Policy> policies(Config config) {

@Override
public void record(long key) {
Object value = cache.get(key);
var value = cache.get(key);
if (value == null) {
if (cache.size() == cache.getMaxSize()) {
policyStats.recordEviction();
}
cache.put(key, key);
cache.put(key, Boolean.TRUE);
policyStats.recordMiss();
} else {
policyStats.recordHit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
*/
@PolicySpec(name = "product.Hazelcast")
public final class HazelcastPolicy implements KeyOnlyPolicy {
private final NearCache<Long, Long> cache;
private final NearCache<Long, Boolean> cache;
private final PolicyStats policyStats;
private final int maximumSize;

Expand Down Expand Up @@ -85,7 +85,7 @@ public void record(long key) {
if (cache.size() == maximumSize) {
policyStats.recordEviction();
}
cache.put(key, /* keyData */ null, key, /* valueDate */ null);
cache.put(key, /* keyData */ null, Boolean.TRUE, /* valueDate */ null);
policyStats.recordMiss();
} else {
policyStats.recordHit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
*/
@PolicySpec(name = "product.TCache")
public final class TCachePolicy implements KeyOnlyPolicy {
private final Cache<Object, Object> cache;
private final Cache<Long, Boolean> cache;
private final PolicyStats policyStats;

public TCachePolicy(TCacheSettings settings, Eviction policy) {
policyStats = new PolicyStats(name() + " (%s)", policy);
cache = TCacheFactory.standardFactory().builder()
cache = TCacheFactory.standardFactory().<Long, Boolean>builder()
.setMaxElements(Ints.checkedCast(settings.maximumSize()))
.setEvictionPolicy(policy.type)
.setStatistics(true)
Expand All @@ -64,7 +64,7 @@ public void record(long key) {
Object value = cache.get(key);
if (value == null) {
policyStats.recordMiss();
cache.put(key, key);
cache.put(key, Boolean.TRUE);
} else {
policyStats.recordHit();
}
Expand Down

0 comments on commit dc0fb48

Please sign in to comment.