Skip to content

Commit

Permalink
Make builds faster by caching ALL_GO_DIRS
Browse files Browse the repository at this point in the history
This operation takes 2-5 seconds on every build, but doesn't actually need to
run most of the time.  Now we cache it and see if it needs a rebuild (fast)
before actually rebuilding (slow).
  • Loading branch information
thockin committed Jul 21, 2016
1 parent 5315fd6 commit 6659667
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
21 changes: 4 additions & 17 deletions Makefile.generated_files
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,11 @@ generated_files: gen_deepcopy gen_conversion
ifeq ($(DBG_MAKEFILE),1)
$(warning ***** finding all *.go dirs)
endif
ALL_GO_DIRS := $(shell \
find . \
-not \( \
\( \
-path ./vendor -o \
-path ./_\* -o \
-path ./.\* -o \
-path ./docs -o \
-path ./examples \
\) -prune \
\) \
-type f -name \*.go \
| sed 's|^./||' \
| xargs -n1 dirname \
| sort -u \
ALL_GO_DIRS := $(shell \
hack/make-rules/helpers/cache_go_dirs.sh $(META_DIR)/all_go_dirs.mk \
)

# The name of the make metadata file listing Go files.
# The name of the metadata file which lists *.go files in each pkg.
GOFILES_META := gofiles.mk

# Establish a dependency between the deps file and the dir. Whenever a dir
Expand Down Expand Up @@ -314,7 +301,7 @@ CONVERSION_FILENAME := $(CONVERSION_BASENAME).go
# The tool used to generate conversions.
CONVERSION_GEN := $(BIN_DIR)/conversion-gen

# The name of the make metadata file controlling conversions.
# The name of the metadata file listing conversion peers for each pkg.
CONVERSIONS_META := conversions.mk

# All directories that request any form of conversion generation.
Expand Down
65 changes: 65 additions & 0 deletions hack/make-rules/helpers/cache_go_dirs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash

# Copyright 2014 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script finds, caches, and prints a list of all directories that hold
# *.go files. If any directory is newer than the cache, re-find everything and
# update the cache. Otherwise use the cached file.

set -o errexit
set -o nounset
set -o pipefail

if [[ -z "${1:-}" ]]; then
echo "usage: $0 <cache-file>"
exit 1
fi
CACHE="$1"; shift

# This is a partial 'find' command. The caller is expected to pass the
# remaining arguments.
#
# Example:
# kfind -type f -name foobar.go
function kfind() {
find . \
-not \( \
\( \
-path ./vendor -o \
-path ./_\* -o \
-path ./.\* -o \
-path ./docs -o \
-path ./examples \
\) -prune \
\) \
"$@"
}

NEED_FIND=true
# It's *significantly* faster to check whether any directories are newer than
# the cache than to blindly rebuild it.
if [[ -f "${CACHE}" ]]; then
N=$(kfind -type d -newer "${CACHE}" -print -quit | wc -l)
[[ "${N}" == 0 ]] && NEED_FIND=false
fi
mkdir -p $(dirname "${CACHE}")
if $("${NEED_FIND}"); then
kfind -type f -name \*.go \
| xargs -n1 dirname \
| sort -u \
| sed 's|^./||' \
> "${CACHE}"
fi
cat "${CACHE}"

0 comments on commit 6659667

Please sign in to comment.