Enforce max memory for DeltaWindow.
Previously, memory limits were enforced at the start of each iteration of the delta search, based on objects that were currently loaded in memory. However, new objects added to the window may be expanded in a future iteration of the search and thus were not accounted for correctly at the start of the search. To fix this, memory limits are now enforced before each object is loaded. Change-Id: I898ab43e7bf5ee7189831f3a68bb9385ae694b8f
This commit is contained in:
parent
03d50dc50b
commit
51beee5568
|
@ -164,6 +164,14 @@ private static long estimateSize(ObjectToPack ent) {
|
||||||
return DeltaIndex.estimateIndexSize(ent.getWeight());
|
return DeltaIndex.estimateIndexSize(ent.getWeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static long estimateIndexSize(DeltaWindowEntry ent) {
|
||||||
|
if (ent.buffer == null)
|
||||||
|
return estimateSize(ent.object);
|
||||||
|
|
||||||
|
int len = ent.buffer.length;
|
||||||
|
return DeltaIndex.estimateIndexSize(len) - len;
|
||||||
|
}
|
||||||
|
|
||||||
private void clear(DeltaWindowEntry ent) {
|
private void clear(DeltaWindowEntry ent) {
|
||||||
if (ent.index != null)
|
if (ent.index != null)
|
||||||
loaded -= ent.index.getIndexSize();
|
loaded -= ent.index.getIndexSize();
|
||||||
|
@ -420,6 +428,8 @@ private DeltaIndex index(DeltaWindowEntry ent)
|
||||||
IOException, LargeObjectException {
|
IOException, LargeObjectException {
|
||||||
DeltaIndex idx = ent.index;
|
DeltaIndex idx = ent.index;
|
||||||
if (idx == null) {
|
if (idx == null) {
|
||||||
|
checkLoadable(ent, estimateIndexSize(ent));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
idx = new DeltaIndex(buffer(ent));
|
idx = new DeltaIndex(buffer(ent));
|
||||||
} catch (OutOfMemoryError noMemory) {
|
} catch (OutOfMemoryError noMemory) {
|
||||||
|
@ -439,6 +449,8 @@ private byte[] buffer(DeltaWindowEntry ent) throws MissingObjectException,
|
||||||
IncorrectObjectTypeException, IOException, LargeObjectException {
|
IncorrectObjectTypeException, IOException, LargeObjectException {
|
||||||
byte[] buf = ent.buffer;
|
byte[] buf = ent.buffer;
|
||||||
if (buf == null) {
|
if (buf == null) {
|
||||||
|
checkLoadable(ent, ent.size());
|
||||||
|
|
||||||
buf = PackWriter.buffer(config, reader, ent.object);
|
buf = PackWriter.buffer(config, reader, ent.object);
|
||||||
if (0 < maxMemory)
|
if (0 < maxMemory)
|
||||||
loaded += buf.length;
|
loaded += buf.length;
|
||||||
|
@ -447,6 +459,18 @@ private byte[] buffer(DeltaWindowEntry ent) throws MissingObjectException,
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void checkLoadable(DeltaWindowEntry ent, long need) {
|
||||||
|
int tail = next(resSlot);
|
||||||
|
while (maxMemory < loaded + need) {
|
||||||
|
DeltaWindowEntry cur = window[tail];
|
||||||
|
clear(cur);
|
||||||
|
if (cur == ent)
|
||||||
|
throw new LargeObjectException.ExceedsLimit(
|
||||||
|
maxMemory, loaded + need);
|
||||||
|
tail = next(tail);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Deflater deflater() {
|
private Deflater deflater() {
|
||||||
if (deflater == null)
|
if (deflater == null)
|
||||||
deflater = new Deflater(config.getCompressionLevel());
|
deflater = new Deflater(config.getCompressionLevel());
|
||||||
|
|
Loading…
Reference in New Issue