base recipe: make EXISTING_BINS filtering fast (one scan, not N full-tree greps)

The driver-list filter ran `grep -R "^name = \"$bin\"$" $COOKBOOK_SOURCE`
once PER bin (~40 bins). $COOKBOOK_SOURCE is local/sources/base, whose
target/ build tree is ~8.7 GB, so each grep re-scanned gigabytes and the
whole loop scanned hundreds of GB — a very slow build phase.

Collect the workspace's crate names ONCE from Cargo.toml files (skipping
target/ via --exclude-dir), into a bash associative array, then do an
O(1) membership check per bin. Verified equivalent (same bins selected)
and near-instant: the single scan of local/sources/base completes in
~0.02s vs minutes for the per-bin recursive greps.
This commit is contained in:
2026-07-24 08:14:48 +09:00
parent 5c9bce4b88
commit 5d04ddc593
+12 -2
View File
@@ -160,11 +160,21 @@ esac
mkdir -pv "${COOKBOOK_STAGE}/usr/bin" "${COOKBOOK_STAGE}/usr/lib/drivers"
export CARGO_PROFILE_RELEASE_OPT_LEVEL=s
export CARGO_PROFILE_RELEASE_PANIC=abort
# Only build drivers that actually have source Cargo.toml entries
# Only build drivers that actually have source Cargo.toml entries.
# Collect the workspace's crate names ONCE from Cargo.toml files, skipping the
# multi-GB target/ build trees. The previous code ran a full recursive grep of
# the entire source (including target/, ~8.7 GB) for EVERY bin (~40 of them),
# re-scanning gigabytes per entry — extremely slow. One scan + O(1) membership
# lookup is equivalent and near-instant.
declare -A HAVE_CRATE=()
while IFS= read -r crate_name
do
HAVE_CRATE["${crate_name}"]=1
done < <(grep -Rhs --include=Cargo.toml --exclude-dir=target '^name = ' "${COOKBOOK_SOURCE}" | sed -E 's/^name = "([^"]+)".*/\\1/')
EXISTING_BINS=()
for bin in "${BINS[@]}"
do
if grep -Rqs "^name = \\\"${bin}\\\"$" "${COOKBOOK_SOURCE}"; then
if [[ -n "${HAVE_CRATE[${bin}]:-}" ]]; then
EXISTING_BINS+=("${bin}")
fi
done