Skip Menu |

This queue is for tickets about the Tie-IxHash CPAN distribution.

Report information
The Basics
Id: 39619
Status: open
Priority: 0/
Queue: Tie-IxHash

People
Owner: Nobody in particular
Requestors: SREZIC [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 1.21
Fixed in: (no value)



Subject: Add SCALAR method
Adding the SCALAR method would more than double the speed of conditions à la if (%ixhash) { ... } See the attached benchmark script. The implementation: sub SCALAR { scalar @{ $_[0]->[1] }; } Note that currently scalar %ixhash would return undef for false and 1 for true. A normal hash usually returns 0 for false and some quotient for true. With this implementation the return value would be 0 for false and the number of keys for true. Regards, Slaven
Subject: tie-ixhash-scalar.pl
#!/usr/bin/perl -w # -*- perl -*- # # $Id: $ # Author: Slaven Rezic # # Copyright (C) 2008 Slaven Rezic. All rights reserved. # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # Mail: slaven@rezic.de # WWW: http://www.rezic.de/eserte/ # use Benchmark qw(cmpthese); use Tie::IxHash; { package Tie::MyIxHash; use base qw(Tie::IxHash); sub SCALAR { scalar @{ $_[0]->[1] }; } } tie %ixhash, 'Tie::IxHash'; tie %myixhash, 'Tie::MyIxHash'; %ixhash = %INC; %myixhash = %INC; cmpthese(-1, { without_SCALAR => sub { if (%ixhash) { # nop } }, with_SCALAR => sub { if (%myixhash) { # nop } }, } ); __END__ Rate without_SCALAR with_SCALAR without_SCALAR 159421/s -- -56% with_SCALAR 358596/s 125% --
The upcoming MCE 1.7 release will include MCE::Shared. It will allow ordered hash keys. Your module is among 1 of the 3 supported. Unfortuantely, it is missing 2 of the 8 Tie StdHash functions; CLEAR and SCALAR. use Tie::IxHash; use MCE::Flow max_workers => 4; use MCE::Shared ordered => 1; my $db = mce_share_m {}; $db->{key1} = 'foo'; $db->{key2} = 'bar'; $db->{key3} = 'baz'; $db->{counter} = 0; mce_flow sub { $db->lock(); $db->{counter} += 1; $db->unlock(); }; print $db->{counter}, "\n"; # 4 MCE::Shared adds CLEAR and SCALAR (only if missing). The SCALAR output is the same as from a normal hash. ... elsif($INC{'Tie/IxHash.pm'}) { unless (Tie::IxHash->can('SCALAR')) { eval q( sub Tie::IxHash::SCALAR { scalar %{ $_[0]->[0] } } )} unless (Tie::IxHash->can('CLEAR')) { eval q( *{ Tie::IxHash::CLEAR } = \&Tie::IxHash::Clear; )} } Thank you in advance if considering to add the 2 missing of 8 TIE StdHash functions. Regards, Mario
On 2015-04-25 00:42:59, MARIOROY wrote: Show quoted text
> > The upcoming MCE 1.7 release will include MCE::Shared. It will allow > ordered hash keys. Your module is among 1 of the 3 supported. > Unfortuantely, it is missing 2 of the 8 Tie StdHash functions; CLEAR > and SCALAR. > > use Tie::IxHash; > use MCE::Flow max_workers => 4; > use MCE::Shared ordered => 1; > > my $db = mce_share_m {}; > > $db->{key1} = 'foo'; > $db->{key2} = 'bar'; > $db->{key3} = 'baz'; > $db->{counter} = 0; > > mce_flow sub { > $db->lock(); > $db->{counter} += 1; > $db->unlock(); > }; > > print $db->{counter}, "\n"; # 4 > > MCE::Shared adds CLEAR and SCALAR (only if missing). The SCALAR output > is the same as from a normal hash. > > ... > elsif($INC{'Tie/IxHash.pm'}) { > unless (Tie::IxHash->can('SCALAR')) { eval q( > sub Tie::IxHash::SCALAR { scalar %{ $_[0]->[0] } } > )} > unless (Tie::IxHash->can('CLEAR')) { eval q( > *{ Tie::IxHash::CLEAR } = \&Tie::IxHash::Clear; > )} > } > > Thank you in advance if considering to add the 2 missing of 8 TIE > StdHash functions.
Hi Mario, thanks for pointing to the missing CLEAR method. I right now had the situation where a %ixhash=() took many many minutes, and after defining the CLEAR method as above the performance problem went away. Regards, Slaven
RT-Send-CC: MARIOROY [...] cpan.org
On 2015-04-25 00:42:59, MARIOROY wrote: Show quoted text
> > The upcoming MCE 1.7 release will include MCE::Shared. It will allow > ordered hash keys. Your module is among 1 of the 3 supported. > Unfortuantely, it is missing 2 of the 8 Tie StdHash functions; CLEAR > and SCALAR. > > use Tie::IxHash; > use MCE::Flow max_workers => 4; > use MCE::Shared ordered => 1; > > my $db = mce_share_m {}; > > $db->{key1} = 'foo'; > $db->{key2} = 'bar'; > $db->{key3} = 'baz'; > $db->{counter} = 0; > > mce_flow sub { > $db->lock(); > $db->{counter} += 1; > $db->unlock(); > }; > > print $db->{counter}, "\n"; # 4 > > MCE::Shared adds CLEAR and SCALAR (only if missing). The SCALAR output > is the same as from a normal hash. > > ... > elsif($INC{'Tie/IxHash.pm'}) { > unless (Tie::IxHash->can('SCALAR')) { eval q( > sub Tie::IxHash::SCALAR { scalar %{ $_[0]->[0] } } > )} > unless (Tie::IxHash->can('CLEAR')) { eval q( > *{ Tie::IxHash::CLEAR } = \&Tie::IxHash::Clear; > )} > } > > Thank you in advance if considering to add the 2 missing of 8 TIE > StdHash functions.
Hi Mario, I created a ticket specifically for the CLEAR method: https://rt.cpan.org/Ticket/Display.html?id=133647 For the SCALAR method, there's now a pull request: https://github.com/chorny/Tie-IxHash/pull/5 The implementation is now slightly different to provide full compatibility with previous Tie::IxHash versions (i.e. always return 1 instead of the number of keys in the TRUE case, and return the empty string in the FALSE case). Regards, Slaven