Skip to content

Commit 411d0c8

Browse files
committed
Improve resolve-dependencies performance
1 parent e695e8a commit 411d0c8

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

scripts/resolve-dependencies.bash

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ replace_patch_crates_io() {
66
}
77

88
get_deps() {
9-
local all=$(sed -n -e '/^\[dependencies\]/,/^\[/{//!p}' "$1")
10-
echo "$all" | grep git | sed -n -e 's/^\([a-z0-9_-]*\)\(\.workspace\)*\s*=.*$/\1 git/p'
11-
echo "$all" | grep -v git | sed -n -e 's/^\([a-z0-9_-]*\)\(\.workspace\)*\s*=.*$/\1/p'
12-
13-
local all=$(sed -n -e '/^\[workspace\.dependencies\]/,/^\[/{//!p}' "$1")
14-
echo "$all" | grep git | sed -n -e 's/^\([a-z0-9_-]*\)\(\.workspace\)*\s*=.*$/\1 git/p'
15-
echo "$all" | grep -v git | sed -n -e 's/^\([a-z0-9_-]*\)\(\.workspace\)*\s*=.*$/\1/p'
9+
sed -n -e '/^\[\(workspace\.\)\{0,1\}dependencies\]/,/^\[/{
10+
/^[[]/d
11+
/git/ s/^\([a-z0-9_-]*\)\(\.workspace\)*\s*=.*$/\1 git/p
12+
/git/! s/^\([a-z0-9_-]*\)\(\.workspace\)*\s*=.*$/\1/p
13+
}' "$1"
1614
}
1715

1816
get_deps_with_path() {
@@ -23,7 +21,12 @@ get_deps_with_path() {
2321
local cargo_dep
2422
local cargo_git
2523

24+
[[ -n "${traversed_deps[$parent_dep_path]}" ]] && return
25+
traversed_deps[$parent_dep_path]=1
26+
2627
while read cargo_dep cargo_git; do
28+
29+
[[ -z "${found_deps["$cargo_dep"]}" ]] && continue
2730
local cargo_dep_path="${found_deps["$cargo_dep"]}"
2831
[[ -z "$cargo_dep_path" ]] && cargo_dep_path="${found_deps["$cargo_dep-rs"]}"
2932
[[ -z "$cargo_dep_path" ]] && continue
@@ -36,7 +39,7 @@ get_deps_with_path() {
3639
echo "$cargo_dep $cargo_dep_path"
3740
get_deps_with_path "$cargo_dep_path/Cargo.toml" "$cargo_dep" "$cargo_git"
3841
fi
39-
done < <(get_deps "$parent_dep_path")
42+
done < <(echo -n "${got_deps[$(realpath "$parent_dep_path")]}")
4043
}
4144

4245
update_deps() {
@@ -45,6 +48,9 @@ update_deps() {
4548

4649
replace_patch_crates_io "$cargo_toml"
4750

51+
unset traversed_deps
52+
declare -A traversed_deps
53+
4854
while read cargo_dep cargo_dep_path; do
4955
echo "$cargo_dep => $cargo_dep_path"
5056
grep -q "$cargo_dep.*git" "$cargo_toml" && continue # TODO: this is not fully working
@@ -58,16 +64,24 @@ search_dir="${1:-.}"
5864

5965
declare -A found_deps
6066
declare -A git_deps
67+
declare -A got_deps
68+
69+
echo "Resolving dependencies in $search_dir"
6170

6271
while read cargo_path; do
6372
cargo_path=$(dirname "$cargo_path")
6473
cargo_dep=$(basename "$cargo_path")
6574
git_repo=$(git -C "$cargo_path" rev-parse --show-toplevel)
6675
found_deps[$cargo_dep]="$cargo_path"
6776
git_deps[$cargo_dep]="$git_repo"
68-
done < <(find "$PWD" -wholename "*/Cargo.toml" | sort)
77+
got_deps[$(realpath "$cargo_path/Cargo.toml")]="$(get_deps "$cargo_path/Cargo.toml" | sort -u)"
78+
done < <(find "$PWD" -wholename "*/Cargo.toml" -not -path "*/vendor/*" | sort)
79+
80+
echo "Found dependencies:"
81+
echo "Total: ${#found_deps[@]}"
6982

7083
while read CARGO_TOML; do
84+
echo "Updating $CARGO_TOML"
7185
update_deps "$CARGO_TOML"
7286

7387
git_repo=$(git -C "$(dirname "$CARGO_TOML")" rev-parse --show-toplevel)
@@ -76,6 +90,6 @@ while read CARGO_TOML; do
7690
git -C "$git_repo" diff --cached | cat
7791
git -C "$git_repo" commit -m "resolve-dependencies.bash"
7892
fi
79-
done < <(find $1 -name Cargo.toml)
93+
done < <(find $1 -name Cargo.toml -not -path "*/vendor/*")
8094

8195
echo "Done."

0 commit comments

Comments
 (0)