Skip to content

Commit

Permalink
Add scaled versions
Browse files Browse the repository at this point in the history
  • Loading branch information
ohadeytan committed Apr 24, 2021
1 parent 2a00061 commit ba686c4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@

public final class RistrettoSizedWindowTinyLfuPolicy extends SizedWindowTinyLfuPolicy {

public RistrettoSizedWindowTinyLfuPolicy(double percentMain, WindowTinyLfuSettings settings) {
public RistrettoSizedWindowTinyLfuPolicy(double percentMain, SizedWindowTinyLfuSettings settings) {
super(percentMain, settings);
String name = String.format("sketch.sized.RistrettoWindowTinyLfu (%.0f%%)", 100 * (1.0d - percentMain));
String name = String.format("sketch.sized." + (scaled ? "Scaled" : "")
+ "RistrettoWindowTinyLfu (%.0f%%)", 100 * (1.0d - percentMain));
policyStats.setName(name);
}

/** Returns all variations of this policy based on the configuration parameters. */
public static Set<Policy> policies(Config config) {
WindowTinyLfuSettings settings = new WindowTinyLfuSettings(config);
SizedWindowTinyLfuSettings settings = new SizedWindowTinyLfuSettings(config);
return settings.percentMain().stream()
.map(percentMain -> new RistrettoSizedWindowTinyLfuPolicy(percentMain, settings))
.collect(toSet());
Expand All @@ -44,7 +45,7 @@ public static Set<Policy> policies(Config config) {
protected void coreEviction(Node candidate) {
while ((sizeData + candidate.weight - sizeWindow) > (maximumSize - maxWindow)) {
Node victim = getVictim();
if (sketch.frequency(candidate.key) > sketch.frequency(victim.key)) {
if (compare(sketch.frequency(candidate.key), candidate.weight, sketch.frequency(victim.key), victim.weight)) {
evictNode(victim);
} else {
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,19 @@ public class SizedWindowTinyLfuPolicy implements Policy{
protected final long maxWindow;
private final long maxProtected;
protected final long maxMain;
protected final boolean scaled;

protected long sizeWindow;
private long sizeProtected;
protected long sizeData;


public SizedWindowTinyLfuPolicy(double percentMain, WindowTinyLfuSettings settings) {
String name = String.format("sketch.sized.WindowTinyLfu (%.0f%%)", 100 * (1.0d - percentMain));
this.policyStats = new PolicyStats(name);
public SizedWindowTinyLfuPolicy(double percentMain, SizedWindowTinyLfuSettings settings) {
this.sketch = new PeriodicResetCountMin4(settings.config());
this.scaled = settings.scaled();
String name = String.format("sketch.sized." + (scaled ? "Scaled" : "")
+ "WindowTinyLfu (%.0f%%)", 100 * (1.0d - percentMain));
this.policyStats = new PolicyStats(name);

this.maxMain = (long) (settings.maximumSizeLong() * percentMain);
this.maxProtected = (long) (maxMain * settings.percentMainProtected());
Expand All @@ -87,7 +90,7 @@ public SizedWindowTinyLfuPolicy(double percentMain, WindowTinyLfuSettings settin

/** Returns all variations of this policy based on the configuration parameters. */
public static Set<Policy> policies(Config config) {
WindowTinyLfuSettings settings = new WindowTinyLfuSettings(config);
SizedWindowTinyLfuSettings settings = new SizedWindowTinyLfuSettings(config);
return settings.percentMain().stream()
.map(percentMain -> new SizedWindowTinyLfuPolicy(percentMain, settings))
.collect(toSet());
Expand Down Expand Up @@ -198,7 +201,7 @@ private void evict() {

protected void coreEviction(Node candidate) {
Node victim = getVictim();
if (sketch.frequency(candidate.key) > sketch.frequency(victim.key)) {
if (compare(sketch.frequency(candidate.key), candidate.weight, sketch.frequency(victim.key), victim.weight)) {
while ((sizeData + candidate.weight - sizeWindow) > maxMain) {
Node evict = getVictim();
evictNode(evict);
Expand All @@ -209,6 +212,13 @@ protected void coreEviction(Node candidate) {
}
}

protected boolean compare(int candidateFreq, int candidateWeight, int victimFreq, int victimWeight) {
if (scaled) {
return (candidateFreq * victimWeight) > (victimFreq * candidateWeight);
}
return candidateFreq > victimFreq;
}

protected void admit(Node candidate) {
candidate.appendToTail(headProbation);
sizeData += candidate.weight;
Expand Down Expand Up @@ -335,18 +345,12 @@ public String toString() {
}
}

// static enum Version {
// SUM,
// CAFFEINE,
// RISTRETTO,
// }
//
// public static final class SizedWindowTinyLfuSettings extends WindowTinyLfuSettings {
// public SizedWindowTinyLfuSettings(Config config) {
// super(config);
// }
// public Version version() {
// return Version.valueOf(config().getString("sized-window-tiny-lfu.version").toUpperCase());
// }
// }
public static final class SizedWindowTinyLfuSettings extends WindowTinyLfuSettings {
public SizedWindowTinyLfuSettings(Config config) {
super(config);
}
public boolean scaled() {
return config().getBoolean("sized-window-tiny-lfu.scaled");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@

public final class SumSizedWindowTinyLfuPolicy extends SizedWindowTinyLfuPolicy {

public SumSizedWindowTinyLfuPolicy(double percentMain, WindowTinyLfuSettings settings) {
public SumSizedWindowTinyLfuPolicy(double percentMain, SizedWindowTinyLfuSettings settings) {
super(percentMain, settings);
String name = String.format("sketch.sized.SumWindowTinyLfu (%.0f%%)", 100 * (1.0d - percentMain));
String name = String.format("sketch.sized." + (scaled ? "Scaled" : "")
+ "SumWindowTinyLfu (%.0f%%)", 100 * (1.0d - percentMain));
policyStats.setName(name);
}

/** Returns all variations of this policy based on the configuration parameters. */
public static Set<Policy> policies(Config config) {
WindowTinyLfuSettings settings = new WindowTinyLfuSettings(config);
SizedWindowTinyLfuSettings settings = new SizedWindowTinyLfuSettings(config);
return settings.percentMain().stream()
.map(percentMain -> new SumSizedWindowTinyLfuPolicy(percentMain, settings))
.collect(toSet());
Expand All @@ -57,11 +58,11 @@ protected void coreEviction(Node candidate) {
victimsSize += victim.weight;
victimsNum++;
victimsFreq += sketch.frequency(victim.key);
if (victimsFreq > candidateFreq) {
break;
}
//if (victimsFreq > candidateFreq) {
// break;
//}
}
if (victimsFreq > candidateFreq) {
if (!compare(candidateFreq, candidate.weight, victimsFreq, victimsSize)) {
reject(candidate);
} else {
for (int i = 0; i < victimsNum; i++) {
Expand Down
6 changes: 5 additions & 1 deletion simulator/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ caffeine.simulator {
count-min-64 {
eps = 0.0001
confidence = 0.99
}
}

count-min-4 {
# periodic: Resets by periodically halving all counters
Expand All @@ -215,6 +215,10 @@ caffeine.simulator {
}
}
}

sized-window-tiny-lfu {
scaled = false
}

feedback-tiny-lfu {
# The maximum emphasis to give newly inserted entries
Expand Down

0 comments on commit ba686c4

Please sign in to comment.