mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-22 09:54:36 +02:00
Merge pull request #694 from orisano/feat-optimize-build
feat: optimize build
This commit is contained in:
+2
-2
@@ -588,10 +588,10 @@ func excludeFile(path, buildcontext string) bool {
|
||||
|
||||
// HasFilepathPrefix checks if the given file path begins with prefix
|
||||
func HasFilepathPrefix(path, prefix string, prefixMatchOnly bool) bool {
|
||||
path = filepath.Clean(path)
|
||||
prefix = filepath.Clean(prefix)
|
||||
pathArray := strings.Split(path, "/")
|
||||
prefixArray := strings.Split(prefix, "/")
|
||||
path = filepath.Clean(path)
|
||||
pathArray := strings.SplitN(path, "/", len(prefixArray)+1)
|
||||
|
||||
if len(pathArray) < len(prefixArray) {
|
||||
return false
|
||||
|
||||
@@ -19,11 +19,13 @@ package util
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/GoogleContainerTools/kaniko/testutil"
|
||||
@@ -342,6 +344,84 @@ func TestHasFilepathPrefix(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHasFilepathPrefix(b *testing.B) {
|
||||
tests := []struct {
|
||||
path string
|
||||
prefix string
|
||||
prefixMatchOnly bool
|
||||
}{
|
||||
{
|
||||
path: "/foo/bar",
|
||||
prefix: "/foo",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar/baz",
|
||||
prefix: "/foo",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar/baz/foo",
|
||||
prefix: "/foo",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar/baz/foo/foobar",
|
||||
prefix: "/foo",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar",
|
||||
prefix: "/foo/bar",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar/baz",
|
||||
prefix: "/foo/bar",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar/baz/foo",
|
||||
prefix: "/foo/bar",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar/baz/foo/foobar",
|
||||
prefix: "/foo/bar",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar",
|
||||
prefix: "/foo/bar/baz",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar/baz",
|
||||
prefix: "/foo/bar/baz",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar/baz/foo",
|
||||
prefix: "/foo/bar/baz",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
{
|
||||
path: "/foo/bar/baz/foo/foobar",
|
||||
prefix: "/foo/bar/baz",
|
||||
prefixMatchOnly: true,
|
||||
},
|
||||
}
|
||||
for _, ts := range tests {
|
||||
name := fmt.Sprint("PathDepth=", strings.Count(ts.path, "/"), ",PrefixDepth=", strings.Count(ts.prefix, "/"))
|
||||
b.Run(name, func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
for i := 0; i < b.N; i++ {
|
||||
HasFilepathPrefix(ts.path, ts.prefix, ts.prefixMatchOnly)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type checker func(root string, t *testing.T)
|
||||
|
||||
func fileExists(p string) checker {
|
||||
|
||||
+13
-2
@@ -23,8 +23,10 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
highwayhash "github.com/minio/HighwayHash"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -44,8 +46,15 @@ func ConfigureLogging(logLevel string) error {
|
||||
|
||||
// Hasher returns a hash function, used in snapshotting to determine if a file has changed
|
||||
func Hasher() func(string) (string, error) {
|
||||
pool := sync.Pool{
|
||||
New: func() interface{} {
|
||||
b := make([]byte, highwayhash.Size*10*1024)
|
||||
return &b
|
||||
},
|
||||
}
|
||||
key := make([]byte, highwayhash.Size)
|
||||
hasher := func(p string) (string, error) {
|
||||
h := md5.New()
|
||||
h, _ := highwayhash.New(key)
|
||||
fi, err := os.Lstat(p)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -63,7 +72,9 @@ func Hasher() func(string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
buf := pool.Get().(*[]byte)
|
||||
defer pool.Put(buf)
|
||||
if _, err := io.CopyBuffer(h, f, *buf); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user