]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Optimize IptablesManager._find_last_entry
authorAssaf Muller <amuller@redhat.com>
Fri, 15 May 2015 21:58:13 +0000 (17:58 -0400)
committerAssaf Muller <amuller@redhat.com>
Fri, 15 May 2015 21:58:13 +0000 (17:58 -0400)
As it turns out calling .strip() thousands of times can be expensive.
I'll defer to security groups and iptables experts to try and find ways
to call the method less often, cache the results, or any other clever
trick.

Moving strip to the return statement speeds up the method by more than
x2.

Change-Id: I7522c6db50c76274bef93e0f0ea6a78d508b7fbe
Related-Bug: #1455675

neutron/agent/linux/iptables_manager.py

index ed99155d174a7dd98265d4f35feedeb4a8e1e330..f63098681f6fe7e4fb27a8df78a17affb6406228 100644 (file)
@@ -506,9 +506,8 @@ class IptablesManager(object):
     def _find_last_entry(self, filter_list, match_str):
         # find a matching entry, starting from the bottom
         for s in reversed(filter_list):
-            s = s.strip()
             if match_str in s:
-                return s
+                return s.strip()
 
     def _modify_rules(self, current_lines, table, table_name):
         # Chains are stored as sets to avoid duplicates.