Skip to content

proposal: reflect: add allocation-free variant of Value.Call #43732

Closed
@dsnet

Description

@dsnet

At present, Value.Call for any function with any return arguments is slow because it needs to:

  1. allocate a stack frame for the underlying return values
  2. allocate a []Value for the return Values themselves

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 (v Value) 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.

\cc @mvdan @rogpeppe

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions