-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Collision cache to simulator and benchmarks (closes #126)
Due to requiring JDK9 this cannot be enabled by default. An easy way to run when enabled is using, $ jenv shell 9-ea $ gradle status --stop $ gradle simulator:run --no-daemon For details on this implementation see https://github.com/jamespedwards42/collision
- Loading branch information
Showing
11 changed files
with
196 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ bin | |
build | ||
test-output | ||
jitwatch.out | ||
.java-version | ||
.DS_Store | ||
.classpath | ||
.settings | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
caffeine/src/jmh/java/com/github/benmanes/caffeine/cache/impl/Collision.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2015 Ben Manes. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.benmanes.caffeine.cache.impl; | ||
|
||
import com.fabahaba.collision.cache.CollisionCache; | ||
import com.github.benmanes.caffeine.cache.BasicCache; | ||
|
||
/** | ||
* Requires JDK9. | ||
* | ||
* @author [email protected] (Ben Manes) | ||
*/ | ||
public final class Collision<K, V> implements BasicCache<K, V> { | ||
private final CollisionCache<K, V> cache; | ||
|
||
public Collision(int maximumSize) { | ||
cache = CollisionCache.<V>withCapacity(maximumSize) | ||
.setStrictCapacity(true) | ||
.buildSparse(); | ||
} | ||
|
||
@Override | ||
public V get(K key) { | ||
return cache.getIfPresent(key); | ||
} | ||
|
||
@Override | ||
public void put(K key, V value) { | ||
cache.putReplace(key, value); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
cache.clear(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...ain/java/com/github/benmanes/caffeine/cache/simulator/policy/product/CollisionPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright 2016 Ben Manes. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.github.benmanes.caffeine.cache.simulator.policy.product; | ||
|
||
import static java.util.stream.Collectors.toSet; | ||
|
||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import com.fabahaba.collision.cache.CollisionBuilder; | ||
import com.fabahaba.collision.cache.CollisionCache; | ||
import com.github.benmanes.caffeine.cache.simulator.BasicSettings; | ||
import com.github.benmanes.caffeine.cache.simulator.policy.Policy; | ||
import com.github.benmanes.caffeine.cache.simulator.policy.PolicyStats; | ||
import com.typesafe.config.Config; | ||
|
||
/** | ||
* Collision cache implementation. | ||
* | ||
* @author [email protected] (Ben Manes) | ||
*/ | ||
public final class CollisionPolicy implements Policy { | ||
private final CollisionCache<Object, Object> cache; | ||
private final PolicyStats policyStats; | ||
private final int maximumSize; | ||
|
||
private int trackedSize; | ||
|
||
public CollisionPolicy(CollisionSettings settings, Density density) { | ||
policyStats = new PolicyStats(String.format("product.Collision (%s)", | ||
StringUtils.capitalize(density.name().toLowerCase()))); | ||
maximumSize = settings.maximumSize(); | ||
|
||
CollisionBuilder<Object> builder = CollisionCache | ||
.withCapacity(maximumSize) | ||
.setInitCount(settings.initCount()) | ||
.setBucketSize(settings.bucketSize()) | ||
.setStrictCapacity(settings.strictCapacity()); | ||
|
||
if (density == Density.SPARSE) { | ||
cache = builder.buildSparse(settings.sparseFactor()); | ||
} else if (density == Density.PACKED) { | ||
cache = builder.buildPacked(); | ||
} else { | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
/** Returns all variations of this policy based on the configuration parameters. */ | ||
public static Set<Policy> policies(Config config) { | ||
CollisionSettings settings = new CollisionSettings(config); | ||
return settings.density() | ||
.map(density -> new CollisionPolicy(settings, density)) | ||
.collect(toSet()); | ||
} | ||
|
||
@Override | ||
public void record(long key) { | ||
Object value = cache.getIfPresent(key); | ||
if (value == null) { | ||
if (trackedSize == maximumSize) { | ||
policyStats.recordEviction(); | ||
trackedSize--; | ||
} | ||
cache.putIfAbsent(key, key); | ||
policyStats.recordMiss(); | ||
trackedSize++; | ||
} else { | ||
policyStats.recordHit(); | ||
} | ||
} | ||
|
||
@Override | ||
public PolicyStats stats() { | ||
return policyStats; | ||
} | ||
|
||
enum Density { SPARSE, PACKED } | ||
|
||
static final class CollisionSettings extends BasicSettings { | ||
public CollisionSettings(Config config) { | ||
super(config); | ||
} | ||
public int initCount() { | ||
return config().getInt("collision.init-count"); | ||
} | ||
public int bucketSize() { | ||
return config().getInt("collision.bucket-size"); | ||
} | ||
public double sparseFactor() { | ||
return config().getDouble("collision.sparse-factor"); | ||
} | ||
public boolean strictCapacity() { | ||
return config().getBoolean("collision.strict-capacity"); | ||
} | ||
public Stream<Density> density() { | ||
return config().getStringList("collision.density").stream() | ||
.map(denity -> Density.valueOf(denity.toUpperCase())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters