1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/*
* Copyright (C) 2010, Google Inc.
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
* under the terms of the Eclipse Distribution License v1.0 which
* accompanies this distribution, is reproduced below, and is
* available at http://www.eclipse.org/org/documents/edl-v10.php
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* - Neither the name of the Eclipse Foundation, Inc. nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.eclipse.jgit.storage.pack;
import java.io.EOFException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.Deflater;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.LargeObjectException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.ProgressMonitor;
import org.eclipse.jgit.util.TemporaryBuffer;
class DeltaWindow {
private static final int NEXT_RES = 0;
private static final int NEXT_SRC = 1;
private final PackConfig config;
private final DeltaCache deltaCache;
private final ObjectReader reader;
private final DeltaWindowEntry[] window;
/** Maximum number of bytes to admit to the window at once. */
private final long maxMemory;
/** Maximum depth we should create for any delta chain. */
private final int maxDepth;
/** Amount of memory we have loaded right now. */
private long loaded;
// The object we are currently considering needs a lot of state:
/** Position of {@link #res} within {@link #window} array. */
private int resSlot;
/**
* Maximum delta chain depth the current object can have.
* <p>
* This can be smaller than {@link #maxDepth}.
*/
private int resMaxDepth;
/** Window entry of the object we are currently considering. */
private DeltaWindowEntry res;
/** If we have a delta for {@link #res}, this is the shortest found yet. */
private TemporaryBuffer.Heap bestDelta;
/** If we have {@link #bestDelta}, the window position it was created by. */
private int bestSlot;
/** Used to compress cached deltas. */
private Deflater deflater;
DeltaWindow(PackConfig pc, DeltaCache dc, ObjectReader or) {
config = pc;
deltaCache = dc;
reader = or;
// C Git increases the window size supplied by the user by 1.
// We don't know why it does this, but if the user asks for
// window=10, it actually processes with window=11. Because
// the window size has the largest direct impact on the final
// pack file size, we match this odd behavior here to give us
// a better chance of producing a similar sized pack as C Git.
//
// We would prefer to directly honor the user's request since
// PackWriter has a minimum of 2 for the window size, but then
// users might complain that JGit is creating a bigger pack file.
//
window = new DeltaWindowEntry[config.getDeltaSearchWindowSize() + 1];
for (int i = 0; i < window.length; i++)
window[i] = new DeltaWindowEntry();
maxMemory = config.getDeltaSearchMemoryLimit();
maxDepth = config.getMaxDeltaDepth();
}
void search(ProgressMonitor monitor, ObjectToPack[] toSearch, int off,
int cnt) throws IOException {
try {
for (int end = off + cnt; off < end; off++) {
res = window[resSlot];
if (0 < maxMemory) {
clear(res);
int tail = next(resSlot);
final long need = estimateSize(toSearch[off]);
while (maxMemory < loaded + need && tail != resSlot) {
clear(window[tail]);
tail = next(tail);
}
}
res.set(toSearch[off]);
if (res.object.isEdge() || res.object.doNotAttemptDelta()) {
// We don't actually want to make a delta for
// them, just need to push them into the window
// so they can be read by other objects.
//
keepInWindow();
} else {
// Search for a delta for the current window slot.
//
monitor.update(1);
search();
}
}
} finally {
if (deflater != null)
deflater.end();
}
}
private static long estimateSize(ObjectToPack ent) {
return DeltaIndex.estimateIndexSize(ent.getWeight());
}
private void clear(DeltaWindowEntry ent) {
if (ent.index != null)
loaded -= ent.index.getIndexSize();
else if (res.buffer != null)
loaded -= ent.buffer.length;
ent.set(null);
}
private void search() throws IOException {
// TODO(spearce) If the object is used as a base for other
// objects in this pack we should limit the depth we create
// for ourselves to be the remainder of our longest dependent
// chain and the configured maximum depth. This can happen
// when the dependents are being reused out a pack, but we
// cannot be because we are near the edge of a thin pack.
//
resMaxDepth = maxDepth;
// Loop through the window backwards, considering every entry.
// This lets us look at the bigger objects that came before.
//
for (int srcSlot = prior(resSlot); srcSlot != resSlot; srcSlot = prior(srcSlot)) {
DeltaWindowEntry src = window[srcSlot];
if (src.empty())
break;
if (delta(src, srcSlot) == NEXT_RES) {
bestDelta = null;
return;
}
}
// We couldn't find a suitable delta for this object, but it may
// still be able to act as a base for another one.
//
if (bestDelta == null) {
keepInWindow();
return;
}
// Select this best matching delta as the base for the object.
//
ObjectToPack srcObj = window[bestSlot].object;
ObjectToPack resObj = res.object;
if (srcObj.isEdge()) {
// The source (the delta base) is an edge object outside of the
// pack. Its part of the common base set that the peer already
// has on hand, so we don't want to send it. We have to store
// an ObjectId and *NOT* an ObjectToPack for the base to ensure
// the base isn't included in the outgoing pack file.
//
resObj.setDeltaBase(srcObj.copy());
} else {
// The base is part of the pack we are sending, so it should be
// a direct pointer to the base.
//
resObj.setDeltaBase(srcObj);
}
resObj.setDeltaDepth(srcObj.getDeltaDepth() + 1);
resObj.clearReuseAsIs();
cacheDelta(srcObj, resObj);
// Discard the cached best result, otherwise it leaks.
//
bestDelta = null;
// If this should be the end of a chain, don't keep
// it in the window. Just move on to the next object.
//
if (resObj.getDeltaDepth() == maxDepth)
return;
shuffleBaseUpInPriority();
keepInWindow();
}
private int delta(final DeltaWindowEntry src, final int srcSlot)
throws IOException {
// Objects must use only the same type as their delta base.
// If we are looking at something where that isn't true we
// have exhausted everything of the correct type and should
// move on to the next thing to examine.
//
if (src.type() != res.type()) {
keepInWindow();
return NEXT_RES;
}
// Only consider a source with a short enough delta chain.
if (src.depth() > resMaxDepth)
return NEXT_SRC;
// Estimate a reasonable upper limit on delta size.
int msz = deltaSizeLimit(res, resMaxDepth, src);
if (msz <= 8)
return NEXT_SRC;
// If we have to insert a lot to make this work, find another.
if (res.size() - src.size() > msz)
return NEXT_SRC;
// If the sizes are radically different, this is a bad pairing.
if (res.size() < src.size() / 16)
return NEXT_SRC;
DeltaIndex srcIndex;
try {
srcIndex = index(src);
} catch (LargeObjectException tooBig) {
// If the source is too big to work on, skip it.
dropFromWindow(srcSlot);
return NEXT_SRC;
} catch (IOException notAvailable) {
if (src.object.isEdge()) {
// This is an edge that is suddenly not available.
dropFromWindow(srcSlot);
return NEXT_SRC;
} else {
throw notAvailable;
}
}
byte[] resBuf;
try {
resBuf = buffer(res);
} catch (LargeObjectException tooBig) {
// If its too big, move on to another item.
return NEXT_RES;
}
// If we already have a delta for the current object, abort
// encoding early if this new pairing produces a larger delta.
if (bestDelta != null && bestDelta.length() < msz)
msz = (int) bestDelta.length();
TemporaryBuffer.Heap delta = new TemporaryBuffer.Heap(msz);
try {
if (!srcIndex.encode(delta, resBuf, msz))
return NEXT_SRC;
} catch (IOException deltaTooBig) {
// This only happens when the heap overflows our limit.
return NEXT_SRC;
}
if (isBetterDelta(src, delta)) {
bestDelta = delta;
bestSlot = srcSlot;
}
return NEXT_SRC;
}
private void cacheDelta(ObjectToPack srcObj, ObjectToPack resObj) {
if (Integer.MAX_VALUE < bestDelta.length())
return;
int rawsz = (int) bestDelta.length();
if (deltaCache.canCache(rawsz, srcObj, resObj)) {
try {
byte[] zbuf = new byte[deflateBound(rawsz)];
ZipStream zs = new ZipStream(deflater(), zbuf);
bestDelta.writeTo(zs, null);
bestDelta = null;
int len = zs.finish();
resObj.setCachedDelta(deltaCache.cache(zbuf, len, rawsz));
resObj.setCachedSize(rawsz);
} catch (IOException err) {
deltaCache.credit(rawsz);
} catch (OutOfMemoryError err) {
deltaCache.credit(rawsz);
}
}
}
private static int deflateBound(int insz) {
return insz + ((insz + 7) >> 3) + ((insz + 63) >> 6) + 11;
}
private void shuffleBaseUpInPriority() {
// Shuffle the entire window so that the best match we just used
// is at our current index, and our current object is at the index
// before it. Slide any entries in between to make space.
//
window[resSlot] = window[bestSlot];
DeltaWindowEntry next = res;
int slot = prior(resSlot);
for (; slot != bestSlot; slot = prior(slot)) {
DeltaWindowEntry e = window[slot];
window[slot] = next;
next = e;
}
window[slot] = next;
}
private void keepInWindow() {
resSlot = next(resSlot);
}
private int next(int slot) {
if (++slot == window.length)
return 0;
return slot;
}
private int prior(int slot) {
if (slot == 0)
return window.length - 1;
return slot - 1;
}
private void dropFromWindow(@SuppressWarnings("unused") int srcSlot) {
// We should drop the current source entry from the window,
// it is somehow invalid for us to work with.
}
private boolean isBetterDelta(DeltaWindowEntry src,
TemporaryBuffer.Heap resDelta) {
if (bestDelta == null)
return true;
// If both delta sequences are the same length, use the one
// that has a shorter delta chain since it would be faster
// to access during reads.
//
if (resDelta.length() == bestDelta.length())
return src.depth() < window[bestSlot].depth();
return resDelta.length() < bestDelta.length();
}
private static int deltaSizeLimit(DeltaWindowEntry res, int maxDepth,
DeltaWindowEntry src) {
// Ideally the delta is at least 50% of the original size,
// but we also want to account for delta header overhead in
// the pack file (to point to the delta base) so subtract off
// some of those header bytes from the limit.
//
final int limit = res.size() / 2 - 20;
// Distribute the delta limit over the entire chain length.
// This is weighted such that deeper items in the chain must
// be even smaller than if they were earlier in the chain, as
// they cost significantly more to unpack due to the increased
// number of recursive unpack calls.
//
final int remainingDepth = maxDepth - src.depth();
return (limit * remainingDepth) / maxDepth;
}
private DeltaIndex index(DeltaWindowEntry ent)
throws MissingObjectException, IncorrectObjectTypeException,
IOException, LargeObjectException {
DeltaIndex idx = ent.index;
if (idx == null) {
try {
idx = new DeltaIndex(buffer(ent));
} catch (OutOfMemoryError noMemory) {
LargeObjectException.OutOfMemory e;
e = new LargeObjectException.OutOfMemory(noMemory);
e.setObjectId(ent.object);
throw e;
}
if (0 < maxMemory)
loaded += idx.getIndexSize() - idx.getSourceSize();
ent.index = idx;
}
return idx;
}
private byte[] buffer(DeltaWindowEntry ent) throws MissingObjectException,
IncorrectObjectTypeException, IOException, LargeObjectException {
byte[] buf = ent.buffer;
if (buf == null) {
buf = PackWriter.buffer(config, reader, ent.object);
if (0 < maxMemory)
loaded += buf.length;
ent.buffer = buf;
}
return buf;
}
private Deflater deflater() {
if (deflater == null)
deflater = new Deflater(config.getCompressionLevel());
else
deflater.reset();
return deflater;
}
static final class ZipStream extends OutputStream {
private final Deflater deflater;
private final byte[] zbuf;
private int outPtr;
ZipStream(Deflater deflater, byte[] zbuf) {
this.deflater = deflater;
this.zbuf = zbuf;
}
int finish() throws IOException {
deflater.finish();
for (;;) {
if (outPtr == zbuf.length)
throw new EOFException();
int n = deflater.deflate(zbuf, outPtr, zbuf.length - outPtr);
if (n == 0) {
if (deflater.finished())
return outPtr;
throw new IOException();
}
outPtr += n;
}
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
deflater.setInput(b, off, len);
for (;;) {
if (outPtr == zbuf.length)
throw new EOFException();
int n = deflater.deflate(zbuf, outPtr, zbuf.length - outPtr);
if (n == 0) {
if (deflater.needsInput())
break;
throw new IOException();
}
outPtr += n;
}
}
@Override
public void write(int b) throws IOException {
throw new UnsupportedOperationException();
}
}
}
|