Bambosa
Bambosa
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
these were added for AOT I think so I assume these are compile time
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
especially when they probably work with source generators
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
a shame the UnmanagedCallersAttribute is sealed, would have liked to just have a CdeclAttribute to default the calling convention
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
yeah I think this is the route I will go down
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
some things to think about, thanks for the info guys
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
yeah I was thinking if I went down that route I would just have some lookup to get the function pointer for the given function
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
I guess I will have to use unsafe then as the overhead for the stub is too big for the use case (interacting with an ECS)
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
yeah just checked
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
No description
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
also I thought the default calling convention for unmanaged callers only was cdecl? is it not?
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
yeah but there seems to be a pretty beefy overhead to using it
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
so I should pass it as delegate* unmanaged[Cdecl]<int, void>?
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
it currently works with this rust code and executes fine:
#[no_mangle]
pub extern "C" fn callback(number: i32, callback: unsafe extern "C" fn(i32)) {
(|n| unsafe { callback(n) })(number);
}
#[no_mangle]
pub extern "C" fn callback(number: i32, callback: unsafe extern "C" fn(i32)) {
(|n| unsafe { callback(n) })(number);
}
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
I just wish there was an easy way to get the same benefit without the consumer having to dip into unsafe code
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
with the delegate* managed<int, void> it only works against static methods which makes it safe to execute from native code
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
is there a way to take a managed delegate and convert it to a function pointer without the huge overheads?
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
Test.Callback(1234, &Callback); is perfect other than the fact the user consuming this static method has to be an unsafe context to give me a delegate* managed<int, void>
40 replies
CC#
Created by Bambosa on 9/16/2023 in #help
❔ How can I provide a safe zero cost abstraction over passing managed function pointers over FFI
I want to be able to pass a callback function from a safe managed context over FFI without these insane overheads, here is the benchmark I used:
[MemoryDiagnoser(false)]
public class Benchmarks
{
[Params(10, 100, 1000, 10000)] public int Iterations { get; set; }

[Benchmark]
public void CallbackSafe()
{
for (var i = 0; i < Iterations; i++)
Test.Callback(1234, Callback);
}

[Benchmark]
public void CallbackSafe2()
{
for (var i = 0; i < Iterations; i++)
Test.Callback2(1234, Callback);
}

[Benchmark]
public unsafe void CallbackUnsafe()
{
for (var i = 0; i < Iterations; i++)
Test.Callback(1234, &Callback);
}

private static void Callback(int i){ }
}
[MemoryDiagnoser(false)]
public class Benchmarks
{
[Params(10, 100, 1000, 10000)] public int Iterations { get; set; }

[Benchmark]
public void CallbackSafe()
{
for (var i = 0; i < Iterations; i++)
Test.Callback(1234, Callback);
}

[Benchmark]
public void CallbackSafe2()
{
for (var i = 0; i < Iterations; i++)
Test.Callback2(1234, Callback);
}

[Benchmark]
public unsafe void CallbackUnsafe()
{
for (var i = 0; i < Iterations; i++)
Test.Callback(1234, &Callback);
}

private static void Callback(int i){ }
}
40 replies