Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Type stable wrapper for arbitrary callables #13984

Open
yuyichao opened this issue Nov 14, 2015 · 4 comments · May be fixed by #55111
Open

[RFC] Type stable wrapper for arbitrary callables #13984

yuyichao opened this issue Nov 14, 2015 · 4 comments · May be fixed by #55111
Labels
performance Must go faster speculative Whether the change will be implemented is speculative

Comments

@yuyichao
Copy link
Contributor

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 ?

@ExpandingMan
Copy link
Contributor

I'd like to advocate for this being in stdlib sooner rather than later, my arguments are as follows:

  • The functionality offered by FunctionWrappers is not otherwise available in the language. As far as I know there are no shortcuts, cheats or hacks to get this kind of efficient unboxing of functions by "straightforward" means.
  • Very few people would have the expertise to write or maintain this (certainly not me), so if you decide that you need it and are not aware of the package you are probably pretty screwed. If the package doesn't get updated for a new version of Julia you are likewise screwed.

I know it has been stated repeatedly that this is experimental, but it certainly already offers quite a bit of functionality which, again, is otherwise unobtainable.

Any interest in doing this? Any ideas what else would be required? (I wish I could offer to be more helpful, but this is well beyond my expertise.)

@EricForgy
Copy link
Contributor

I recently had the need for this and totally support the idea. Cool stuff 👍

@Moelf
Copy link
Contributor

Moelf commented Feb 19, 2025

Thanks Oscar Smith for pointing out #55111 is implementing this

@vtjnash vtjnash closed this as completed Feb 19, 2025
@oscardssmith
Copy link
Member

This isn't completed. #55111 isn't merged.

@oscardssmith oscardssmith reopened this Feb 19, 2025
@vtjnash vtjnash linked a pull request Feb 19, 2025 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster speculative Whether the change will be implemented is speculative
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants