From 28d51dce158b97b3d197cc54f7f46b8d724bc5aa Mon Sep 17 00:00:00 2001 From: Kirill Solomko <ksolomko@sipwise.com> Date: Fri, 28 Jun 2019 15:49:55 +0200 Subject: [PATCH] TT#58950 RedisLocationResult improvements * switch from Moose to Moo reduced memory consumption * rework RedisLocationResultSource to use AUTOLOAD instead of creating accessors in BUILD (that is very expensive considering the amount of rows, multiplied by the overall amount of entries). Now the object creation takes sub 0.0001 sec from 0.017 sec as before. Change-Id: I9917ff38266ce89297adf55d75c40dd5f16a435b --- .../Panel/Utils/RedisLocationResultSet.pm | 47 ++++++++++--------- .../Panel/Utils/RedisLocationResultSource.pm | 24 ++++------ 2 files changed, 35 insertions(+), 36 deletions(-) diff --git a/lib/NGCP/Panel/Utils/RedisLocationResultSet.pm b/lib/NGCP/Panel/Utils/RedisLocationResultSet.pm index a867f69465..9ff1c2593c 100644 --- a/lib/NGCP/Panel/Utils/RedisLocationResultSet.pm +++ b/lib/NGCP/Panel/Utils/RedisLocationResultSet.pm @@ -1,37 +1,54 @@ package NGCP::Panel::Utils::RedisLocationResultSet; -use Moose; +use Moo; use TryCatch; +use NGCP::Panel::Utils::Generic qw(:all); use NGCP::Panel::Utils::RedisLocationResultSource; use Data::Dumper; +use Time::HiRes qw(time); has _c => ( is => 'ro', - isa => 'NGCP::Panel', + isa => sub { die "$_[0] must be NGCP::Panel" unless $_[0] && ref $_[0] eq 'NGCP::Panel' }, ); has _redis => ( is => 'ro', - isa => 'Redis', + isa => sub { die "$_[0] must be Redis" unless $_[0] && ref $_[0] eq 'Redis' }, ); has _rows => ( is => 'rw', - isa => 'ArrayRef', + isa => sub { die "$_[0] must be ARRAY" unless $_[0] && ref $_[0] eq 'ARRAY' }, default => sub {[]} ); has _query_done => ( is => 'rw', - isa => 'Int', + isa => sub { die "$_[0] must be int" unless defined $_[0] && is_int($_[0]) }, default => 0, ); +has result_source => ( + is => 'ro', + default => sub { NGCP::Panel::Utils::RedisLocationResultSource->new }, +); + +has result_class => ( + is => 'ro', + default => 'dummy', +); + +has current_source_alias => ( + is => 'ro', + default => 'me', +); + has _domain_resellers => ( is => 'rw', - isa => 'HashRef', + isa => sub { die "$_[0] must be HASHREF" unless $_[0] && ref $_[0] eq 'HASH' }, lazy => 1, default => sub { my $self = shift; @@ -164,7 +181,6 @@ sub _filter { foreach my $row (@{ $self->_rows }) { my $match = 0; my $filter_applied = 0; - my %attr = map { $_->name => 1 } $row->meta->get_all_attributes; foreach my $colname (keys %{ $filter }) { my $condition = $filter->{$colname}; my $searchname = $colname; @@ -173,7 +189,7 @@ sub _filter { next if ($colname =~ /\./); # we don't support joined table columns $filter_applied = 1; if (ref $condition eq "") { - if (!exists $attr{$colname} || lc($row->$colname) ne lc($condition)) { + if (lc($row->$colname) ne lc($condition)) { $match = 0; last; } else { @@ -183,7 +199,7 @@ sub _filter { my $fil = $condition->{like}; $fil =~ s/^\%//; $fil =~ s/\%$//; - if (!exists $attr{$colname} || $row->$colname !~ /$fil/i) { + if ($row->$colname !~ /$fil/i) { $match = 0; last; } else { @@ -221,17 +237,4 @@ sub _scan { return 1; } -sub result_class { - "dummy"; -} - -sub result_source { - NGCP::Panel::Utils::RedisLocationResultSource->new; -} - -sub current_source_alias { - my ($self) = @_; - return 'me'; -} - 1; diff --git a/lib/NGCP/Panel/Utils/RedisLocationResultSource.pm b/lib/NGCP/Panel/Utils/RedisLocationResultSource.pm index d9f6af4587..0713637401 100644 --- a/lib/NGCP/Panel/Utils/RedisLocationResultSource.pm +++ b/lib/NGCP/Panel/Utils/RedisLocationResultSource.pm @@ -1,21 +1,20 @@ package NGCP::Panel::Utils::RedisLocationResultSource; -use Moose; +use Moo; + +our $AUTOLOAD; has _data => ( is => 'ro', - isa => 'HashRef', + isa => sub { die "$_[0] must be HASHREF" unless $_[0] && ref $_[0] eq 'HASH' }, default => sub {{}}, ); -sub BUILD { - my ($self) = @_; - foreach my $k (keys %{ $self->_data }) { - $self->meta->add_attribute( - $k => (accessor => $k) - ); - $self->$k($self->_data->{$k}); - } +sub AUTOLOAD { + my $self = shift; + my $col = $AUTOLOAD; + $col = (split /::/, $col)[-1]; + return $self->_data->{$col}; } sub get_inflated_columns { @@ -23,9 +22,6 @@ sub get_inflated_columns { return %{ $self->_data }; } -sub columns { - -} - +sub columns { } 1;