From d1ad545c228b4f330e1b57390282f33cbd9d3619 Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Thu, 27 Feb 2020 23:17:23 +0100 Subject: [PATCH] TT#76851 Remove unnecessary inner-loop for interface access The host hash contains interfaces and other keys, but one of these keys contains a list of the interfaces, so we just need to check whether the interface exists in the hash, instead of scanning all the keys in the hash and matching the one that is equal to the one we are looking for which is rather inefficient. Change-Id: If7d49f0a0680b82cd35ffb3007666d19741372c4 --- lib/get_all_adv_ips | 16 +++++++--------- lib/get_all_adv_ips_for_host | 14 ++++++-------- lib/get_all_hosts_ips | 12 +++++------- lib/get_all_ifaces_for_host | 8 +++----- lib/get_all_ips | 12 +++++------- lib/get_all_ips_for_host | 10 ++++------ lib/get_all_shared_ips | 16 +++++++--------- lib/get_all_shared_ips_for_host | 14 ++++++-------- lib/get_all_shared_v6ips | 20 +++++++++----------- lib/get_all_shared_v6ips_for_host | 18 ++++++++---------- lib/get_all_v6ips | 16 +++++++--------- lib/get_all_v6ips_for_host | 18 ++++++++---------- lib/get_default_pingnodes | 23 +++++++++-------------- lib/get_iface | 10 ++++------ lib/get_network_for_host | 22 +++++++++------------- 15 files changed, 97 insertions(+), 132 deletions(-) diff --git a/lib/get_all_adv_ips b/lib/get_all_adv_ips index 3346fd49..29ac9619 100644 --- a/lib/get_all_adv_ips +++ b/lib/get_all_adv_ips @@ -34,16 +34,14 @@ FOREACH host IN hosts.keys.sort; NEXT IF blktmp.processed_hosts.$host.defined; FOREACH iface IN hosts.$host.interfaces; - FOREACH realiface IN hosts.$host.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.$host.$realiface.type.grep('^' _ argv.type _ '$').size(); + NEXT UNLESS hosts.$host.exists(iface); + NEXT UNLESS hosts.$host.$iface.type.grep('^' _ argv.type _ '$').size(); - peer = hosts.$host.peer; - blktmp.processed_hosts.$peer = 1; - FOREACH ip IN hosts.$host.$realiface.advertised_ip; - NEXT UNLESS ip; - out.push(ip); - END; + peer = hosts.$host.peer; + blktmp.processed_hosts.$peer = 1; + FOREACH ip IN hosts.$host.$iface.advertised_ip; + NEXT UNLESS ip; + out.push(ip); END; END; END; diff --git a/lib/get_all_adv_ips_for_host b/lib/get_all_adv_ips_for_host index 92544f4a..00fadca2 100644 --- a/lib/get_all_adv_ips_for_host +++ b/lib/get_all_adv_ips_for_host @@ -33,14 +33,12 @@ END; out = []; IF status.item(hosts.${argv.host}.status); FOREACH iface IN hosts.${argv.host}.interfaces; - FOREACH realiface IN hosts.${argv.host}.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.${argv.host}.$realiface.type.grep('^' _ argv.type _ '$').size(); - - FOREACH ip IN hosts.${argv.host}.$realiface.advertised_ip; - NEXT UNLESS ip; - out.push(ip); - END; + NEXT UNLESS hosts.${argv.host}.exists(iface); + NEXT UNLESS hosts.${argv.host}.$iface.type.grep('^' _ argv.type _ '$').size(); + + FOREACH ip IN hosts.${argv.host}.$iface.advertised_ip; + NEXT UNLESS ip; + out.push(ip); END; END; END; diff --git a/lib/get_all_hosts_ips b/lib/get_all_hosts_ips index 2b8f06c6..31c9c90b 100644 --- a/lib/get_all_hosts_ips +++ b/lib/get_all_hosts_ips @@ -35,14 +35,12 @@ FOREACH host IN hosts.keys.sort; NEXT UNLESS ngcp.has_role(host, argv.role); FOREACH iface IN hosts.$host.interfaces; - FOREACH realiface IN hosts.$host.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.$host.$realiface.type.grep('^' _ argv.type _ '$').size(); - NEXT IF out.grep('^' _ hosts.$host.$realiface.ip _ '$').size(); - NEXT UNLESS hosts.$host.$realiface.ip; + NEXT UNLESS hosts.$host.exists(iface); + NEXT UNLESS hosts.$host.$iface.type.grep('^' _ argv.type _ '$').size(); + NEXT IF out.grep('^' _ hosts.$host.$iface.ip _ '$').size(); + NEXT UNLESS hosts.$host.$iface.ip; - out.push({ name = host, ip = hosts.$host.$realiface.ip }); - END; + out.push({ name = host, ip = hosts.$host.$iface.ip }); END; END; diff --git a/lib/get_all_ifaces_for_host b/lib/get_all_ifaces_for_host index 599a088c..cbe599a8 100644 --- a/lib/get_all_ifaces_for_host +++ b/lib/get_all_ifaces_for_host @@ -30,12 +30,10 @@ END; out = []; IF status.item(hosts.${argv.host}.status); FOREACH iface IN hosts.${argv.host}.interfaces; - FOREACH realiface IN hosts.${argv.host}.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.${argv.host}.$realiface.type.grep('^' _ argv.type _ '$').size(); + NEXT UNLESS hosts.${argv.host}.exists(iface); + NEXT UNLESS hosts.${argv.host}.$iface.type.grep('^' _ argv.type _ '$').size(); - out.push(realiface); - END; + out.push(iface); END; END; out = out.sort; diff --git a/lib/get_all_ips b/lib/get_all_ips index f6064951..60dd35dc 100644 --- a/lib/get_all_ips +++ b/lib/get_all_ips @@ -32,14 +32,12 @@ FOREACH host IN hosts.keys; NEXT UNLESS ngcp.has_role(host, argv.role); FOREACH iface IN hosts.$host.interfaces; - FOREACH realiface IN hosts.$host.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.$host.$realiface.type.grep('^' _ argv.type _ '$').size(); - NEXT IF out.grep('^' _ hosts.$host.$realiface.ip _ '$').size(); - NEXT UNLESS hosts.$host.$realiface.ip; + NEXT UNLESS hosts.$host.exists(iface); + NEXT UNLESS hosts.$host.$iface.type.grep('^' _ argv.type _ '$').size(); + NEXT IF out.grep('^' _ hosts.$host.$iface.ip _ '$').size(); + NEXT UNLESS hosts.$host.$iface.ip; - out.push(hosts.$host.$realiface.ip); - END; + out.push(hosts.$host.$iface.ip); END; END; out = out.sort; diff --git a/lib/get_all_ips_for_host b/lib/get_all_ips_for_host index a165f132..7b59281b 100644 --- a/lib/get_all_ips_for_host +++ b/lib/get_all_ips_for_host @@ -33,13 +33,11 @@ END; out = []; IF status.item(hosts.${argv.host}.status); FOREACH iface IN hosts.${argv.host}.interfaces; - FOREACH realiface IN hosts.${argv.host}.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.${argv.host}.$realiface.type.grep('^' _ argv.type _ '$').size(); - NEXT UNLESS hosts.${argv.host}.$realiface.ip; + NEXT UNLESS hosts.${argv.host}.exists(iface); + NEXT UNLESS hosts.${argv.host}.$iface.type.grep('^' _ argv.type _ '$').size(); + NEXT UNLESS hosts.${argv.host}.$iface.ip; - out.push(hosts.${argv.host}.$realiface.ip); - END; + out.push(hosts.${argv.host}.$iface.ip); END; END; out = out.sort; diff --git a/lib/get_all_shared_ips b/lib/get_all_shared_ips index 4345d196..c8fbe4c7 100644 --- a/lib/get_all_shared_ips +++ b/lib/get_all_shared_ips @@ -34,16 +34,14 @@ FOREACH host IN hosts.keys.sort; NEXT IF blktmp.processed_hosts.$host.defined; FOREACH iface IN hosts.$host.interfaces; - FOREACH realiface IN hosts.$host.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.$host.$realiface.type.grep('^' _ argv.type _ '$').size(); + NEXT UNLESS hosts.$host.exists(iface); + NEXT UNLESS hosts.$host.$iface.type.grep('^' _ argv.type _ '$').size(); - peer = hosts.$host.peer; - blktmp.processed_hosts.$peer = 1; - FOREACH ip IN hosts.$host.$realiface.shared_ip; - NEXT UNLESS ip; - out.push(ip); - END; + peer = hosts.$host.peer; + blktmp.processed_hosts.$peer = 1; + FOREACH ip IN hosts.$host.$iface.shared_ip; + NEXT UNLESS ip; + out.push(ip); END; END; END; diff --git a/lib/get_all_shared_ips_for_host b/lib/get_all_shared_ips_for_host index 973a8590..3da23a11 100644 --- a/lib/get_all_shared_ips_for_host +++ b/lib/get_all_shared_ips_for_host @@ -33,14 +33,12 @@ END; out = []; IF status.item(hosts.${argv.host}.status); FOREACH iface IN hosts.${argv.host}.interfaces; - FOREACH realiface IN hosts.${argv.host}.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.${argv.host}.$realiface.type.grep('^' _ argv.type _ '$').size(); - - FOREACH ip IN hosts.${argv.host}.$realiface.shared_ip; - NEXT UNLESS ip; - out.push(ip); - END; + NEXT UNLESS hosts.${argv.host}.exists(iface); + NEXT UNLESS hosts.${argv.host}.$iface.type.grep('^' _ argv.type _ '$').size(); + + FOREACH ip IN hosts.${argv.host}.$iface.shared_ip; + NEXT UNLESS ip; + out.push(ip); END; END; END; diff --git a/lib/get_all_shared_v6ips b/lib/get_all_shared_v6ips index 19222744..0d550d83 100644 --- a/lib/get_all_shared_v6ips +++ b/lib/get_all_shared_v6ips @@ -35,19 +35,17 @@ FOREACH host IN hosts.keys.sort; NEXT IF blktmp.processed_hosts.$host.defined; FOREACH iface IN hosts.$host.interfaces; - FOREACH realiface IN hosts.$host.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.$host.$realiface.type.grep('^' _ argv.type _ '$').size(); + NEXT UNLESS hosts.$host.exists(iface); + NEXT UNLESS hosts.$host.$iface.type.grep('^' _ argv.type _ '$').size(); - peer = hosts.$host.peer; - blktmp.processed_hosts.$peer = 1; - FOREACH ip IN hosts.$host.$realiface.shared_v6ip; - IF argv.format == 'expand'; - ip = ngcp.net_ip_expand(ip); - END; - NEXT UNLESS ip; - out.push(ip); + peer = hosts.$host.peer; + blktmp.processed_hosts.$peer = 1; + FOREACH ip IN hosts.$host.$iface.shared_v6ip; + IF argv.format == 'expand'; + ip = ngcp.net_ip_expand(ip); END; + NEXT UNLESS ip; + out.push(ip); END; END; END; diff --git a/lib/get_all_shared_v6ips_for_host b/lib/get_all_shared_v6ips_for_host index 75303a0c..97eb3886 100644 --- a/lib/get_all_shared_v6ips_for_host +++ b/lib/get_all_shared_v6ips_for_host @@ -34,17 +34,15 @@ END; out = []; IF status.item(hosts.${argv.host}.status); FOREACH iface IN hosts.${argv.host}.interfaces; - FOREACH realiface IN hosts.${argv.host}.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.${argv.host}.$realiface.type.grep('^' _ argv.type _ '$').size(); - - FOREACH ip IN hosts.${argv.host}.$realiface.shared_v6ip; - IF argv.format == 'expand'; - ip = ngcp.net_ip_expand(ip); - END; - NEXT UNLESS ip; - out.push(ip); + NEXT UNLESS hosts.${argv.host}.exists(iface); + NEXT UNLESS hosts.${argv.host}.$iface.type.grep('^' _ argv.type _ '$').size(); + + FOREACH ip IN hosts.${argv.host}.$iface.shared_v6ip; + IF argv.format == 'expand'; + ip = ngcp.net_ip_expand(ip); END; + NEXT UNLESS ip; + out.push(ip); END; END; END; diff --git a/lib/get_all_v6ips b/lib/get_all_v6ips index dcbe89f7..e74448b6 100644 --- a/lib/get_all_v6ips +++ b/lib/get_all_v6ips @@ -33,17 +33,15 @@ FOREACH host IN hosts.keys.sort; NEXT UNLESS ngcp.has_role(host, argv.role); FOREACH iface IN hosts.$host.interfaces; - FOREACH realiface IN hosts.$host.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.$host.$realiface.type.grep('^' _ argv.type _ '$').size(); + NEXT UNLESS hosts.$host.exists(iface); + NEXT UNLESS hosts.$host.$iface.type.grep('^' _ argv.type _ '$').size(); - FOREACH ip IN hosts.$host.$realiface.v6ip; - IF argv.format == 'expand'; - ip = ngcp.net_ip_expand(ip); - END; - NEXT UNLESS ip; - out.push(ip); + FOREACH ip IN hosts.$host.$iface.v6ip; + IF argv.format == 'expand'; + ip = ngcp.net_ip_expand(ip); END; + NEXT UNLESS ip; + out.push(ip); END; END; END; diff --git a/lib/get_all_v6ips_for_host b/lib/get_all_v6ips_for_host index 587bbc2e..229c1508 100644 --- a/lib/get_all_v6ips_for_host +++ b/lib/get_all_v6ips_for_host @@ -35,17 +35,15 @@ END; out = []; IF status.item(hosts.${argv.host}.status); FOREACH iface IN hosts.${argv.host}.interfaces; - FOREACH realiface IN hosts.${argv.host}.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.${argv.host}.$realiface.type.grep('^' _ argv.type _ '$').size(); - - FOREACH ip IN hosts.${argv.host}.$realiface.v6ip; - IF argv.format == 'expand'; - ip = ngcp.net_ip_expand(ip); - END; - NEXT UNLESS ip; - out.push(ip); + NEXT UNLESS hosts.${argv.host}.exists(iface); + NEXT UNLESS hosts.${argv.host}.$iface.type.grep('^' _ argv.type _ '$').size(); + + FOREACH ip IN hosts.${argv.host}.$iface.v6ip; + IF argv.format == 'expand'; + ip = ngcp.net_ip_expand(ip); END; + NEXT UNLESS ip; + out.push(ip); END; END; END; diff --git a/lib/get_default_pingnodes b/lib/get_default_pingnodes index a0474a87..f2041a5b 100644 --- a/lib/get_default_pingnodes +++ b/lib/get_default_pingnodes @@ -11,25 +11,20 @@ host = ngcp.get_hostname(); out = []; FOREACH iface IN hosts.${host}.interfaces; - FOREACH realiface IN hosts.${host}.keys; - IF realiface == iface; - IF hosts.${host}.$realiface.gateway; + NEXT UNLESS hosts.${host}.exists(iface); + NEXT UNLESS hosts.${host}.$iface.gateway; - IF argv.gw == 'yes'; - out.push(hosts.${host}.$realiface.gateway); - END; - - IF argv.dns == 'yes'; - FOREACH dns_server_ip IN hosts.${host}.${realiface}.dns_nameservers; - out.push(dns_server_ip); - END; - END; + IF argv.gw == 'yes'; + out.push(hosts.${host}.$iface.gateway); + END; - END; + IF argv.dns == 'yes'; + FOREACH dns_server_ip IN hosts.${host}.${iface}.dns_nameservers; + out.push(dns_server_ip); END; END; END; out = out.sort; --%] \ No newline at end of file +-%] diff --git a/lib/get_iface b/lib/get_iface index 019f515a..c9d2fb35 100644 --- a/lib/get_iface +++ b/lib/get_iface @@ -14,13 +14,11 @@ END; out = ''; FOREACH iface IN hosts.${argv.host}.interfaces; - FOREACH realiface IN hosts.${argv.host}.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.${argv.host}.$realiface.type.grep('^' _ argv.type _ '$').size(); + NEXT UNLESS hosts.${argv.host}.exists(iface); + NEXT UNLESS hosts.${argv.host}.$iface.type.grep('^' _ argv.type _ '$').size(); - out = realiface; - RETURN; - END; + out = iface; + RETURN; END; -%] diff --git a/lib/get_network_for_host b/lib/get_network_for_host index 249fb042..5524fcfa 100644 --- a/lib/get_network_for_host +++ b/lib/get_network_for_host @@ -14,24 +14,20 @@ END; out = ''; FOREACH iface IN hosts.${argv.host}.interfaces; - FOREACH realiface IN hosts.${argv.host}.keys; - NEXT IF realiface != iface; - NEXT UNLESS hosts.${argv.host}.$realiface.type.grep('^' _ argv.type _ '$').size(); + NEXT UNLESS hosts.${argv.host}.exists(iface); + NEXT UNLESS hosts.${argv.host}.$iface.type.grep('^' _ argv.type _ '$').size(); - IF hosts.${argv.host}.$realiface.ip AND - hosts.${argv.host}.$realiface.netmask; --%] -[% PERL -%] + IF hosts.${argv.host}.$iface.ip AND + hosts.${argv.host}.$iface.netmask; + PERL -%] use Net::Netmask; - my $network = Net::Netmask->new("[% hosts.${argv.host}.${realiface}.ip %]/[% hosts.${argv.host}.${realiface}.netmask %]"); + my $network = Net::Netmask->new("[% hosts.${argv.host}.${iface}.ip %]/[% hosts.${argv.host}.${iface}.netmask %]"); $stash->set(out => $network->desc); -[% END -%] -[% - END; - - RETURN; +[% END; END; + + RETURN; END; -%]