October 08, 2002

Deep weirdness

I've been digging around inside the guts of perl 5 the past day or so. Ann was looking for a way to get the real size of data structures in perl. This isn't the first, or the fiftieth, time that anyone's asked for it, but it's the first time I've much cared, so I figured I'd go give it a shot. I've been feeling rusty, twiddling words and ideas around, and it's nice to just dig into a good chunk of XS for a change.

This, of course, turns out to be more difficult than I'd originally thought. But, then, what isn't? :)

Scalars are reasonably straightforward, mostly, if you're aware of the tricks perl plays under the hood. (Scalars are different sizes in some cases depending on whether you've built perl with purify support) Things get interesting with some of the odder SV types, and there's always the question of whether some of the ancillary support structs count or not. (Do you count the size of the magic structs?)

Arrays and hashes, though...

Perl plays lots of games with arrays and hashes for speed, and because of it the code's pretty hairy in spots. And poorly documented, of course, which is why it takes so long to figure out that xof_off, for example, isn't used anywhere. And figuring out which bits count as the real array and which don't, is also fun. Not that you necessarily need to know that to get the used array size, but if you want to figure out how much actual memory the array's got allocated to it, rather than actually in use, well... Not rocket science, but the half-dozen levels of macros in the source doesn't help.

But, still, it's mostly done. At least the simple bits. Doing full data structures is the interesting bit, since that means tracking both what we need to check as well as what we've already seen. This could get rather expensive as memory goes but, well, there's not much for it if you want to do it right.

Posted by Dan at October 8, 2002 12:47 AM
Comments