Skip to content
Myr

Revert "reduce: handle empty updates (fix #1313)"

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This reverts commit e24af3c78e78a3aab05a2800d825d56f1d842b1b. While the semantics are desirable, there is no way to implement them efficiently. The reason is that in order to handle backtracking (empty) from the state update expression, we have to retain a reference to the reduction state value in order to restore it upon backtracking. Retaining a reference to the reduction state kills performance by causing lots of additional memory allocations and garbage because the input to the update expression will always have at least two references, thus no changes to it can be done in-place, and all changes end up being CoW changes. Avoiding this is the very reason for the LOADVN instruction (leaving `null` in the variable loaded from).

authored by Nicolas Williams · committed by Nicolas Williams · parent b5560d8 · Browse files · 4 files+4 added−52 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+4 added−52 removed

  • tests:+0 added−5 removed
  • other:+4 added−47 removed

4 files+4 added−52 removed

modified:docs/content/3.manual/manual.yml-8 removed
2594 unmodified lines
2595 0 | (3 as $item | . + $item) |2596 (2 as $item | . + $item) |2597 (1 as $item | . + $item)25982599 If the reduction update expression outputs `empty` (that is,2600 no values), then the reduction state is left as-is. For2601 example, `reduce range(4) as $n ({}; if .==2 then empty else2602 .[$n|tostring] |= $n)` will produce `{"0":0,"1":1,"3":3}`.26032599 examples:2600 - program: 'reduce .[] as $item (0; . + $item)'2601 input: '[10,2,5,3]'2602 output: ['20']2608 - program: 'reduce .[] as $n ([]; if $n%2==0 then empty else . + [$n] end)'2609 input: '[0,1,2,3,4,5]'2610 output: ['[1,3,5]']26032604 - title: "`isempty(exp)`"2605 body: |2606
628 unmodified lines
modified:src/compile.c+1 added-21 removed
821 unmodified lines
822}823824block gen_reduce(block source, block matcher, block init, block body) {825 block res_var = gen_op_var_fresh(STOREV, "reduce");826 block update_var = gen_op_bound(STOREV, res_var);827 block jmp = gen_op_targetlater(JUMP);828 if (body.last == NULL) {829 inst_set_target(jmp, jmp);830 } else {831 inst_set_target(jmp, body);832 }826 block loop = BLOCK(gen_op_simple(DUPN),827 source,828 bind_alternation_matchers(matcher,829 BLOCK(gen_op_bound(LOADVN, res_var),837 /*838 * We fork to the body, jump to839 * the STOREV. This means that840 * if body produces no results841 * (i.e., it just does empty)842 * then we keep the current843 * reduction state as-is.844 *845 * To break out of a846 * reduction... use break.847 */848 gen_op_target(FORK, jmp),849 jmp,830 body,851 update_var)),831 gen_op_bound(STOREV, res_var))),832 gen_op_simple(BACKTRACK));833 return BLOCK(gen_op_simple(DUP),834 init,835 res_var,
557 unmodified lines
modified:tests/jq.test-5 removed
703 unmodified lines
704reduce [[1,2,10], [3,4,10]][] as [$i,$j] (0; . + $i * $j)705null70614707708# Test fix for #1313 (reduce should handle empty updates)709reduce range(5) as $n ([]; select($n%2 == 1) | . + [$n])710null711[1,3]712708# This, while useless, should still compile.709reduce . as $n (.; .)710null711null
707 unmodified lines
modified:jq.1.prebuilt+3 added-18 removed
1.\" generated with Ronn/v0.7.32.\" http://github.com/rtomayko/ronn/tree/0.7.33.4.TH "JQ" "1" "May 2017" "" ""4.TH "JQ" "1" "December 2017" "" ""5.6.SH "NAME"7\fBjq\fR \- Command\-line JSON processor8.
2090 unmodified lines
2099.P2100The format strings for \fBstrptime\fR and \fBstrftime\fR are described in typical C library documentation\. The format string for ISO 8601 datetime is \fB"%Y\-%m\-%dT%H:%M:%SZ"\fR\.2101.2102.P2103jq may not support some or all of this date functionality on some systems\.2103jq may not support some or all of this date functionality on some systems\. In particular, the \fB%u\fR and \fB%j\fR specifiers for \fBstrptime(fmt)\fR are not supported on macOS\.2104.2105.IP "" 42106.2107.nf
269 unmodified lines
2377.2378.P2379because no label \fB$out\fR is visible\.2380.2381.SS "Error Suppresion / Optional Operator: ?"2381.SS "Error Suppression / Optional Operator: ?"2382The \fB?\fR operator, used as \fBEXP?\fR, is shorthand for \fBtry EXP\fR\.2383.2384.IP "" 42385.
474 unmodified lines
286028610 | (3 as $item | \. + $item) |2862 (2 as $item | \. + $item) |2863 (1 as $item | \. + $item)2864.2865.fi2866.2867.IP "" 02868.2869.P2870If the reduction update expression outputs \fBempty\fR (that is, no values), then the reduction state is left as\-is\. For example, \fBreduce range(4) as $n ({}; if \.==2 then empty else \.[$n|tostring] |= $n)\fR will produce \fB{"0":0,"1":1,"3":3}\fR\.2871.2872.IP "" 42873.2874.nf28642865jq \'reduce \.[] as $item (0; \. + $item)\'2866 [10,2,5,3]2867=> 2028792880jq \'reduce \.[] as $n ([]; if $n%2==0 then empty else \. + [$n] end)\'2881 [0,1,2,3,4,5]2882=> [1,3,5]2868.2869.fi2870.2871.IP "" 0
588 unmodified lines