forked from hachi/Perlbal
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perlbal::Fields provide a simple way to override field::new that uses…
… Hash::Util::lock_ref_keys set env PERLBAL_REMOVE_FIELDS=1 to unactivate fields stuff fields are fast for reading access, but slow when need to create many objects
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package Perlbal::Fields; | ||
use strict; | ||
use warnings; | ||
use fields; | ||
|
||
# allow package to be called in command line | ||
__PACKAGE__->run(@ARGV) unless caller(); | ||
|
||
# should be the main method called, extra sub could be triggered from this point | ||
sub run { | ||
my ( $package, @options ) = @_; | ||
|
||
# unactivate fields if launch in command line | ||
$package->remove(); | ||
|
||
1; | ||
} | ||
|
||
# hash with keys and undef val for each class | ||
my $cache_for_class = {}; | ||
|
||
# replace fields::new method which uses Hash::Util::lock_ref_keys | ||
# - it's a good idea to keep using the original fields::new during development stage | ||
# - but during production we can avoid locking hash and wasting time doing this ( ~ 30 % ) | ||
sub remove { | ||
my ($class) = @_; | ||
|
||
no warnings "redefine"; | ||
no strict 'refs'; | ||
*fields::new = sub { | ||
my $class = shift; | ||
$class = ref $class if ref $class; | ||
|
||
if ( !defined( $cache_for_class->{$class} ) ) { | ||
my $h = {}; | ||
my @keys = keys %{ $class . "::FIELDS" }; | ||
map { $h->{$_} = undef; } @keys; | ||
$cache_for_class->{$class} = $h; | ||
} | ||
my %h = %{ $cache_for_class->{$class} }; | ||
|
||
return bless \%h, $class; | ||
}; | ||
} | ||
|
||
1; | ||
|