Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Problem on perl callbacks from XS

by cosimo (Hermit)
on May 25, 2008 at 09:04 UTC ( [id://688376]=note: print w/replies, xml ) Need Help??


in reply to Re: Problem on perl callbacks from XS
in thread Problem on perl callbacks from XS

Yes, indeed. This should be the approach. However, (there's always one :), I want the serialization function to handle a big data structure, which can contain these blessed objects rather deep inside it.

Since the main purpose of all this mess is to speed up things, I thought I'd want to pass a reference to this "big structure" and then the XS function would do the heavy work.

Doing as you say, and I've tried that before, probably means that I must move all the data structure traversal outside of XS. Example:

my $big_struct = [ {a=>1}, {b=>2}, [1, 2, 3, 4, 5], ['blah', 'blah', ['bluh', $blessed_object]], {d=>3, e=>[1,2,3]} ];

If I want to correctly handle $blessed_object from XS, I must call its toString() (assume for now it has one) with call_sv().
Doing this from Perl code, means I must shift all the structure traversal from XS to Perl... losing a share of the speed gain...

Replies are listed 'Best First'.
Re^3: Problem on perl callbacks from XS
by tachyon-II (Chaplain) on May 25, 2008 at 15:35 UTC

    Here is an example. It's done in Inline but is just straight vanilla XS. The stack pointer behaves itself over the separate calls.

    package Serialize; use Data::Dump; sub new { bless {foo=>'bar'}, shift } sub toString { return Data::Dump::dump($_[0]) } my $obj = new Serialize; my $str = "some string"; print serialize($obj), $/; print serialize($str), $/; use Inline Config => FORCE_BUILD => 1, CLEAN_AFTER_BUILD => 0; use Inline C => <<'END_OF_C_CODE'; void serialize(SV* sv_data) { int count; dXSARGS; sp = mark; printf("\nEntry SP is %d\n", sp); if (sv_isobject(sv_data)) { // this is an object so presume a toString method PUSHMARK(sp); XPUSHs(sv_data); // whack it back on the stack PUTBACK; count = perl_call_pv("toString", G_SCALAR); if ( count != 1 ) croak("Expected 1 value got %d\n", count); XPUSHs(sv_2mortal(newSVsv(POPs))); } else { // just return SV with something appended for this example sv_catpv(sv_data, " - serialized\0"); XPUSHs(sv_2mortal(newSVsv(sv_data))); } PUTBACK; printf("Exit SP is %d\n", sp); XSRETURN(1); } END_OF_C_CODE __DATA__ bless({ foo => "bar" }, "Serialize") some string - serialized Entry SP is 32203548 Exit SP is 32203552 Entry SP is 32203548 Exit SP is 32203552

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://688376]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-03-28 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found