function pointer
I need to have some kind of array of function pointer, which can be pretty big, how to encapsulate a managed Function Pointer like a Delegate, with support of arguments by ref ?
15 Replies
You can create a delegate?
Something like this should work
delegate would be way to expensive for my case, I don't need any dynamic resolution, and must ensure that the function is static 😅
I don't need an array to all function at the same time, but call a function based on ID
delegate
is just a way to basically alias the type of a Func
or Action
, whether you assign a lambda to it, or an existing method, is up to you
As long as it matches the signature
It's also the way to handle function pointers in C#
Well, Func<>
, Action<>
and delegate
aredelegate support references on the parameter, but they are way bigger than a function pointer
(I will have something like 150k of them in memory...)
do you know its too big, or do you think it will be too big?
I believe 24k set of at least 10 pointers is too big 🤔
That's something like 240K of memory. That's nothing
8 times this, because pointers use 8 bytes, and delegate use much more
They don't use much more
And that's still an extremely small amount of memory
Measure how much memory you actually end up using, and then decide if that's a problem
Don't just micro-optimize right out of the gate
Angius
REPL Result: Success
Console Output
Quoted by
<@85903769203642368> from #bot-spam (click here)
Compile: 449.975ms | Execution: 23.616ms | React with ❌ to remove this embed.
You're still looking at kilobytes
A megabyte and change, at most
I lose control over data used by the callback 🤨
What?
Lambda and méthods can use data other than the parameters
Ok?
You're not really explaining why this is a bad thing
Indeed, a delegate can have a closure. But one of the following will be true:
1. The delegate didn't actually have a closure, so you're paying one extra pointer's worth of space to track that nothing is there.
2. The delegate did need a closure, and you are either storing that extra information somewhere else, or the function pointer doesn't work
I would highly, highly suggest just seeing how much memory you're using, and decide at that point whether it's too much