Bug 33745: Lazily create attribute accessor methods in AUTOLOAD

Lazily create accessor methods when getting or setting an Koha::Object
attribute resulting in a significant speed up.

To test:
1) Run the benchmark.pl script
2) Apply the patch
3) Run the script again, Koha::Object accessors should be about 7 times faster
5) Ensure tests in t/db_dependent/Item.t still pass

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
David Gustafsson 2023-05-16 13:42:55 +02:00 committed by Tomas Cohen Arazi
parent 8fac9067b2
commit 837d0a955e
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F

View file

@ -900,7 +900,7 @@ The autoload method is used only to get and set values for an objects properties
=cut
sub AUTOLOAD {
my $self = shift;
my ($self) = @_;
my $method = our $AUTOLOAD;
$method =~ s/.*://;
@ -908,13 +908,17 @@ sub AUTOLOAD {
my @columns = @{$self->_columns()};
# Using direct setter/getter like $item->barcode() or $item->barcode($barcode);
if ( grep { $_ eq $method } @columns ) {
if ( @_ ) {
$self->_result()->set_column( $method, @_ );
return $self;
} else {
my $value = $self->_result()->get_column( $method );
return $value;
}
no strict 'refs';
*{$AUTOLOAD} = sub {
my $self = shift;
if ( @_ ) {
$self->_result()->set_column( $method, @_);
return $self;
} else {
return $self->_result()->get_column( $method );
}
};
goto &{$AUTOLOAD};
}
my @known_methods = qw( is_changed id in_storage get_column discard_changes make_column_dirty );
@ -924,7 +928,8 @@ sub AUTOLOAD {
show_trace => 1
) unless grep { $_ eq $method } @known_methods;
# Remove $self so that @_ now contain arguments only
shift;
my $r = eval { $self->_result->$method(@_) };
if ( $@ ) {
Koha::Exceptions::Object->throw( ref($self) . "::$method generated this error: " . $@ );