From c0c8edf6f048a16d347cf21f8fd7d464d2c7dfce Mon Sep 17 00:00:00 2001 From: "Eric A. Miller" Date: Fri, 25 Oct 2013 19:00:50 -0400 Subject: [PATCH] Use cursor to speed up get_port_macs(). This provides a significant performance improvement in both macsuck and macwalk operations especially in deployments with a large number of devices and device interfaces. --- Netdisco/lib/App/Netdisco/Util/PortMAC.pm | 43 +++++++++++++---------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/Netdisco/lib/App/Netdisco/Util/PortMAC.pm b/Netdisco/lib/App/Netdisco/Util/PortMAC.pm index 4ec33a53..6aee6dda 100644 --- a/Netdisco/lib/App/Netdisco/Util/PortMAC.pm +++ b/Netdisco/lib/App/Netdisco/Util/PortMAC.pm @@ -29,28 +29,35 @@ addresses on a device. =cut sub get_port_macs { - my $device = shift; - my $port_macs = {}; + my $device = shift; + my $port_macs = {}; - unless ($device->in_storage) { - debug sprintf ' [%s] get_port_macs - skipping device not yet discovered', - $device->ip; - return $port_macs; - } + unless ( $device->in_storage ) { + debug sprintf + ' [%s] get_port_macs - skipping device not yet discovered', + $device->ip; + return $port_macs; + } - my $dp_macs = schema('netdisco')->resultset('DevicePort') - ->search({ mac => { '!=' => undef} }); - while (my $r = $dp_macs->next) { - $port_macs->{ $r->mac } = $r->ip; - } + my $dp_macs + = schema('netdisco')->resultset('DevicePort') + ->search( { mac => { '!=' => undef } }, + { select => [ 'mac', 'ip' ] } ); + my $dp_cursor = $dp_macs->cursor; + while ( my @vals = $dp_cursor->next ) { + $port_macs->{ $vals[0] } = $vals[1]; + } - my $d_macs = schema('netdisco')->resultset('Device') - ->search({ mac => { '!=' => undef} }); - while (my $r = $d_macs->next) { - $port_macs->{ $r->mac } = $r->ip; - } + my $d_macs + = schema('netdisco')->resultset('Device') + ->search( { mac => { '!=' => undef } }, + { select => [ 'mac', 'ip' ] } ); + my $d_cursor = $d_macs->cursor; + while ( my @vals = $d_cursor->next ) { + $port_macs->{ $vals[0] } = $vals[1]; + } - return $port_macs; + return $port_macs; } 1;