On May 02, Brian Ingerson wrote:
>
> I was thinking you might want to look at the implementation of
> Tie::GHash, written by my friend Leon "Acme" Brocard. He uses tie with
> Inline, but with a twist: all of the tie functions (TIEHASH, STORE,
> FETCH, etc) are written in C! It's a wonderful example of Inline::C and
> it may help you make your solution even faster.
Actually, this is exactly how it works. Each of the method handlers are
thinly veiled calls to actual C code. My previous mail showed an example
of a call to "lookup". No Perl data is ever created in C land (which makes
things much less complicated). The scalars held by Perl are just C
pointers.
Another example:
sub EXISTS
{
my $self = shift;
my $key = shift;
#
# for debug purposes
#
printf("Request fetch of key %s of hash %d\n", $key, $$self);
return Virgelo::lookup($$self, $key);
}
Where lookup is:
__C__
void* lookup(void *h, char *key)
{
return hash_lookup(h, key);
}
The twist of my solution over Tie::GHash is that GHash requires a
serialization scheme to enable mult-level data structures (hashes of hashes
ad nauseum) - yuck. The results those methods pass back are not again
"tied" to another (or the same) class. GHash is not a general solution for
the kinds of data we normally see in Perl, Python, or Ruby.
The two main limitations of my way are that:
1) C structs must be painstakingly translated into Perl hashes. I looked
at Inline::Struct briefly, but I'm not sure if it does what I want.
2) You have to know ahead of time what your data structure looks like, so
you can tie the right things to the right classes. Unless you're the
Amazing Kreskin, this should be a necesity.
3) The data I'm querying is an in-memory copy, not disk.
-Clint