On 11/27/2014 12:36 AM, Seth Arnold wrote:
> On Wed, Nov 26, 2014 at 10:57:13PM +0100, Peter Maloney wrote:diff -ur 
> apparmor.orig/aamode.py apparmor.p1/aamode.py
> --- apparmor.orig/aamode.py   2014-07-14 20:56:26.000000000 +0200
> +++ apparmor.p1/aamode.py     2014-11-09 22:15:01.875415033 +0100
>
> ...
>
> -MODE_MAP_RE = re.compile('(r|w|l|m|k|a|x|i|u|p|c|n|I|U|P|C|N)')
> +MODE_MAP_LIST = ["r", "w", "l", "m", "k", "a", "x", "i", "u", "p", "c", "n", 
> "I", "U", "P", "C", "N"]
>  
> ...
>
> +    for mode_char in string:
> +        if mode_char not in MODE_MAP_LIST:
>              break
> ...
> The mode.split("::") speedup makes a ton of sense. I'm less convinced of
> the MOD_MAP_LIST lookup, that's a long list to iterate through. If you're
> up for more testing, I'm curious about the performance difference of only
> the :: splitting, only the MODE_MAP_LIST, and perhaps using MOD_MAP_SET =
> { 'r', 'w'. 'l', ...} as well.
>
> For just the split_log_mode() change,
> Acked-by: Seth Arnold <[email protected]>
>
>

Yes I agree that a dict would likely be faster than a list, even a small
list like that one. (attached new patch)



from timeit import timeit
MODE_MAP_LIST = ["r", "w", "l", "m", "k", "a", "x", "i", "u", "p", "c",
"n", "I", "U", "P", "C", "N"]
MODE_MAP_SET = {"r", "w", "l", "m", "k", "a", "x", "i", "u", "p", "c",
"n", "I", "U", "P", "C", "N"}
def testlist():
    for i in MODE_MAP_LIST:
        i in MODE_MAP_LIST
def testset():
    for i in MODE_MAP_LIST:
        i in MODE_MAP_SET
timeit(testlist, number=10000)
timeit(testset, number=10000)



>>> timeit(testlist, number=10000)
0.0441557650001414
>>> timeit(testset, number=10000)
0.016543965999971988

diff -ur apparmor.orig/aamode.py apparmor.p1/aamode.py
--- apparmor.orig/aamode.py	2014-07-14 20:56:26.000000000 +0200
+++ apparmor.p1/aamode.py	2014-11-09 22:15:01.875415033 +0100
@@ -68,7 +68,7 @@
              }
 
 LOG_MODE_RE = re.compile('(r|w|l|m|k|a|x|ix|ux|px|pux|cx|nx|pix|cix|Ux|Px|PUx|Cx|Nx|Pix|Cix)')
-MODE_MAP_RE = re.compile('(r|w|l|m|k|a|x|i|u|p|c|n|I|U|P|C|N)')
+MODE_MAP_SET = {"r", "w", "l", "m", "k", "a", "x", "i", "u", "p", "c", "n", "I", "U", "P", "C", "N"}
 
 def str_to_mode(string):
     if not string:
@@ -87,27 +87,23 @@
 
 def sub_str_to_mode(string):
     mode = set()
-
-    while string:
-        tmp = MODE_MAP_RE.search(string)
-        if not tmp:
+    
+    for mode_char in string:
+        if mode_char not in MODE_MAP_SET:
             break
-        string = MODE_MAP_RE.sub('', string, 1)
-
-        mode_char = tmp.groups()[0]
         if MODE_HASH.get(mode_char, False):
             mode |= MODE_HASH[mode_char]
-        else:
-            pass
 
     return mode
 
 def split_log_mode(mode):
+    #if the mode has a "::", then the left side is the user mode, and the right side is the other mode
+    #if not, then the mode is both the user and other mode
     user = ''
     other = ''
-    match = re.search('(.*?)::(.*)', mode)
-    if match:
-        user, other = match.groups()
+    
+    if "::" in mode:
+        user, other = mode.split("::")
     else:
         user = mode
         other = mode
-- 
AppArmor mailing list
[email protected]
Modify settings or unsubscribe at: 
https://lists.ubuntu.com/mailman/listinfo/apparmor

Reply via email to