Skip to content

Commit 0993a9a

Browse files
jepiokrnowak
authored andcommitted
profiles: slsa: Remove quadratic complexity in SRC_URI iteration
SLSA provenance generation iterates over $A (which is a subset of $SRC_URI) and for each of those tries to find a match in $SRC_URI. That's quadratic complexity, and the performance impact is bad because we shell out to a helper utility (basename) for every entry. This is leading to long stalls when generating SLSA for packages with long distfile lists, like go and rust packages. Iterate over SRC_URI once and create a dictionary to speed up subsequent lookups. dev-db/etcdctl is a good candidate for testing. Signed-off-by: Jeremi Piotrowski <jpiotrowski@microsoft.com>
1 parent 9faab43 commit 0993a9a

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

sdk_container/src/third_party/coreos-overlay/profiles/coreos/base/profile.bashrc.slsa-provenance

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -133,35 +133,35 @@ __slsa_provenance_resolved_dependencies() {
133133
# There can be multiple, and can be used conditionally based on use flags,
134134
# and even replaced with different local names ("http://... -> othername.tgz"). So
135135
# we go through what's actually used ($A), then find the corresponding source URI.
136-
local src prev_uri rename orig_name found
137-
for src in ${A}; do
138-
found=
136+
declare -A uri_dict=() uri_orig_names=()
137+
local prev_uri='' rename='' base_name prev_base_name
138+
for uri in ${SRC_URI}; do
139+
if [[ ${uri} = '->' ]] ; then
140+
rename=x
141+
continue
142+
fi
143+
base_name=$(basename "${uri}")
144+
uri_orig_names["${uri}"]=${base_name}
145+
if [[ -n ${rename} ]] ; then
146+
unset "uri_dict[${prev_base_name}]"
147+
uri=${prev_uri}
148+
fi
149+
uri_dict["${base_name}"]=${uri}
139150
rename=
140-
prev_uri=''
141-
orig_name=''
142-
for uri in ${SRC_URI}; do
143-
if [[ ${uri} = '->' ]] ; then
144-
rename=x
145-
continue
146-
fi
147-
if [[ ${src} = "$(basename "${uri}")" ]] ; then
148-
orig_name=${src}
149-
if [[ -n ${rename} ]] ; then
150-
uri=${prev_uri}
151-
orig_name=$(basename "${uri}")
152-
fi
153-
einfo " Provenance: recording tarball material (input) '${src}' ('${orig_name}')"
154-
csum=$(sha512sum "${DISTDIR}/${src}")
155-
csum=${csum%% *}
156-
__slsa_rd_printf "${uri}" 'sha512' "${csum}"
157-
found=x
158-
fi
159-
rename=
160-
prev_uri=${uri}
161-
done
162-
if [[ -z ${found} ]] ; then
151+
prev_uri=${uri}
152+
prev_base_name=${base_name}
153+
done
154+
local src orig_name
155+
for src in ${A}; do
156+
uri=${uri_dict["${src}"]:-}
157+
if [[ -z ${uri} ]] ; then
163158
die "No SRC_URI found for source '${src}', unable to record provenance!"
164159
fi
160+
orig_name=${uri_orig_names["${uri}"]}
161+
einfo " Provenance: recording tarball material (input) '${src}' ('${orig_name}')"
162+
csum=$(sha512sum "${DISTDIR}/${src}")
163+
csum=${csum%% *}
164+
__slsa_rd_printf "${uri}" 'sha512' "${csum}"
165165
done
166166
elif [[ -n ${EGIT_REPO_URI:-} ]] ; then
167167
# package is built from repo checkout (git)

0 commit comments

Comments
 (0)