Skip to content

Commit 403acbc

Browse files
committed
Speed up sources.sh even more
by improving slow jq section in sources.sh
1 parent 698b0ab commit 403acbc

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

sources.sh

+36-12
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,42 @@ jq <<<"$sources" --argjson pins "$externalPinsJson" '
219219
# https://unix.stackexchange.com/a/738744/153467
220220
reduce .[] as $a ([]; if IN(.[]; $a) then . else . += [$a] end)
221221
;
222-
reduce .[] as $in ({};
222+
def meld($o):
223+
# recursive merge of objects like "*", but also append lists (uniquely) instead of replace
224+
# https://stackoverflow.com/a/53666584, but with lists unique-ified and less work being done
225+
if type == "object" and ($o | type) == "object" then
226+
reduce ($o | keys_unsorted[]) as $k (.;
227+
.[$k] |= meld($o[$k])
228+
)
229+
elif type == "array" and ($o | type) == "array" then
230+
. + $o
231+
| unique_unsorted
232+
elif $o == null then
233+
.
234+
else
235+
$o
236+
end
237+
;
238+
(
239+
# creating a lookup of .[tag][arch] to a list of sourceIds
240+
[
241+
.[] as $s
242+
| ( $s.arches[] | .tags[], .archTags[] )
243+
| {
244+
key: .,
245+
# this happens pre-arches-merge, so it is only one arch
246+
value: { ($s.arches | keys[]): [$s.sourceId] },
247+
}
248+
]
249+
# do not try to code golf this to one reduce without from_entries or group_by; doing ".[xxx] |=" on the *full* unordered object gets orders of magnitude slower
250+
| group_by(.key)
251+
| [
252+
# many little reduces based on same key (group_by^), instead of one very big and expensive
253+
.[] | reduce .[] as $r ({}; meld($r))
254+
]
255+
| from_entries
256+
) as $tagArches
257+
| reduce .[] as $in ({};
223258
.[$in.sourceId] |=
224259
if . == null then
225260
$in
@@ -253,17 +288,6 @@ jq <<<"$sources" --argjson pins "$externalPinsJson" '
253288
)
254289
# TODO a lot of this could be removed/parsed during the above reduce, since it has to parse things in build order anyhow
255290
# TODO actually, instead, this bit should be a totally separate script so the use case of "combine sources.json files together" works better 👀
256-
| (
257-
# TODO make this faster, this reduce takes the longest time now
258-
reduce to_entries[] as $e ({};
259-
$e.key as $sourceId
260-
| .[ $e.value.arches[] | .tags[], .archTags[] ] |= (
261-
.[$e.value.arches | keys[]] |= (
262-
. + [$sourceId] | unique_unsorted
263-
)
264-
)
265-
)
266-
) as $tagArches
267291
| map_values(
268292
.arches |= with_entries(
269293
.key as $arch

0 commit comments

Comments
 (0)