Skip to content

Commit 663ded4

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

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

sources.sh

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ 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+
def meld($a; $b):
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
225+
if ($a | type) == "object" and ($b | type) == "object" then
226+
reduce ([$a, $b] | add | keys_unsorted[]) as $k ({};
227+
.[$k] = meld( $a[$k]; $b[$k])
228+
)
229+
elif ($a | type) == "array" and ($b | type) == "array" then
230+
$a + $b
231+
| unique_unsorted
232+
elif $b == null then
233+
$a
234+
else
235+
$b
236+
end
237+
;
222238
reduce .[] as $in ({};
223239
.[$in.sourceId] |=
224240
if . == null then
@@ -254,15 +270,26 @@ jq <<<"$sources" --argjson pins "$externalPinsJson" '
254270
# TODO a lot of this could be removed/parsed during the above reduce, since it has to parse things in build order anyhow
255271
# TODO actually, instead, this bit should be a totally separate script so the use case of "combine sources.json files together" works better 👀
256272
| (
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-
)
273+
# creating a lookup of .[tag][arch] to a list of sourceIds
274+
[
275+
.[] as $e
276+
| ( $e.arches[] | .tags[], .archTags[] )
277+
| {
278+
key: .,
279+
value: {
280+
($e.arches | keys[]): [$e.sourceId]
281+
}
282+
}
283+
]
284+
# do not try to code golf this to one reduce without from_entries or group_by, that is very slow
285+
| group_by(.key)
286+
| [
287+
# many little reduces based on same key (group_by^), instead of one very big and expensive
288+
.[] | reduce .[] as $r ({};
289+
meld(.; $r)
264290
)
265-
)
291+
]
292+
| from_entries
266293
) as $tagArches
267294
| map_values(
268295
.arches |= with_entries(

0 commit comments

Comments
 (0)