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

Commit 509b128

Browse files
committed
feat: avoid ioutil.ReadAll on ApplyDelta
Signed-off-by: Nao YONASHIRO <owan.orisano@gmail.com>
1 parent 6241d0e commit 509b128

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

plumbing/format/packfile/patch_delta.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package packfile
22

33
import (
4+
"bytes"
45
"errors"
5-
"io/ioutil"
6+
"sync"
67

78
"gopkg.in/src-d/go-git.v4/plumbing"
89
)
@@ -14,6 +15,12 @@ import (
1415

1516
const deltaSizeMin = 4
1617

18+
var bytesBufferPool = sync.Pool{
19+
New: func() interface{} {
20+
return &bytes.Buffer{}
21+
},
22+
}
23+
1724
// ApplyDelta writes to target the result of applying the modification deltas in delta to base.
1825
func ApplyDelta(target, base plumbing.EncodedObject, delta []byte) error {
1926
r, err := base.Reader()
@@ -26,10 +33,16 @@ func ApplyDelta(target, base plumbing.EncodedObject, delta []byte) error {
2633
return err
2734
}
2835

29-
src, err := ioutil.ReadAll(r)
36+
buf := bytesBufferPool.Get().(*bytes.Buffer)
37+
defer func() {
38+
buf.Reset()
39+
bytesBufferPool.Put(buf)
40+
} ()
41+
_, err = buf.ReadFrom(r)
3042
if err != nil {
3143
return err
3244
}
45+
src := buf.Bytes()
3346

3447
dst, err := PatchDelta(src, delta)
3548
if err != nil {

0 commit comments

Comments
 (0)