aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/packfile/diff_delta.go
diff options
context:
space:
mode:
authorMiguel Molina <miguel@erizocosmi.co>2017-09-06 12:13:41 +0200
committerMiguel Molina <miguel@erizocosmi.co>2017-09-06 12:13:41 +0200
commit0b68b0fd8e8a0f57419a72479ddeb8d267f15f72 (patch)
treea6b49669163d3a0a1c85141513eeb52c3ee6561e /plumbing/format/packfile/diff_delta.go
parent2dc59a6925f47e9d1bc99dd64d86e37b79b65d11 (diff)
downloadgo-git-0b68b0fd8e8a0f57419a72479ddeb8d267f15f72.tar.gz
packfile: reuse delta indexes when possible
Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
Diffstat (limited to 'plumbing/format/packfile/diff_delta.go')
-rw-r--r--plumbing/format/packfile/diff_delta.go17
1 files changed, 12 insertions, 5 deletions
diff --git a/plumbing/format/packfile/diff_delta.go b/plumbing/format/packfile/diff_delta.go
index 7e9f822..d0db46b 100644
--- a/plumbing/format/packfile/diff_delta.go
+++ b/plumbing/format/packfile/diff_delta.go
@@ -26,6 +26,10 @@ const (
// To generate target again, you will need the obtained object and "base" one.
// Error will be returned if base or target object cannot be read.
func GetDelta(base, target plumbing.EncodedObject) (plumbing.EncodedObject, error) {
+ return getDelta(make(deltaIndex), base, target)
+}
+
+func getDelta(index deltaIndex, base, target plumbing.EncodedObject) (plumbing.EncodedObject, error) {
br, err := base.Reader()
if err != nil {
return nil, err
@@ -45,7 +49,7 @@ func GetDelta(base, target plumbing.EncodedObject) (plumbing.EncodedObject, erro
return nil, err
}
- db := DiffDelta(bb, tb)
+ db := DiffDelta(index, bb, tb)
delta := &plumbing.MemoryObject{}
_, err = delta.Write(db)
if err != nil {
@@ -59,13 +63,15 @@ func GetDelta(base, target plumbing.EncodedObject) (plumbing.EncodedObject, erro
}
// DiffDelta returns the delta that transforms src into tgt.
-func DiffDelta(src []byte, tgt []byte) []byte {
+func DiffDelta(sindex deltaIndex, src []byte, tgt []byte) []byte {
buf := bufPool.Get().(*bytes.Buffer)
buf.Reset()
buf.Write(deltaEncodeSize(len(src)))
buf.Write(deltaEncodeSize(len(tgt)))
- sindex := initMatch(src)
+ if len(sindex) == 0 {
+ initMatch(sindex, src)
+ }
ibuf := bufPool.Get().(*bytes.Buffer)
ibuf.Reset()
@@ -126,9 +132,8 @@ func encodeInsertOperation(ibuf, buf *bytes.Buffer) {
ibuf.Reset()
}
-func initMatch(src []byte) map[uint32]int {
+func initMatch(index map[uint32]int, src []byte) map[uint32]int {
i := 0
- index := make(map[uint32]int)
for {
if i+s > len(src) {
break
@@ -213,3 +218,5 @@ func encodeCopyOperation(offset, length int) []byte {
return append([]byte{byte(code)}, opcodes...)
}
+
+type deltaIndex map[uint32]int