@@ -219,6 +219,22 @@ jq <<<"$sources" --argjson pins "$externalPinsJson" '
219
219
# https://unix.stackexchange.com/a/738744/153467
220
220
reduce .[] as $a ([]; if IN(.[]; $a) then . else . += [$a] end)
221
221
;
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
+ ;
222
238
reduce .[] as $in ({};
223
239
.[$in.sourceId] |=
224
240
if . == null then
@@ -254,15 +270,26 @@ jq <<<"$sources" --argjson pins "$externalPinsJson" '
254
270
# TODO a lot of this could be removed/parsed during the above reduce, since it has to parse things in build order anyhow
255
271
# TODO actually, instead, this bit should be a totally separate script so the use case of "combine sources.json files together" works better 👀
256
272
| (
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)
264
290
)
265
- )
291
+ ]
292
+ | from_entries
266
293
) as $tagArches
267
294
| map_values(
268
295
.arches |= with_entries(
0 commit comments