perf: update the accumulated value of a reduce expression in place
_assign and _modify create an allocator on every call, so the value they are given is never one that allocator created, and updateObject and updateArrayIndex always take the copying branch. Accumulating into an object or an array with a reduce expression therefore copies the whole accumulated value on every iteration, which is quadratic in the number of elements. The query of #241, [range(20000)] | reduce .[] as $item ({}; .[($item|tostring)] = {}) took between 10 and 21 seconds here over three runs, against 0.03 seconds for jq. Create the allocator once per reduce expression and pass it to the assignment as a third argument, so that the accumulated value is allocated on the first iteration and modified in place afterwards. The query above now takes 0.03 seconds, and the memory allocated by an accumulating reduction grows linearly instead of quadratically: 2.1 MB instead of 545 MB for 4000 elements. This is only correct while the accumulated value, and the values the allocator created inside it, are reachable through the reduction alone. Query#inPlaceUpdate therefore requires the update query to be a single assignment, and its right-hand side to neither observe the input value, which would let the assigned value alias the value it is assigned into, nor yield more than one value, which would run the assignment again against a value it already modified. Everything else keeps copying. A foreach expression is left alone, because it yields every intermediate accumulated value. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
c5274c6af4
commit
70b3f7d698
4 files changed
+493
-43
No files matched your search
+152
-43
@@ -397,7 +397,7 @@ func (c *compiler) compileQuery(e *Query) error {
|
||||
return c.compileAlt(e.Left, e.Right)
|
||||
case OpAssign, OpModify, OpUpdateAdd, OpUpdateSub,
|
||||
OpUpdateMul, OpUpdateDiv, OpUpdateMod, OpUpdateAlt:
|
||||
return c.compileQueryUpdate(e.Left, e.Right, e.Op)
|
||||
return c.compileQueryUpdate(e.Left, e.Right, e.Op, nil)
|
||||
case OpOr:
|
||||
return c.compileIf(
|
||||
&If{
|
||||
@@ -471,7 +471,11 @@ func (c *compiler) compileAlt(l, r *Query) error {
|
||||
return c.compileQuery(r)
|
||||
}
|
||||
|
||||
func (c *compiler) compileQueryUpdate(l, r *Query, op Operator) error {
|
||||
// compileQueryUpdate appends the compiled code for an assignment operator. The
|
||||
// alloc query is nil unless the assignment may update the input value in
|
||||
// place, in which case it yields the allocator of the enclosing reduce
|
||||
// expression; see compiler#compileReduce.
|
||||
func (c *compiler) compileQueryUpdate(l, r *Query, op Operator, alloc *Query) error {
|
||||
switch op {
|
||||
case OpAssign:
|
||||
// optimize assignment operator with constant indexing and slicing
|
||||
@@ -491,12 +495,7 @@ func (c *compiler) compileQueryUpdate(l, r *Query, op Operator) error {
|
||||
}
|
||||
fallthrough
|
||||
case OpModify:
|
||||
return c.compileFunc(
|
||||
&Func{
|
||||
Name: op.getFunc(),
|
||||
Args: []*Query{l, r},
|
||||
},
|
||||
)
|
||||
return c.compileUpdateFunc(op.getFunc(), []*Query{l, r}, alloc)
|
||||
default:
|
||||
name := "$%0"
|
||||
c.append(&code{op: opdup})
|
||||
@@ -504,27 +503,59 @@ func (c *compiler) compileQueryUpdate(l, r *Query, op Operator) error {
|
||||
return err
|
||||
}
|
||||
c.append(&code{op: opstore, v: c.pushVariable(name)})
|
||||
return c.compileFunc(
|
||||
&Func{
|
||||
Name: "_modify",
|
||||
Args: []*Query{
|
||||
l,
|
||||
{Term: &Term{
|
||||
Type: TermTypeFunc,
|
||||
Func: &Func{
|
||||
Name: op.getFunc(),
|
||||
Args: []*Query{
|
||||
{Term: &Term{Type: TermTypeIdentity}},
|
||||
{Term: &Term{Type: TermTypeFunc, Func: &Func{Name: name}}},
|
||||
},
|
||||
return c.compileUpdateFunc(
|
||||
"_modify",
|
||||
[]*Query{
|
||||
l,
|
||||
{Term: &Term{
|
||||
Type: TermTypeFunc,
|
||||
Func: &Func{
|
||||
Name: op.getFunc(),
|
||||
Args: []*Query{
|
||||
{Term: &Term{Type: TermTypeIdentity}},
|
||||
{Term: &Term{Type: TermTypeFunc, Func: &Func{Name: name}}},
|
||||
},
|
||||
}},
|
||||
},
|
||||
},
|
||||
}},
|
||||
},
|
||||
alloc,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
// Appended to the names of the _assign and _modify variants taking an
|
||||
// allocator as an additional argument. The resulting name is not a valid
|
||||
// function name, so that a query cannot call such a variant directly with
|
||||
// an arbitrary value in place of the allocator.
|
||||
allocatorArgSuffix = "/3"
|
||||
|
||||
// The name of the variable holding the allocator of a reduce expression.
|
||||
// It is not a valid variable name, just like the variable of the operand
|
||||
// of an update-assignment operator.
|
||||
allocatorName = "$%allocator"
|
||||
)
|
||||
|
||||
// compileUpdateFunc appends a call to _assign or _modify, or to the variant
|
||||
// taking the allocator to update the input value with as its third argument.
|
||||
func (c *compiler) compileUpdateFunc(name string, args []*Query, alloc *Query) error {
|
||||
if alloc == nil {
|
||||
return c.compileFunc(&Func{Name: name, Args: args})
|
||||
}
|
||||
args = append(args, alloc)
|
||||
fn := c.lookupBuiltin(name+allocatorArgSuffix, len(args))
|
||||
if fn == nil {
|
||||
switch name {
|
||||
case "_assign":
|
||||
c.compileAssign(true)
|
||||
case "_modify":
|
||||
c.compileModify(true)
|
||||
}
|
||||
fn = c.lookupBuiltin(name+allocatorArgSuffix, len(args))
|
||||
}
|
||||
return c.compileCallPc(fn, args)
|
||||
}
|
||||
|
||||
func (c *compiler) compileBind(l, r *Query, patterns []*Pattern) error {
|
||||
defer c.newScopeDepth()()
|
||||
c.append(&code{op: opdup})
|
||||
@@ -720,6 +751,25 @@ func (c *compiler) compileReduce(e *Reduce) error {
|
||||
}
|
||||
f()
|
||||
c.append(&code{op: opstore, v: v})
|
||||
// The accumulated value is passed to the update query and replaced by its
|
||||
// result, so an assignment forming the whole update query is the only
|
||||
// thing that observes it. Such an assignment may modify the accumulated
|
||||
// value in place, instead of copying it on every iteration, as long as it
|
||||
// keeps track of the values it allocated; see Query#inPlaceUpdate. The
|
||||
// allocator is created once, outside the loop, and lives until the
|
||||
// reduction finishes.
|
||||
l, r, op, inPlace := e.Update.inPlaceUpdate()
|
||||
var alloc *Query
|
||||
if inPlace {
|
||||
c.appends(
|
||||
&code{op: oppush, v: nil},
|
||||
&code{op: opcall, v: [3]any{funcAllocator, 0, "_allocator"}},
|
||||
&code{op: opstore, v: c.pushVariable(allocatorName)},
|
||||
)
|
||||
alloc = &Query{Term: &Term{
|
||||
Type: TermTypeFunc, Func: &Func{Name: allocatorName},
|
||||
}}
|
||||
}
|
||||
setfork := c.lazy(func() *code {
|
||||
return &code{op: opfork, v: len(c.codes)}
|
||||
})
|
||||
@@ -731,7 +781,13 @@ func (c *compiler) compileReduce(e *Reduce) error {
|
||||
}
|
||||
c.append(&code{op: opload, v: v})
|
||||
f = c.newScopeDepth()
|
||||
if err := c.compileQuery(e.Update); err != nil {
|
||||
var err error
|
||||
if inPlace {
|
||||
err = c.compileQueryUpdate(l, r, op, alloc)
|
||||
} else {
|
||||
err = c.compileQuery(e.Update)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f()
|
||||
@@ -936,9 +992,9 @@ func (c *compiler) compileFunc(e *Func) error {
|
||||
if !compiled {
|
||||
switch e.Name {
|
||||
case "_assign":
|
||||
c.compileAssign()
|
||||
c.compileAssign(false)
|
||||
case "_modify":
|
||||
c.compileModify()
|
||||
c.compileModify(false)
|
||||
case "_last":
|
||||
c.compileLast()
|
||||
}
|
||||
@@ -1034,29 +1090,56 @@ func (c *compiler) compileFunc(e *Func) error {
|
||||
//
|
||||
// To overcome the difficulty of reducing allocations on `setpath`, we use the
|
||||
// `allocator` type and track the allocated addresses during the reduction.
|
||||
func (c *compiler) compileAssign() {
|
||||
defer c.appendBuiltin("_assign", 2)()
|
||||
//
|
||||
// The shared variant takes the allocator of the enclosing reduce expression as
|
||||
// its third argument instead of creating a new one, so that the value being
|
||||
// assigned to is modified in place on every iteration but the first; see
|
||||
// compiler#compileReduce.
|
||||
func (c *compiler) compileAssign(shared bool) {
|
||||
name, argcnt := "_assign", 2
|
||||
if shared {
|
||||
name, argcnt = name+allocatorArgSuffix, 3
|
||||
}
|
||||
defer c.appendBuiltin(name, argcnt)()
|
||||
scope := c.newScope()
|
||||
v, p := [2]int{scope.id, 0}, [2]int{scope.id, 1}
|
||||
x, a := [2]int{scope.id, 2}, [2]int{scope.id, 3}
|
||||
// Cannot reuse v, p due to backtracking in x.
|
||||
w, q := [2]int{scope.id, 4}, [2]int{scope.id, 5}
|
||||
c.appends(
|
||||
&code{op: opscope, v: [3]int{scope.id, 6, 2}},
|
||||
&code{op: opscope, v: [3]int{scope.id, 6, argcnt}},
|
||||
&code{op: opstore, v: v}, // def _assign(p; $x):
|
||||
&code{op: opstore, v: p},
|
||||
&code{op: opstore, v: x},
|
||||
)
|
||||
if shared {
|
||||
c.append(&code{op: opstore, v: a}) // the allocator argument
|
||||
}
|
||||
c.appends(
|
||||
&code{op: opload, v: v},
|
||||
&code{op: opexpbegin},
|
||||
&code{op: opload, v: x},
|
||||
&code{op: opcallpc},
|
||||
&code{op: opstore, v: x},
|
||||
&code{op: opexpend},
|
||||
&code{op: oppush, v: nil},
|
||||
&code{op: opcall, v: [3]any{funcAllocator, 0, "_allocator"}},
|
||||
&code{op: opstore, v: a},
|
||||
)
|
||||
if shared {
|
||||
c.appends(
|
||||
&code{op: opload, v: v},
|
||||
&code{op: opload, v: a},
|
||||
&code{op: opcallpc},
|
||||
&code{op: opstore, v: a},
|
||||
)
|
||||
} else {
|
||||
c.appends(
|
||||
&code{op: oppush, v: nil},
|
||||
&code{op: opcall, v: [3]any{funcAllocator, 0, "_allocator"}},
|
||||
&code{op: opstore, v: a},
|
||||
)
|
||||
}
|
||||
c.appends(
|
||||
&code{op: opload, v: v},
|
||||
&code{op: opfork, v: len(c.codes) + 30}, // reduce [L1]
|
||||
&code{op: opfork, v: len(c.codes) + 17}, // reduce [L1]
|
||||
&code{op: opdup},
|
||||
&code{op: opstore, v: w},
|
||||
&code{op: oppathbegin}, // path(p)
|
||||
@@ -1080,24 +1163,50 @@ func (c *compiler) compileAssign() {
|
||||
|
||||
// Appends the compiled code for the update-assignment operator (`|=`) to
|
||||
// maximize performance. We use the `allocator` type, just like `_assign/2`.
|
||||
func (c *compiler) compileModify() {
|
||||
defer c.appendBuiltin("_modify", 2)()
|
||||
//
|
||||
// The shared variant takes the allocator of the enclosing reduce expression as
|
||||
// its third argument instead of creating a new one; see
|
||||
// compiler#compileReduce and compiler#compileAssign.
|
||||
func (c *compiler) compileModify(shared bool) {
|
||||
name, argcnt := "_modify", 2
|
||||
if shared {
|
||||
name, argcnt = name+allocatorArgSuffix, 3
|
||||
}
|
||||
defer c.appendBuiltin(name, argcnt)()
|
||||
scope := c.newScope()
|
||||
v, p := [2]int{scope.id, 0}, [2]int{scope.id, 1}
|
||||
f, d := [2]int{scope.id, 2}, [2]int{scope.id, 3}
|
||||
a, l := [2]int{scope.id, 4}, [2]int{scope.id, 5}
|
||||
c.appends(
|
||||
&code{op: opscope, v: [3]int{scope.id, 6, 2}},
|
||||
&code{op: opscope, v: [3]int{scope.id, 6, argcnt}},
|
||||
&code{op: opstore, v: v}, // def _modify(p; f):
|
||||
&code{op: opstore, v: p},
|
||||
&code{op: opstore, v: f},
|
||||
)
|
||||
if shared {
|
||||
c.append(&code{op: opstore, v: a}) // the allocator argument
|
||||
}
|
||||
c.appends(
|
||||
&code{op: oppush, v: []any{}},
|
||||
&code{op: opstore, v: d},
|
||||
&code{op: oppush, v: nil},
|
||||
&code{op: opcall, v: [3]any{funcAllocator, 0, "_allocator"}},
|
||||
&code{op: opstore, v: a},
|
||||
)
|
||||
if shared {
|
||||
c.appends(
|
||||
&code{op: opload, v: v},
|
||||
&code{op: opload, v: a},
|
||||
&code{op: opcallpc},
|
||||
&code{op: opstore, v: a},
|
||||
)
|
||||
} else {
|
||||
c.appends(
|
||||
&code{op: oppush, v: nil},
|
||||
&code{op: opcall, v: [3]any{funcAllocator, 0, "_allocator"}},
|
||||
&code{op: opstore, v: a},
|
||||
)
|
||||
}
|
||||
c.appends(
|
||||
&code{op: opload, v: v},
|
||||
&code{op: opfork, v: len(c.codes) + 40}, // reduce [L1]
|
||||
&code{op: opfork, v: len(c.codes) + 31}, // reduce [L1]
|
||||
&code{op: oppathbegin}, // path(p)
|
||||
&code{op: opload, v: p},
|
||||
&code{op: opcallpc},
|
||||
@@ -1106,7 +1215,7 @@ func (c *compiler) compileModify() {
|
||||
&code{op: opstore, v: p}, // as $p (.;
|
||||
&code{op: opforklabel, v: l}, // label $l |
|
||||
&code{op: opload, v: v}, //
|
||||
&code{op: opfork, v: len(c.codes) + 37}, // [L2]
|
||||
&code{op: opfork, v: len(c.codes) + 28}, // [L2]
|
||||
&code{op: oppop}, // (getpath($p) |
|
||||
&code{op: opload, v: a},
|
||||
&code{op: opload, v: a},
|
||||
@@ -1120,8 +1229,8 @@ func (c *compiler) compileModify() {
|
||||
&code{op: opcall, v: [3]any{funcSetpathWithAllocator, 3, "_setpath"}},
|
||||
&code{op: opstore, v: v},
|
||||
&code{op: opload, v: v}, // ., break $l
|
||||
&code{op: opfork, v: len(c.codes) + 35}, // [L4]
|
||||
&code{op: opjump, v: len(c.codes) + 39}, // [L3]
|
||||
&code{op: opfork, v: len(c.codes) + 26}, // [L4]
|
||||
&code{op: opjump, v: len(c.codes) + 30}, // [L3]
|
||||
&code{op: opload, v: l}, // [L4]
|
||||
&code{op: opcall, v: [3]any{funcBreak(""), 0, "_break"}},
|
||||
&code{op: opload, v: p}, // append $p to $d [L2]
|
||||
|
||||
Reference in new issue
Block a user