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
TypedCallable provides a wrapper for callable objects, with the following benefits:
1. Enforced type-stability (for concrete AT/RT types)
2. Fast calling convention (frequently < 10 ns / call)
3. Normal Julia dispatch semantics (sees new Methods, etc.) + invoke_latest
4. Pre-compilation support (including `--trim` compatibility)
It can be used like this:
```julia
callbacks = @TypedCallable{(::Int,::Int)->Bool}[]
register_callback!(callbacks, f::F) where {F<:Function} =
push!(callbacks, @TypedCallable f(::Int,::Int)::Bool)
register_callback!(callbacks, (x,y)->(x == y))
register_callback!(callbacks, (x,y)->(x != y))
@Btime callbacks[rand(1:2)](1,1)
```
This is very similar to the existing `FunctionWrappers.jl`, but there
are a few key differences:
- Better type support: TypedCallable supports the full range of Julia
types (incl. Varargs), and it has access to all of Julia's "internal"
calling conventions so calls are fast (and allocation-free) for a
wider range of input types
- Improved dispatch handling: The `@cfunction` functionality used by
FunctionWrappers has several dispatch bugs, which cause wrappers to
occasionally not see new Methods. These bugs are fixed (or soon to
be fixed) for TypedCallable.
- Pre-compilation support including for `juliac` / `--trim` (#55047)
Many of the improvements here are actually thanks to the `OpaqueClosure`
introduced by @Keno - This type just builds on top of OpaqueClosure to
provide an interface with Julia's usual dispatch semantics.
0 commit comments