-
Notifications
You must be signed in to change notification settings - Fork 24
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
Memory consumption and closures #40
Comments
Thank you! I'd come up with a similar "solution", except mine used a tiny TinyCC function to convert the closure to a pointer: $tcc->compile_string(q{
void *
identity(void *arg)
{
return arg;
}
});
my $identity = $tcc->get_symbol('identity');
my $function_pointer = $ffi->function($identity => ['ErrorReporter'] => 'opaque')->call($closure); (I'm also passing $function_pointer to an Inline::CPP-generated function, which Just Works). Using ->cast() would be much cleaner (and actually avoid the need for TinyCC entirely in my case), though. I think it would also be nice to be able to pass opaque pointers instead of closures to a function without changing the signature:
I'll have a look at the code to see how hard that would be to implement. Thank you again! |
I look forward to the PR :) |
It's at #41 (I've added documentation and a test case to the original pull request, but in essence it's a one-liner). Let me know if anything else needs changing. |
The doco and the test cases are essential, including it saves me from doing it. #41 should be included in |
Okay, I've gone ahead and implemented cached closures based on the FFI type as key. It seemed straightforward enough and passes the existing tests, but I've run into a new problem: FFI::Platypus::Closure actually modifies (blesses) its argument, so calling it twice with the same argument causes trouble: use FFI::Platypus;
sub c {
return 7;
}
my $ffi = FFI::Platypus->new;
my $c = \&c;
$ffi->closure($c);
$ffi->closure($c); The obvious solution is to make a FFI::Platypus::Closure an ordinary blessed hashref rather than a blessed coderef, and overload calling if it's really needed. Working on that now. |
This should be fixed in 0.30 on its way to CPAN. |
Reopening in the right place from here:
PerlFFI/FFI-Platypus-Lang-CPP#8
This test cases consumes memory, until it runs out:
If you cast the closure to an opaque before the loop then you are good:
Possible fixes:
FFI::Platypus::Type
? Probably good enough since the type should be reused in theFFI::Platypus
instance.CAVEATS
section, with the above alternate suggestion. This is reasonable if the use case is not common.The text was updated successfully, but these errors were encountered: