From e83d07d00a93ac3902025a0588d4f7c5b74dcd5c Mon Sep 17 00:00:00 2001 From: Quan Tian Date: Mon, 21 Nov 2016 18:37:38 +0800 Subject: [PATCH] Improve performance of _modify_rules The _modify_rules method currently uses nested loop to removes rules that belong to us but don't have the wrap name. Speed up this operation by storing our rules as set. Reduce operation complexity from O(n*m) to O(n). Change-Id: I82e6184a30ddb25f2258e21fe749573af44a52ca Related-Bug: #1502297 --- neutron/agent/linux/iptables_manager.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/neutron/agent/linux/iptables_manager.py b/neutron/agent/linux/iptables_manager.py index 23657d66067..b2ffd52afd8 100644 --- a/neutron/agent/linux/iptables_manager.py +++ b/neutron/agent/linux/iptables_manager.py @@ -567,11 +567,17 @@ class IptablesManager(object): # Sort the output chains here to make their order predictable. unwrapped_chains = sorted(table.unwrapped_chains) chains = sorted(table.chains) + rules = set(map(str, table.rules)) # we don't want to change any rules that don't belong to us so we start # the new_filter with these rules + # there are some rules that belong to us but they don't have the wrap + # name. we want to add them in the right location in case our new rules + # changed the order + # (e.g. '-A FORWARD -j neutron-filter-top') new_filter = [line.strip() for line in current_lines - if self.wrap_name not in line] + if self.wrap_name not in line and + line.strip() not in rules] # generate our list of chain names our_chains = [':%s-%s' % (self.wrap_name, name) for name in chains] @@ -586,12 +592,6 @@ class IptablesManager(object): our_bottom_rules = [] for rule in table.rules: rule_str = str(rule) - # similar to the unwrapped chains, there are some rules that belong - # to us but they don't have the wrap name. we want to remove them - # from the new_filter and then add them in the right location in - # case our new rules changed the order. - # (e.g. '-A FORWARD -j neutron-filter-top') - new_filter = [s for s in new_filter if rule_str not in s] if rule.top: # rule.top == True means we want this rule to be at the top.