1
0
mirror of https://github.com/stedolan/jq.git synced 2024-05-11 05:55:39 +00:00

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

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).
This commit is contained in:
Nicolas Williams
2017-12-11 11:20:16 -06:00
parent b5560d8420
commit 9a4576e756
4 changed files with 4 additions and 52 deletions

View File

@ -2596,18 +2596,10 @@ sections:
(2 as $item | . + $item) |
(1 as $item | . + $item)
If the reduction update expression outputs `empty` (that is,
no values), then the reduction state is left as-is. For
example, `reduce range(4) as $n ({}; if .==2 then empty else
.[$n|tostring] |= $n)` will produce `{"0":0,"1":1,"3":3}`.
examples:
- program: 'reduce .[] as $item (0; . + $item)'
input: '[10,2,5,3]'
output: ['20']
- program: 'reduce .[] as $n ([]; if $n%2==0 then empty else . + [$n] end)'
input: '[0,1,2,3,4,5]'
output: ['[1,3,5]']
- title: "`isempty(exp)`"
body: |

View File

@ -1,7 +1,7 @@
.\" generated with Ronn/v0.7.3
.\" http://github.com/rtomayko/ronn/tree/0.7.3
.
.TH "JQ" "1" "May 2017" "" ""
.TH "JQ" "1" "December 2017" "" ""
.
.SH "NAME"
\fBjq\fR \- Command\-line JSON processor
@ -2100,7 +2100,7 @@ The \fBstrftime(fmt)\fR builtin formats a time (GMT) with the given format\. The
The 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\.
.
.P
jq may not support some or all of this date functionality on some systems\.
jq 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\.
.
.IP "" 4
.
@ -2378,7 +2378,7 @@ break $out
.P
because no label \fB$out\fR is visible\.
.
.SS "Error Suppresion / Optional Operator: ?"
.SS "Error Suppression / Optional Operator: ?"
The \fB?\fR operator, used as \fBEXP?\fR, is shorthand for \fBtry EXP\fR\.
.
.IP "" 4
@ -2861,25 +2861,10 @@ For each result that \fB\.[]\fR produces, \fB\. + $item\fR is run to accumulate
0 | (3 as $item | \. + $item) |
(2 as $item | \. + $item) |
(1 as $item | \. + $item)
.
.fi
.
.IP "" 0
.
.P
If 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\.
.
.IP "" 4
.
.nf
jq \'reduce \.[] as $item (0; \. + $item)\'
[10,2,5,3]
=> 20
jq \'reduce \.[] as $n ([]; if $n%2==0 then empty else \. + [$n] end)\'
[0,1,2,3,4,5]
=> [1,3,5]
.
.fi
.

View File

@ -823,32 +823,12 @@ static block bind_alternation_matchers(block matchers, block body) {
block gen_reduce(block source, block matcher, block init, block body) {
block res_var = gen_op_var_fresh(STOREV, "reduce");
block update_var = gen_op_bound(STOREV, res_var);
block jmp = gen_op_targetlater(JUMP);
if (body.last == NULL) {
inst_set_target(jmp, jmp);
} else {
inst_set_target(jmp, body);
}
block loop = BLOCK(gen_op_simple(DUPN),
source,
bind_alternation_matchers(matcher,
BLOCK(gen_op_bound(LOADVN, res_var),
/*
* We fork to the body, jump to
* the STOREV. This means that
* if body produces no results
* (i.e., it just does empty)
* then we keep the current
* reduction state as-is.
*
* To break out of a
* reduction... use break.
*/
gen_op_target(FORK, jmp),
jmp,
body,
update_var)),
gen_op_bound(STOREV, res_var))),
gen_op_simple(BACKTRACK));
return BLOCK(gen_op_simple(DUP),
init,

View File

@ -705,11 +705,6 @@ reduce [[1,2,10], [3,4,10]][] as [$i,$j] (0; . + $i * $j)
null
14
# Test fix for #1313 (reduce should handle empty updates)
reduce range(5) as $n ([]; select($n%2 == 1) | . + [$n])
null
[1,3]
# This, while useless, should still compile.
reduce . as $n (.; .)
null