From 717d8f2599fe84df5304d9161ffb40969359751f Mon Sep 17 00:00:00 2001 From: jklemm Date: Tue, 31 Oct 2023 09:31:14 +0100 Subject: [PATCH 1/2] speed up code for arm architecture when creating new rules --- iptc/ip4tc.py | 14 ++++++++++++++ iptc/ip6tc.py | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 4ddd2dc..082b227 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -40,12 +40,19 @@ xtables(NFPROTO_IPV4) +exist_table_names = dict() # Dictionary to check faster if table is available + def is_table_available(name): + global exist_table_names try: + if name in exist_table_names: + return exist_table_names[name] Table(name) + exist_table_names[name] = True return True except IPTCError: pass + exist_table_names[name] = False return False @@ -669,6 +676,11 @@ class Target(IPTCModule): does not take any value in the iptables extension, an empty string i.e. "" should be used. """ + + STANDARD_TARGETS = ["", "ACCEPT", "DROP", "REJECT", "RETURN", "REDIRECT", "SNAT", "DNAT", \ + "MASQUERADE", "MIRROR", "TOS", "MARK", "QUEUE", "LOG"] + """This is the constant for all standard targets.""" + def __init__(self, rule, name=None, target=None, revision=None, goto=None): """ *rule* is the Rule object this match belongs to; it can be changed @@ -784,6 +796,8 @@ def _create_buffer(self, target): self.reset() def _is_standard_target(self): + if self._name in Target.STANDARD_TARGETS: + return False for t in self._rule.tables: if t.is_chain(self._name): return True diff --git a/iptc/ip6tc.py b/iptc/ip6tc.py index ea4a1d7..d266330 100644 --- a/iptc/ip6tc.py +++ b/iptc/ip6tc.py @@ -17,12 +17,19 @@ _IFNAMSIZ = 16 +exist_table6_names = dict() # Dictionary to check faster if table is available + def is_table6_available(name): + global exist_table6_names try: + if name in exist_table6_names: + return exist_table6_names[name] Table6(name) + exist_table6_names[name] = True return True except IPTCError: pass + exist_table6_names[name] = False return False From f631fe014038e0f886d47a6f5f601719699a98c3 Mon Sep 17 00:00:00 2001 From: jklemm Date: Fri, 7 Mar 2025 21:23:39 +0100 Subject: [PATCH 2/2] moved dict to static class variable --- iptc/ip4tc.py | 13 ++++++------- iptc/ip6tc.py | 13 ++++++------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/iptc/ip4tc.py b/iptc/ip4tc.py index 082b227..90f5ee7 100644 --- a/iptc/ip4tc.py +++ b/iptc/ip4tc.py @@ -40,19 +40,16 @@ xtables(NFPROTO_IPV4) -exist_table_names = dict() # Dictionary to check faster if table is available - def is_table_available(name): - global exist_table_names try: - if name in exist_table_names: - return exist_table_names[name] + if name in Table.existing_table_names: + return Table.existing_table_names[name] Table(name) - exist_table_names[name] = True + Table.existing_table_names[name] = True return True except IPTCError: pass - exist_table_names[name] = False + Table.existing_table_names[name] = False return False @@ -1586,6 +1583,8 @@ class Table(object): """This is the constant for all tables.""" _cache = dict() + existing_table_names = dict() + """Dictionary to check faster if a table is available.""" def __new__(cls, name, autocommit=None): obj = Table._cache.get(name, None) diff --git a/iptc/ip6tc.py b/iptc/ip6tc.py index d266330..e0d8787 100644 --- a/iptc/ip6tc.py +++ b/iptc/ip6tc.py @@ -17,19 +17,16 @@ _IFNAMSIZ = 16 -exist_table6_names = dict() # Dictionary to check faster if table is available - def is_table6_available(name): - global exist_table6_names try: - if name in exist_table6_names: - return exist_table6_names[name] + if name in Table6.existing_table_names: + return Table6.existing_table_names[name] Table6(name) - exist_table6_names[name] = True + Table6.existing_table_names[name] = True return True except IPTCError: pass - exist_table6_names[name] = False + Table6.existing_table_names[name] = False return False @@ -579,6 +576,8 @@ class Table6(Table): """This is the constant for all tables.""" _cache = dict() + existing_table_names = dict() + """Dictionary to check faster if a table is available.""" def __new__(cls, name, autocommit=None): obj = Table6._cache.get(name, None)