develooper Front page | perl.perl5.porters | Postings from October 2016

Re: hv.h hek definition

Thread Previous | Thread Next
From:
Todd Rinaldo
Date:
October 14, 2016 03:46
Subject:
Re: hv.h hek definition
Message ID:
988FE4B8-09BE-46FB-8133-7DF85683DD54@cpanel.net
> On Sep 28, 2016, at 10:27 PM, Father Chrysostomos <sprout@cpan.org> wrote:
> 
> Todd Rinaldo wrote:
>> IMO, the declaration for hek_key is VERY misleading. In the context of a
>> struct, what you end up with when you do char hek_key[1]; is a char
>> pointer followed by a char.
> 
> char hek_key[1] stores the char array's members directly in the
> struct.  There is no pointer stored in the struct.
> 

Right this was my first time seeing a char[1] hanging off the end of a struct so one could dynamically allocate a variable size string off the end of the struct without the need for a pointer. Neat trick.

Digging further I now have a few questions:

1. The only value of HvHASKFLAGS is for Perl_hv_assert to make sure the flag has been set properly. It does this by checking HeKUTF8 and HeKWASUTF8 against every he/hek in the hash and determining if any of them are set. The only other use of this I can find is to turn the flag on and off. I can find no evidence in perl or on grep.cpan.me that anything is using the flag. Does it actually have value?

2. HEK_FLAGS seems too complicated. Rather than being stored as a "char hek_flags;" in the hek struct, It hides off the tail end of the hek_key. From what I've been able to determine, it is this way because the hek_key used to be a PV way back when it wasn't in its own struct. So instead of the HEK_FLAGS macro being something simple like:

(hek)->hek_flags

It instead has to be something complicated like:

(*((unsigned char *)(HEK_KEY(hek))+HEK_LEN(hek)+1)) 

I can only imagine that the former would be much cheaper computationally. It is true that when an SV rather than the normal C string hangs off the end of a HEK, the flag is wasted. However from what I can tell, this is an extreme rarity (related to magic) that an SV is ever used in a hek. I could probably produce some anecdotal proof of the rarity of SVs if it was needed.

I have attached a patch in this email to show what I think would need to be changed to simplify HEK_FLAGS.

3. This is still stewing in my head but it occurs to me that if you no longer had a flag hanging off the end of your hek_key, you could store a COW counter. I'm not sure what use cases that would provide but it might be worth considering.

This patch seems to pass tests. It seems too simple, but then that's the beauty of using macros, right?


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About