Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^5: Perl memory usage

by Anonymous Monk
on Sep 01, 2005 at 20:02 UTC ( #488480=note: print w/replies, xml ) Need Help??

in reply to Re^4: Performance, Abstraction and HOP
in thread Performance, Abstraction and HOP

I think that 6x bigger and 5x slower pretty much makes my point.
Holy crap, now I'm curious. Only 5x slower is faster than what I would have guessed. But 6x larger is crazy. Anyone know how much boxing Perl does? I would have thought that the rough estimate for the size of a scalar number would be say 16 bytes (4 bytes for a pointer, 4 bytes for type/reference-count information, 8 bytes for a double precision floating point number). And the overhead for an array at maybe 16 bytes (8 bytes for a length field, 4 bytes for type/reference-count info, 4 bytes for a pointer to the array of pointers). For the tree structure above (an array composed of one scalar and two arrays) that would be 16 (array overhead)+3*4(three elements in first array, 4 byte pointers (32 bit machine))+16(the scalar)+2*16(the left and right branches) = 76 bytes. I guess that's starting to add up, but it is still shy of the 112 bytes measured above. Perl must preallocate space for each array to make growing it faster (maybe 12 elements initially?). Does that sound about right? Any way to get a more slimed down data structure in pure Perl?

Replies are listed 'Best First'.
Re^6: Perl memory usage
by kscaldef (Pilgrim) on Sep 02, 2005 at 08:26 UTC
    Is 6x really that crazy? Doesn't a typical generic C tree use about 4x? (If you're storing integers in the tree. If you are storing something larger, the overhead is smaller.) The perl array includes at least a reference count and a length that aren't in the C Node struct, so 6x quickly becomes plausible.
      Well, on a 32bit machine, it is 3x larger...
      struct tree { int elem; struct tree *left, *right; }; int main(int argc, char **argv) { printf("sizeof(int)=%d,sizeof(struct tree)=%d\n", sizeof(int),sizeof(struct tree)); }
      ...of course, the 12 bytes from above is a tad bit slimmer than the apparently 112 byte structure on the perl side.

        I said "generic" tree, along the lines of a glib GTree. So, there's an extra pointer to the data:

        struct Node { gpointer data; struct Node *left; struct Node *right; }

        So, 3 (probably 4 byte) pointers, plus the data pointed to.

        The perl implementation, though, is not a simple struct. Instead, the three members are SVs in an array, and an SV is significantly larger than a single pointer. Then there's some additional overhead for book-keeping on the array.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://488480]
and the leaves swirl about...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2017-03-29 19:40 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (352 votes). Check out past polls.