Open
Description
See #2679.
Tiny example to see what reduction compiles into:
jq --debug-dump-disasm -cn '[0,1,2,3,4]|reduce .[] as $i (0; if $i > . then $i else . end)'
0000 TOP
0001 LOADK [0,1,2,3,4]
0003 DUP
0004 LOADK 0 <-------- initial reduction state
0006 STOREV $reduce:0 <-------- store the initial reduction state
0009 FORK 0045 <-------- begin execution of the update body at the next instruction; later go to 045 to end the reduction
0011 DUPN
0012 EACH <-------- .[]
0013 STOREV $i:1 <-------- as $i
0016 LOADVN $reduce:0 <-------- put the reduction state at the top of the stack
0019 DUP <-------- begin update expression
0020 SUBEXP_BEGIN <-------- conditional start
0021 DUP
0022 SUBEXP_BEGIN
0023 LOADV $i:1
0026 SUBEXP_END
0027 CALL_BUILTIN _greater
0030 SUBEXP_END <-------- conditional end
0031 POP
0032 JUMP_F 0040
0034 POP
0035 LOADV $i:1
0038 JUMP 0041
0040 POP
0041 STOREV $reduce:0 <-------- reduction update expression end
0044 BACKTRACK <-------- reduction update expression end
0045 LOADVN $reduce:0 <-------- reduction end
0048 RET <-------- end of the whole thing
4
That's a lot of bytecodes, and that might have something to do with it, but we need to profile this with a larger example and see where the slowness is.