From edbc1da8093f55f8e7e68f29e93abcf2e19c5aa0 Mon Sep 17 00:00:00 2001 From: itchyny Date: Tue, 17 Jun 2025 15:29:46 +0900 Subject: [PATCH] Revert "fix: reduce/foreach state variable should not be reset each iteration" (#3349) This reverts 96e8d893c10ed2f7656ccb8cfa39a9a291663a7e. The change introduced a serious performance regression as reported in #3345. --- src/compile.c | 4 ++-- tests/jq.test | 8 -------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/compile.c b/src/compile.c index 43e04cd5..f0be1a5f 100644 --- a/src/compile.c +++ b/src/compile.c @@ -831,7 +831,7 @@ block gen_reduce(block source, block matcher, block init, block body) { block loop = BLOCK(gen_op_simple(DUPN), source, bind_alternation_matchers(matcher, - BLOCK(gen_op_bound(LOADV, res_var), + BLOCK(gen_op_bound(LOADVN, res_var), body, gen_op_bound(STOREV, res_var))), gen_op_simple(BACKTRACK)); @@ -855,7 +855,7 @@ block gen_foreach(block source, block matcher, block init, block update, block e // in the body to see bind_alternation_matchers(matcher, // load the loop state variable - BLOCK(gen_op_bound(LOADV, state_var), + BLOCK(gen_op_bound(LOADVN, state_var), // generate updated state update, // save the updated state for value extraction diff --git a/tests/jq.test b/tests/jq.test index bf2311e7..55ae09b7 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -358,10 +358,6 @@ null [1,2,3] [1,3,6] -[foreach range(5) as $x (0; .+$x | select($x!=2); [$x,.])] -null -[[0,0],[1,1],[3,4],[4,8]] - [limit(3; .[])] [11,22,33,44,55,66,77,88,99] [11,22,33] @@ -920,10 +916,6 @@ reduce . as $n (.; .) null null -reduce range(5) as $x (0; .+$x | select($x!=2)) -null -8 - # Destructuring . as {$a, b: [$c, {$d}]} | [$a, $c, $d] {"a":1, "b":[2,{"d":3}]}