Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

enabling XML::Rabbit serialization

by glasswalk3r (Pilgrim)
on Dec 19, 2012 at 18:23 UTC ( #1009596=perlquestion: print w/ replies, xml ) Need Help??
glasswalk3r has asked for the wisdom of the Perl Monks concerning the following question:

Hello there monks,

I started using XML::Rabbit a couples of days ago and I quite content with it for reading XML configuration files.

I would like to add some optimization to the program by serializing the objects to the disk and avoid some memory consumption by recovering the values over and over from XML.

The problem is that XML::Rabbit objects do not hold the recovered values from XPath expression in classes attributes. I tried to use a naive approach with Storable but this is what I got from it:

c:\Temp>rabbit.pl Bizarre copy of UNKNOWN in subroutine entry at C:/Perl/site/lib/XML/Li +bXML/XPathContext.pm line 67. PmmREFCNT_dec: REFCNT decremented below 0 for 2ca1798! at C:/Perl/site +/lib/XML/LibXML/XPathContext.pm line 67. PmmREFCNT_dec: REFCNT decremented below 0 for 2cbe650! at C:/Perl/site +/lib/XML/LibXML.pm line 1549. PmmREFCNT_dec: REFCNT decremented below 0 for 2cbe638! at C:/Perl/site +/lib/XML/LibXML.pm line 1549. PmmREFCNT_dec: REFCNT decremented below 0 for 2cbe620! at C:/Perl/site +/lib/XML/LibXML.pm line 1549. PmmREFCNT_dec: REFCNT decremented below 0 for 2cbe608! at C:/Perl/site +/lib/XML/LibXML.pm line 1549. PmmREFCNT_dec: REFCNT decremented below 0 for 2cbe5d8! at C:/Perl/site +/lib/XML/LibXML.pm line 1549. PmmREFCNT_dec: REFCNT decremented below 0 for 2cbe5a8! at C:/Perl/site +/lib/XML/LibXML.pm line 1549. PmmREFCNT_dec: REFCNT decremented below 0 for 2cbe5c0! at C:/Perl/site +/lib/XML/LibXML.pm line 1549. PmmREFCNT_dec: REFCNT decremented below 0 for 2cbe290! at C:/Perl/site +/lib/XML/LibXML.pm line 1549. PmmREFCNT_dec: REFCNT decremented below 0 for 2cdff18! at C:/Perl/site +/lib/XML/LibXML.pm line 1549.

Considering that XML::Rabbit is Moose based, what do you recommend to use for objects serialization? I thought about cloning the XML::Rabbit objects, saving the values as attributes and keeping the references as necessary, but this looks like a lot of trouble to do it.

Thanks,

Alceu Rodrigues de Freitas Junior
---------------------------------
"You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

Comment on enabling XML::Rabbit serialization
Download Code
Re: enabling XML::Rabbit serialization
by tobyink (Abbot) on Dec 19, 2012 at 18:34 UTC

    XML::LibXML is a wrapper around the C libxml2 library. Most XML::LibXML objects are just ephemeral pointers (which pop in and out of existence as required) to the "real" objects which live in "C space".

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: enabling XML::Rabbit serialization
by Jenda (Abbot) on Dec 19, 2012 at 19:02 UTC

    I don't think I understand. The XML is a serialization of the data already, why do you want to have another? If you are parsing the same XML several times during one run time of the script, then just keep it in memory. If it's a config, it's unlikely it's gonna be prohibitively big.

    If you parse the XML just one each time you run the script, then unless the script is a very tiny thing, the time spent parsing the XML will be irrelevant. Besides you'd have to parse the other serialization as well!

    If the working with the XML::Rabbit created objects is slow, you'll have to use something else. In either case, FIRST PROFILE the script to find out where the problem is and THEN OPTIMIZE the problematic part.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      Your observation is right Jenda, but I would like to give a try to Sereal since it seems to be faster and use less memory.

      And yes, the script executes very quickly since it will be a Nagios plugin. Parsing the XML once is useful because somebody must configure de plugin, but for later executions, recovering the data from the XML as plain Moose objects would be faster and use less memory. After parsing the XML, it is just a matter to save the new objects in the file system and recovering them latter. Since of it like a cache.

      Looks like I will need to clone the XML::Rabbit objects by copying their "attributes" to plain Moose objects.

      Alceu Rodrigues de Freitas Junior
      ---------------------------------
      "You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill

        Do they have to be Moose objects? And does it have to be XML::Rabbit? You can just as easily set up XML::Rules to build a data structure (either plain or blessed as plain old Perl objects) and thus have something easily serializable.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1009596]
Approved by tobyink
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2014-08-22 02:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (145 votes), past polls