Skip to content

[RFC] Type stable wrapper for arbitrary callables #13984

@yuyichao

Description

@yuyichao

The idea is to have an equivalent of function pointer in C or std::function in C++11. This could be useful to avoid over specialization (Ref #13412 (comment)) and also for implementing mutable callback list/table. In these cases, it is acceptable that the callback function is not/cannot be inlined to the caller but it is also desired to avoid necessary boxing or runtime type check/dispatch.

One way to implement something like this is to use cfunction. I've hacked together a proof of concept implementation here. It is very non-generic and is not ideal due to the limitations of cfunction (no vararg, no way to bypass type checks, for example). Nevertheless, the benchmark comparing calling two functors stored in a Union array and an array with this wrapper shows that calling with the wrapper is ~40 times faster and avoids the necessary boxing of the arguments and results.

A few features that I think should be included:

  • Allow non-leaf types as arguments and results (not sure about Vararg)
  • Allow ignoring the results
  • Allow conversion/type assertion on the result if type inference cannot figure out the return type of the function (instead of erroring out like cfunction)

@JeffBezanson Is there anything related that is planed / implemented for #13412 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureIndicates new feature / enhancement requestsperformanceMust go fasterspeculativeWhether the change will be implemented is speculative

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions