Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 865d49c

Browse files
authored
Merge pull request #694 from orisano/feat-optimize-build
feat: optimize build
2 parents eee6f83 + 38fa360 commit 865d49c

29 files changed

Lines changed: 1823 additions & 5 deletions

Gopkg.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,7 @@ required = [
4646
[[constraint]]
4747
name = "gopkg.in/src-d/go-git.v4"
4848
version = "4.6.0"
49+
50+
[[constraint]]
51+
name = "github.com/minio/HighwayHash"
52+
version = "1.0.0"

pkg/util/fs_util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,10 @@ func excludeFile(path, buildcontext string) bool {
588588

589589
// HasFilepathPrefix checks if the given file path begins with prefix
590590
func HasFilepathPrefix(path, prefix string, prefixMatchOnly bool) bool {
591-
path = filepath.Clean(path)
592591
prefix = filepath.Clean(prefix)
593-
pathArray := strings.Split(path, "/")
594592
prefixArray := strings.Split(prefix, "/")
593+
path = filepath.Clean(path)
594+
pathArray := strings.SplitN(path, "/", len(prefixArray)+1)
595595

596596
if len(pathArray) < len(prefixArray) {
597597
return false

pkg/util/fs_util_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ package util
1919
import (
2020
"archive/tar"
2121
"bytes"
22+
"fmt"
2223
"io/ioutil"
2324
"os"
2425
"path/filepath"
2526
"reflect"
2627
"sort"
28+
"strings"
2729
"testing"
2830

2931
"github.com/GoogleContainerTools/kaniko/testutil"
@@ -342,6 +344,84 @@ func TestHasFilepathPrefix(t *testing.T) {
342344
}
343345
}
344346

347+
func BenchmarkHasFilepathPrefix(b *testing.B) {
348+
tests := []struct {
349+
path string
350+
prefix string
351+
prefixMatchOnly bool
352+
}{
353+
{
354+
path: "/foo/bar",
355+
prefix: "/foo",
356+
prefixMatchOnly: true,
357+
},
358+
{
359+
path: "/foo/bar/baz",
360+
prefix: "/foo",
361+
prefixMatchOnly: true,
362+
},
363+
{
364+
path: "/foo/bar/baz/foo",
365+
prefix: "/foo",
366+
prefixMatchOnly: true,
367+
},
368+
{
369+
path: "/foo/bar/baz/foo/foobar",
370+
prefix: "/foo",
371+
prefixMatchOnly: true,
372+
},
373+
{
374+
path: "/foo/bar",
375+
prefix: "/foo/bar",
376+
prefixMatchOnly: true,
377+
},
378+
{
379+
path: "/foo/bar/baz",
380+
prefix: "/foo/bar",
381+
prefixMatchOnly: true,
382+
},
383+
{
384+
path: "/foo/bar/baz/foo",
385+
prefix: "/foo/bar",
386+
prefixMatchOnly: true,
387+
},
388+
{
389+
path: "/foo/bar/baz/foo/foobar",
390+
prefix: "/foo/bar",
391+
prefixMatchOnly: true,
392+
},
393+
{
394+
path: "/foo/bar",
395+
prefix: "/foo/bar/baz",
396+
prefixMatchOnly: true,
397+
},
398+
{
399+
path: "/foo/bar/baz",
400+
prefix: "/foo/bar/baz",
401+
prefixMatchOnly: true,
402+
},
403+
{
404+
path: "/foo/bar/baz/foo",
405+
prefix: "/foo/bar/baz",
406+
prefixMatchOnly: true,
407+
},
408+
{
409+
path: "/foo/bar/baz/foo/foobar",
410+
prefix: "/foo/bar/baz",
411+
prefixMatchOnly: true,
412+
},
413+
}
414+
for _, ts := range tests {
415+
name := fmt.Sprint("PathDepth=", strings.Count(ts.path, "/"), ",PrefixDepth=", strings.Count(ts.prefix, "/"))
416+
b.Run(name, func(b *testing.B) {
417+
b.ReportAllocs()
418+
for i := 0; i < b.N; i++ {
419+
HasFilepathPrefix(ts.path, ts.prefix, ts.prefixMatchOnly)
420+
}
421+
})
422+
}
423+
}
424+
345425
type checker func(root string, t *testing.T)
346426

347427
func fileExists(p string) checker {

pkg/util/util.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ import (
2323
"io"
2424
"os"
2525
"strconv"
26+
"sync"
2627
"syscall"
2728

29+
highwayhash "github.com/minio/HighwayHash"
2830
"github.com/pkg/errors"
2931
"github.com/sirupsen/logrus"
3032
)
@@ -44,8 +46,15 @@ func ConfigureLogging(logLevel string) error {
4446

4547
// Hasher returns a hash function, used in snapshotting to determine if a file has changed
4648
func Hasher() func(string) (string, error) {
49+
pool := sync.Pool{
50+
New: func() interface{} {
51+
b := make([]byte, highwayhash.Size*10*1024)
52+
return &b
53+
},
54+
}
55+
key := make([]byte, highwayhash.Size)
4756
hasher := func(p string) (string, error) {
48-
h := md5.New()
57+
h, _ := highwayhash.New(key)
4958
fi, err := os.Lstat(p)
5059
if err != nil {
5160
return "", err
@@ -63,7 +72,9 @@ func Hasher() func(string) (string, error) {
6372
return "", err
6473
}
6574
defer f.Close()
66-
if _, err := io.Copy(h, f); err != nil {
75+
buf := pool.Get().(*[]byte)
76+
defer pool.Put(buf)
77+
if _, err := io.CopyBuffer(h, f, *buf); err != nil {
6778
return "", err
6879
}
6980
}

vendor/github.com/minio/HighwayHash/LICENSE

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)