Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit cb32722

Browse files
authored
Merge pull request #565 from strib/delta-sliding-window
plumbing: use sliding window in delta calculations, like git CL
2 parents bff1d06 + cdddb7a commit cb32722

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

plumbing/format/packfile/delta_selector.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
)
99

1010
const (
11+
// How far back in the sorted list to search for deltas. 10 is
12+
// the default in command line git.
13+
deltaWindowSize = 10
1114
// deltas based on deltas, how many steps we can do.
1215
// 50 is the default value used in JGit
1316
maxDepth = int64(50)
@@ -184,7 +187,7 @@ func (dw *deltaSelector) walk(objectsToPack []*ObjectToPack) error {
184187
continue
185188
}
186189

187-
for j := i - 1; j >= 0; j-- {
190+
for j := i - 1; j >= 0 && i-j < deltaWindowSize; j-- {
188191
base := objectsToPack[j]
189192
// Objects must use only the same type as their delta base.
190193
// Since objectsToPack is sorted by type and size, once we find

plumbing/format/packfile/delta_selector_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,25 @@ func (s *DeltaSelectorSuite) TestObjectsToPack(c *C) {
196196
c.Assert(otp[2].Original, Equals, s.store.Objects[s.hashes["o3"]])
197197
c.Assert(otp[2].IsDelta(), Equals, true)
198198
c.Assert(otp[2].Depth, Equals, 2)
199+
200+
// Check that objects outside of the sliding window don't produce
201+
// a delta.
202+
hashes = make([]plumbing.Hash, 0, deltaWindowSize+2)
203+
hashes = append(hashes, s.hashes["base"])
204+
for i := 0; i < deltaWindowSize; i++ {
205+
hashes = append(hashes, s.hashes["smallTarget"])
206+
}
207+
hashes = append(hashes, s.hashes["target"])
208+
209+
// Don't sort so we can easily check the sliding window without
210+
// creating a bunch of new objects.
211+
otp, err = s.ds.objectsToPack(hashes)
212+
c.Assert(err, IsNil)
213+
err = s.ds.walk(otp)
214+
c.Assert(err, IsNil)
215+
c.Assert(len(otp), Equals, deltaWindowSize+2)
216+
targetIdx := len(otp) - 1
217+
c.Assert(otp[targetIdx].IsDelta(), Equals, false)
199218
}
200219

201220
func (s *DeltaSelectorSuite) TestMaxDepth(c *C) {

0 commit comments

Comments
 (0)