提交 2505065e authored 作者: Thomas Mueller's avatar Thomas Mueller

Prepare for improved memory housekeeping

上级 d089b55f
...@@ -152,7 +152,11 @@ public class Page { ...@@ -152,7 +152,11 @@ public class Page {
p.counts = counts; p.counts = counts;
p.totalCount = totalCount; p.totalCount = totalCount;
p.sharedFlags = sharedFlags; p.sharedFlags = sharedFlags;
p.memory = memory == 0 ? p.calculateMemory() : memory; if (memory == 0) {
p.recalculateMemory();
} else {
p.addMemory(memory);
}
MVStore store = map.store; MVStore store = map.store;
if (store != null) { if (store != null) {
store.registerUnsavedPage(); store.registerUnsavedPage();
...@@ -286,7 +290,7 @@ public class Page { ...@@ -286,7 +290,7 @@ public class Page {
keyCount, keys, values, keyCount, keys, values,
childCount, children, childrenPages, counts, totalCount, childCount, children, childrenPages, counts, totalCount,
SHARED_KEYS | SHARED_VALUES | SHARED_CHILDREN | SHARED_COUNTS, SHARED_KEYS | SHARED_VALUES | SHARED_CHILDREN | SHARED_COUNTS,
memory); getMemory());
// mark the old as deleted // mark the old as deleted
removePage(); removePage();
newPage.cachedCompare = cachedCompare; newPage.cachedCompare = cachedCompare;
...@@ -375,8 +379,8 @@ public class Page { ...@@ -375,8 +379,8 @@ public class Page {
b, bKeys, bValues, b, bKeys, bValues,
0, null, null, null, 0, null, null, null,
bKeys.length, 0, 0); bKeys.length, 0, 0);
memory = calculateMemory(); recalculateMemory();
newPage.memory = newPage.calculateMemory(); newPage.recalculateMemory();
return newPage; return newPage;
} }
...@@ -423,8 +427,8 @@ public class Page { ...@@ -423,8 +427,8 @@ public class Page {
b - 1, bKeys, null, b - 1, bKeys, null,
b, bChildren, bChildrenPages, bCounts, b, bChildren, bChildrenPages, bCounts,
t, 0, 0); t, 0, 0);
memory = calculateMemory(); recalculateMemory();
newPage.memory = newPage.calculateMemory(); newPage.recalculateMemory();
return newPage; return newPage;
} }
...@@ -521,10 +525,11 @@ public class Page { ...@@ -521,10 +525,11 @@ public class Page {
} }
Object old = keys[index]; Object old = keys[index];
DataType keyType = map.getKeyType(); DataType keyType = map.getKeyType();
int mem = keyType.getMemory(key);
if (old != null) { if (old != null) {
memory -= keyType.getMemory(old); mem -= keyType.getMemory(old);
} }
memory += keyType.getMemory(key); addMemory(mem);
keys[index] = key; keys[index] = key;
} }
...@@ -542,8 +547,8 @@ public class Page { ...@@ -542,8 +547,8 @@ public class Page {
sharedFlags &= ~SHARED_VALUES; sharedFlags &= ~SHARED_VALUES;
} }
DataType valueType = map.getValueType(); DataType valueType = map.getValueType();
memory -= valueType.getMemory(old); addMemory(valueType.getMemory(value) -
memory += valueType.getMemory(value); valueType.getMemory(old));
values[index] = value; values[index] = value;
return old; return old;
} }
...@@ -598,8 +603,8 @@ public class Page { ...@@ -598,8 +603,8 @@ public class Page {
keyCount++; keyCount++;
sharedFlags &= ~(SHARED_KEYS | SHARED_VALUES); sharedFlags &= ~(SHARED_KEYS | SHARED_VALUES);
totalCount++; totalCount++;
memory += map.getKeyType().getMemory(key); addMemory(map.getKeyType().getMemory(key) +
memory += map.getValueType().getMemory(value); map.getValueType().getMemory(value));
} }
/** /**
...@@ -637,8 +642,8 @@ public class Page { ...@@ -637,8 +642,8 @@ public class Page {
sharedFlags &= ~(SHARED_KEYS | SHARED_CHILDREN | SHARED_COUNTS); sharedFlags &= ~(SHARED_KEYS | SHARED_CHILDREN | SHARED_COUNTS);
totalCount += childPage.totalCount; totalCount += childPage.totalCount;
memory += map.getKeyType().getMemory(key); addMemory(map.getKeyType().getMemory(key) +
memory += DataUtils.PAGE_MEMORY_CHILD; DataUtils.PAGE_MEMORY_CHILD);
} }
/** /**
...@@ -649,7 +654,7 @@ public class Page { ...@@ -649,7 +654,7 @@ public class Page {
public void remove(int index) { public void remove(int index) {
int keyIndex = index >= keyCount ? index - 1 : index; int keyIndex = index >= keyCount ? index - 1 : index;
Object old = keys[keyIndex]; Object old = keys[keyIndex];
memory -= map.getKeyType().getMemory(old); addMemory(-map.getKeyType().getMemory(old));
if ((sharedFlags & SHARED_KEYS) == 0 && keys.length > keyCount - 4) { if ((sharedFlags & SHARED_KEYS) == 0 && keys.length > keyCount - 4) {
if (keyIndex < keyCount - 1) { if (keyIndex < keyCount - 1) {
System.arraycopy(keys, keyIndex + 1, keys, keyIndex, keyCount - keyIndex - 1); System.arraycopy(keys, keyIndex + 1, keys, keyIndex, keyCount - keyIndex - 1);
...@@ -664,7 +669,7 @@ public class Page { ...@@ -664,7 +669,7 @@ public class Page {
if (values != null) { if (values != null) {
old = values[index]; old = values[index];
memory -= map.getValueType().getMemory(old); addMemory(-map.getValueType().getMemory(old));
if ((sharedFlags & SHARED_VALUES) == 0 && values.length > keyCount - 4) { if ((sharedFlags & SHARED_VALUES) == 0 && values.length > keyCount - 4) {
if (index < keyCount - 1) { if (index < keyCount - 1) {
System.arraycopy(values, index + 1, values, index, keyCount - index - 1); System.arraycopy(values, index + 1, values, index, keyCount - index - 1);
...@@ -680,7 +685,7 @@ public class Page { ...@@ -680,7 +685,7 @@ public class Page {
} }
keyCount--; keyCount--;
if (children != null) { if (children != null) {
memory -= DataUtils.PAGE_MEMORY_CHILD; addMemory(-DataUtils.PAGE_MEMORY_CHILD);
long countOffset = counts[index]; long countOffset = counts[index];
long[] newChildren = new long[childCount - 1]; long[] newChildren = new long[childCount - 1];
...@@ -782,7 +787,7 @@ public class Page { ...@@ -782,7 +787,7 @@ public class Page {
} }
totalCount = len; totalCount = len;
} }
memory = calculateMemory(); recalculateMemory();
} }
/** /**
...@@ -929,7 +934,9 @@ public class Page { ...@@ -929,7 +934,9 @@ public class Page {
public int getMemory() { public int getMemory() {
if (MVStore.ASSERT) { if (MVStore.ASSERT) {
if (memory != calculateMemory()) { int mem = memory;
recalculateMemory();
if (mem != memory) {
throw DataUtils.newIllegalStateException( throw DataUtils.newIllegalStateException(
DataUtils.ERROR_INTERNAL, "Memory calculation error"); DataUtils.ERROR_INTERNAL, "Memory calculation error");
} }
...@@ -937,7 +944,11 @@ public class Page { ...@@ -937,7 +944,11 @@ public class Page {
return memory; return memory;
} }
private int calculateMemory() { private void addMemory(int mem) {
memory += mem;
}
private void recalculateMemory() {
int mem = DataUtils.PAGE_MEMORY; int mem = DataUtils.PAGE_MEMORY;
DataType keyType = map.getKeyType(); DataType keyType = map.getKeyType();
for (int i = 0; i < keyCount; i++) { for (int i = 0; i < keyCount; i++) {
...@@ -951,7 +962,7 @@ public class Page { ...@@ -951,7 +962,7 @@ public class Page {
} else { } else {
mem += this.getChildPageCount() * DataUtils.PAGE_MEMORY_CHILD; mem += this.getChildPageCount() * DataUtils.PAGE_MEMORY_CHILD;
} }
return mem; addMemory(mem - memory);
} }
void setVersion(long version) { void setVersion(long version) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论