From 837d0a955e3b3ebf66cc381911330f9476a4f2ff Mon Sep 17 00:00:00 2001 From: David Gustafsson Date: Tue, 16 May 2023 13:42:55 +0200 Subject: [PATCH] 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 Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- Koha/Object.pm | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Koha/Object.pm b/Koha/Object.pm index e463f94920..13ef9acacd 100644 --- a/Koha/Object.pm +++ b/Koha/Object.pm @@ -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: " . $@ ); -- 2.39.5