Perl Monk, Perl Meditation | |
PerlMonks |
Re: Re: Re: optimization - exists should return a referenceby MarkM (Curate) |
on Jan 15, 2003 at 08:01 UTC ( [id://227078]=note: print w/replies, xml ) | Need Help?? |
With regard to the suggestion that exists() return a reference, and not a boolean, BrowserUk writes: I'm really unfamiliar with the perl source, and I still get lost in all the macro's, but looking at the code for Perl_avhv_exists_ent() in av.c, it looks like it might be less work to return the reference than to generate the boolean? The correct starting place to look for the implementation of Perl op-codes is the pp*.c files. In this case, look in pp.c for a line that reads: PP(pp_exists)The function that ends up being called to actually perform the hash lookup is hv.c:hv_exists_ent() (the avhv code still exists in Perl 5.8.0, although the use of avhv's has been deprecated). The hash itself contains a C "SV *" reference to the value. Perl references, however, are one level above this. A new RV would need to be created using newRV_inc(SV *), and the RV would be returned. The return value would then need to be dereferenced to fetch data from it, or store data to it. Unless the value is to be accessed frequently, it is questionable whether the allocation of an RV, and the following dereferencing of the RV, from Perl code, would be any cheaper than just looking up the value in the hash again.
In Section
Meditations
|
|