This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Change scalar(%hash) to be the same as 0+keys(%hash)
authorYves Orton <demerphq@gmail.com>
Mon, 20 Jun 2016 20:51:38 +0000 (22:51 +0200)
committerYves Orton <demerphq@gmail.com>
Wed, 22 Jun 2016 16:21:32 +0000 (18:21 +0200)
commit8bf4c4010cc474d4000c2a8c78f6890fa5f1e577
treeb12d25aa70138f2dbc13bf1bb49a93fab7e7a4b7
parent6c50b67b99a3df9486896d14dc294825a148a673
Change scalar(%hash) to be the same as 0+keys(%hash)

This subject has a long history see [perl #114576] for more discussion.
https://rt.perl.org/Public/Bug/Display.html?id=114576

There are a variety of reasons we want to change the return signature of
scalar(%hash). One is that it leaks implementation details about our
associative array structure. Another is that it requires us to keep track
of the used buckets in the hash, which we use for no other purpose but
for scalar(%hash). Another is that it is just odd. Almost nothing needs to
know these values. Perhaps debugging, but we have several much better
functions for introspecting the internals of a hash.

By changing the return signature we can remove all the logic related
to maintaining and updating xhv_fill_lazy. This should make hot code
paths a little faster, and maybe save some memory for traversed hashes.

In order to provide some form of backwards compatibility we adds three
new functions to the Hash::Util namespace: bucket_ratio(), num_buckets()
and used_buckets(). These functions are actually implemented in
universal.c, and thus always available even if Hash::Util is not loaded.
This simplifies testing. At the same time Hash::Util contains backwards
compatible code so that the new functions are available from it should
they be needed in older perls.

There are many tests in t/op/hash.t that are more or less obsolete after
this patch as they test that xhv_fill_lazy is correctly set in various
situations. However since we have a backwards compat layer we can just
switch them to use bucket_ratio(%hash) instead of scalar(%hash) and keep
the tests, just in case they are actually testing something not tested
elsewhere.
22 files changed:
MANIFEST
dump.c
embed.fnc
embed.h
ext/Devel-Peek/t/Peek.t
ext/Hash-Util-FieldHash/t/05_perlhook.t
ext/Hash-Util/Changes
ext/Hash-Util/Util.xs
ext/Hash-Util/lib/Hash/Util.pm
ext/Hash-Util/t/builtin.t [new file with mode: 0644]
hv.c
hv.h
pod/perldata.pod
pod/perldelta.pod
pod/perltie.pod
proto.h
sv.c
t/op/coreamp.t
t/op/each.t
t/op/hash.t
t/op/sub_lval.t
universal.c