You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is unfortunate since calling a function that returns any arguments is likely the common case. The aforementioned allocations cannot be eliminated because the current API Value.Call pretty much requires that the implementation be responsible for allocating the return arguments.
I propose a new CallWith (or any other suggested name) method with the modified signature:
func (vValue) CallWith(out, in []reflect.Value)
where:
the in argument is identical to that for Call
the out argument must be a slice where len(out) == v.NumOut().
If out[n] is a valid, settable value where v.Out(n).AssignableTo(out[n].Type()), then CallWith uses the existing value to store the output (with Value.Set).
Otherwise, CallWith allocates new underlying storage for a new reflect.Value and stores that into out[n].
With this API, I believe it is theoretically possible to have an allocation-free version of Value.Call.
The description above only proposes CallWith as an optimized version of Call. You can imagine a CallSliceWith as an optimized version of CallSlice.
At present,
Value.Call
for any function with any return arguments is slow because it needs to:[]Value
for the return Values themselvesThis is unfortunate since calling a function that returns any arguments is likely the common case. The aforementioned allocations cannot be eliminated because the current API
Value.Call
pretty much requires that the implementation be responsible for allocating the return arguments.I propose a new
CallWith
(or any other suggested name) method with the modified signature:where:
in
argument is identical to that forCall
out
argument must be a slice wherelen(out) == v.NumOut()
.out[n]
is a valid, settable value wherev.Out(n).AssignableTo(out[n].Type())
, thenCallWith
uses the existing value to store the output (withValue.Set
).CallWith
allocates new underlying storage for a newreflect.Value
and stores that intoout[n]
.With this API, I believe it is theoretically possible to have an allocation-free version of
Value.Call
.The description above only proposes
CallWith
as an optimized version ofCall
. You can imagine aCallSliceWith
as an optimized version ofCallSlice
.\cc @mvdan @rogpeppe
The text was updated successfully, but these errors were encountered: