提交 8a5b3ca8 authored 作者: Andrei Tokar's avatar Andrei Tokar

use weak references in LIRS cache

上级 0494963b
...@@ -46,13 +46,17 @@ public class TestCacheLongKeyLIRS extends TestBase { ...@@ -46,13 +46,17 @@ public class TestCacheLongKeyLIRS extends TestBase {
testRandomOperations(); testRandomOperations();
} }
private static void testRandomSmallCache() { private void testRandomSmallCache() {
Random r = new Random(1); Random r = new Random(1);
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
int j = 0; int j = 0;
StringBuilder buff = new StringBuilder(); StringBuilder buff = new StringBuilder();
CacheLongKeyLIRS<Integer> test = createCache(1 + r.nextInt(10)); int maxSize = 1 + r.nextInt(10);
buff.append("size:").append(maxSize).append('\n');
CacheLongKeyLIRS<Integer> test = createCache(maxSize);
for (; j < 30; j++) { for (; j < 30; j++) {
String lastState = toString(test);
try {
int key = r.nextInt(5); int key = r.nextInt(5);
switch (r.nextInt(3)) { switch (r.nextInt(3)) {
case 0: case 0:
...@@ -69,6 +73,11 @@ public class TestCacheLongKeyLIRS extends TestBase { ...@@ -69,6 +73,11 @@ public class TestCacheLongKeyLIRS extends TestBase {
buff.append("get ").append(key).append('\n'); buff.append("get ").append(key).append('\n');
test.get(key); test.get(key);
} }
verify(test, null);
} catch (Throwable ex) {
println(i + "\n" + buff + "\n" + lastState + "\n" + toString(test));
throw ex;
}
} }
} }
} }
...@@ -164,22 +173,22 @@ public class TestCacheLongKeyLIRS extends TestBase { ...@@ -164,22 +173,22 @@ public class TestCacheLongKeyLIRS extends TestBase {
assertEquals(1, test.getMemory(5)); assertEquals(1, test.getMemory(5));
assertEquals(0, test.getMemory(4)); assertEquals(0, test.getMemory(4));
assertEquals(0, test.getMemory(100)); assertEquals(0, test.getMemory(100));
assertNull(test.peek(4)); assertNotNull(test.peek(4));
assertNull(test.get(4)); assertNotNull(test.get(4));
assertEquals(10, test.get(1).intValue()); assertEquals(10, test.get(1).intValue());
assertEquals(20, test.get(2).intValue()); assertEquals(20, test.get(2).intValue());
assertEquals(30, test.get(3).intValue()); assertEquals(30, test.get(3).intValue());
verify(test, "mem: 4 stack: 3 2 1 cold: 5 non-resident: 4"); verify(test, "mem: 5 stack: 3 2 1 cold: 4 5 non-resident:");
assertEquals(50, test.get(5).intValue()); assertEquals(50, test.get(5).intValue());
verify(test, "mem: 4 stack: 5 3 2 1 cold: 5 non-resident: 4"); verify(test, "mem: 5 stack: 5 3 2 1 cold: 5 4 non-resident:");
assertEquals(50, test.get(5).intValue()); assertEquals(50, test.get(5).intValue());
verify(test, "mem: 4 stack: 5 3 2 cold: 1 non-resident: 4"); verify(test, "mem: 5 stack: 5 3 2 cold: 1 4 non-resident:");
// remove // remove
assertEquals(50, test.remove(5).intValue()); assertEquals(50, test.remove(5).intValue());
assertNull(test.remove(5)); assertNull(test.remove(5));
verify(test, "mem: 3 stack: 3 2 1 cold: non-resident: 4"); verify(test, "mem: 4 stack: 3 2 1 cold: 4 non-resident:");
assertNull(test.remove(4)); assertNotNull(test.remove(4));
verify(test, "mem: 3 stack: 3 2 1 cold: non-resident:"); verify(test, "mem: 3 stack: 3 2 1 cold: non-resident:");
assertNull(test.remove(4)); assertNull(test.remove(4));
verify(test, "mem: 3 stack: 3 2 1 cold: non-resident:"); verify(test, "mem: 3 stack: 3 2 1 cold: non-resident:");
...@@ -195,7 +204,7 @@ public class TestCacheLongKeyLIRS extends TestBase { ...@@ -195,7 +204,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
verify(test, "mem: 3 stack: 4 3 2 cold: non-resident: 1"); verify(test, "mem: 3 stack: 4 3 2 cold: non-resident: 1");
assertEquals(20, test.remove(2).intValue()); assertEquals(20, test.remove(2).intValue());
assertFalse(test.containsKey(1)); assertFalse(test.containsKey(1));
assertNull(test.remove(1)); assertEquals(10, test.remove(1).intValue());
assertFalse(test.containsKey(1)); assertFalse(test.containsKey(1));
verify(test, "mem: 2 stack: 4 3 cold: non-resident:"); verify(test, "mem: 2 stack: 4 3 cold: non-resident:");
test.put(1, 10); test.put(1, 10);
...@@ -226,7 +235,7 @@ public class TestCacheLongKeyLIRS extends TestBase { ...@@ -226,7 +235,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
// 1 was non-resident, so this should make it hot // 1 was non-resident, so this should make it hot
test.put(1, 10); test.put(1, 10);
verify(test, "mem: 4 stack: 1 5 4 3 cold: 2 non-resident: 5"); verify(test, "mem: 4 stack: 1 5 4 3 cold: 2 non-resident: 5");
assertFalse(test.containsValue(50)); assertTrue(test.containsValue(50));
test.remove(2); test.remove(2);
test.remove(3); test.remove(3);
test.remove(4); test.remove(4);
...@@ -332,8 +341,8 @@ public class TestCacheLongKeyLIRS extends TestBase { ...@@ -332,8 +341,8 @@ public class TestCacheLongKeyLIRS extends TestBase {
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
test.put(i, 10 * i); test.put(i, 10 * i);
} }
verify(test, "mem: 4 stack: 19 18 17 16 15 14 13 12 11 10 3 2 1 " + verify(test, "mem: 4 stack: 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 " +
"cold: 19 non-resident: 18 17 16 15 14 13 12 11 10"); "cold: 19 non-resident: 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 0");
} }
private void testLimitMemory() { private void testLimitMemory() {
...@@ -344,13 +353,13 @@ public class TestCacheLongKeyLIRS extends TestBase { ...@@ -344,13 +353,13 @@ public class TestCacheLongKeyLIRS extends TestBase {
verify(test, "mem: 4 stack: 4 3 2 1 cold: 4 non-resident: 0"); verify(test, "mem: 4 stack: 4 3 2 1 cold: 4 non-resident: 0");
assertTrue("" + test.getUsedMemory(), test.getUsedMemory() <= 4); assertTrue("" + test.getUsedMemory(), test.getUsedMemory() <= 4);
test.put(6, 60, 3); test.put(6, 60, 3);
verify(test, "mem: 4 stack: 6 4 3 cold: 6 non-resident: 2 1 4"); verify(test, "mem: 4 stack: 6 4 3 cold: 6 non-resident: 2 1 4 0");
assertTrue("" + test.getUsedMemory(), test.getUsedMemory() <= 4); assertTrue("" + test.getUsedMemory(), test.getUsedMemory() <= 4);
test.put(7, 70, 3); test.put(7, 70, 3);
verify(test, "mem: 4 stack: 7 6 3 cold: 7 non-resident: 6 2 1"); verify(test, "mem: 4 stack: 7 6 4 3 cold: 7 non-resident: 6 2 1 4 0");
assertTrue("" + test.getUsedMemory(), test.getUsedMemory() <= 4); assertTrue("" + test.getUsedMemory(), test.getUsedMemory() <= 4);
test.put(8, 80, 4); test.put(8, 80, 4);
verify(test, "mem: 4 stack: 8 cold: non-resident:"); verify(test, "mem: 4 stack: 8 cold: non-resident: 3 7 6 2 1 4 0");
assertTrue("" + test.getUsedMemory(), test.getUsedMemory() <= 4); assertTrue("" + test.getUsedMemory(), test.getUsedMemory() <= 4);
} }
...@@ -369,7 +378,7 @@ public class TestCacheLongKeyLIRS extends TestBase { ...@@ -369,7 +378,7 @@ public class TestCacheLongKeyLIRS extends TestBase {
test.put(i, i * 10); test.put(i, i * 10);
test.get(i); test.get(i);
if (log) { if (log) {
System.out.println("get " + i + " -> " + test); println("get " + i + " -> " + test);
} }
} }
verify(test, null); verify(test, null);
...@@ -394,14 +403,13 @@ public class TestCacheLongKeyLIRS extends TestBase { ...@@ -394,14 +403,13 @@ public class TestCacheLongKeyLIRS extends TestBase {
} }
verify(test, null); verify(test, null);
} }
// ensure 0..9 are hot, 10..17 are not resident, 18..19 are cold // ensure 0..9 are hot, 10..17 are not resident, 18..19 are cold
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
Integer x = test.get(i); Integer x = test.get(i);
if (i < size / 2 || i == size - 1 || i == size - 2) { if (i < size / 2 || i == size - 1 || i == size - 2) {
assertNotNull("i: " + i, x); assertNotNull("i: " + i, x);
assertEquals(i * 10, x.intValue()); assertEquals(i * 10, x.intValue());
} else {
assertNull(x);
} }
verify(test, null); verify(test, null);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论