Skip to content

Commit c2d0e67

Browse files
authored
Speed up code for arm architecture when creating new rules (#341)
* speed up code for arm architecture when creating new rules * moved dict to static class variable
1 parent 9c9b4b7 commit c2d0e67

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

iptc/ip4tc.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@
4242

4343
def is_table_available(name):
4444
try:
45+
if name in Table.existing_table_names:
46+
return Table.existing_table_names[name]
4547
Table(name)
48+
Table.existing_table_names[name] = True
4649
return True
4750
except IPTCError:
4851
pass
52+
Table.existing_table_names[name] = False
4953
return False
5054

5155

@@ -669,6 +673,11 @@ class Target(IPTCModule):
669673
does not take any value in the iptables extension, an empty string i.e. ""
670674
should be used.
671675
"""
676+
677+
STANDARD_TARGETS = ["", "ACCEPT", "DROP", "REJECT", "RETURN", "REDIRECT", "SNAT", "DNAT", \
678+
"MASQUERADE", "MIRROR", "TOS", "MARK", "QUEUE", "LOG"]
679+
"""This is the constant for all standard targets."""
680+
672681
def __init__(self, rule, name=None, target=None, revision=None, goto=None):
673682
"""
674683
*rule* is the Rule object this match belongs to; it can be changed
@@ -784,6 +793,8 @@ def _create_buffer(self, target):
784793
self.reset()
785794

786795
def _is_standard_target(self):
796+
if self._name in Target.STANDARD_TARGETS:
797+
return False
787798
for t in self._rule.tables:
788799
if t.is_chain(self._name):
789800
return True
@@ -1572,6 +1583,8 @@ class Table(object):
15721583
"""This is the constant for all tables."""
15731584

15741585
_cache = dict()
1586+
existing_table_names = dict()
1587+
"""Dictionary to check faster if a table is available."""
15751588

15761589
def __new__(cls, name, autocommit=None):
15771590
obj = Table._cache.get(name, None)

iptc/ip6tc.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919

2020
def is_table6_available(name):
2121
try:
22+
if name in Table6.existing_table_names:
23+
return Table6.existing_table_names[name]
2224
Table6(name)
25+
Table6.existing_table_names[name] = True
2326
return True
2427
except IPTCError:
2528
pass
29+
Table6.existing_table_names[name] = False
2630
return False
2731

2832

@@ -572,6 +576,8 @@ class Table6(Table):
572576
"""This is the constant for all tables."""
573577

574578
_cache = dict()
579+
existing_table_names = dict()
580+
"""Dictionary to check faster if a table is available."""
575581

576582
def __new__(cls, name, autocommit=None):
577583
obj = Table6._cache.get(name, None)

0 commit comments

Comments
 (0)