Skip to content

Commit c9a8ef0

Browse files
committed
Optimize diff_lists()
1 parent b03c04e commit c9a8ef0

File tree

1 file changed

+9
-16
lines changed

1 file changed

+9
-16
lines changed

build/scripts/syncExtensions.sh

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,18 @@
22
# Synchronize Drupal extensions (modules and themes) to match core.extension.yml.
33
echo "Sync Extensions : Synchronizing modules and themes..."
44

5-
# Gets items in list1 but not in list2 (space-separated)
5+
# Gets items in list1 but not in list2 (space-separated) - O(n log n)
66
diff_lists() {
77
LIST1="$1"
88
LIST2="$2"
9-
RESULT=""
10-
for item in $LIST1; do
11-
[ -z "$item" ] && continue
12-
FOUND=0
13-
for check in $LIST2; do
14-
if [ "$item" = "$check" ]; then
15-
FOUND=1
16-
break
17-
fi
18-
done
19-
if [ "$FOUND" -eq 0 ]; then
20-
RESULT="$RESULT $item"
21-
fi
22-
done
23-
echo "$RESULT" | xargs
9+
# Handle empty lists
10+
[ -z "$LIST1" ] && return
11+
[ -z "$LIST2" ] && { echo "$LIST1" | xargs; return; }
12+
# Use comm for efficient set difference (requires sorted input)
13+
printf '%s\n' $LIST1 | sort > /tmp/diff_list1.$$
14+
printf '%s\n' $LIST2 | sort > /tmp/diff_list2.$$
15+
comm -23 /tmp/diff_list1.$$ /tmp/diff_list2.$$ | tr '\n' ' ' | xargs
16+
rm -f /tmp/diff_list1.$$ /tmp/diff_list2.$$
2417
}
2518

2619
# Gets if item exists in list

0 commit comments

Comments
 (0)