From d21310351fefa08e67616a82bac8d425271a4ec1 Mon Sep 17 00:00:00 2001 From: Justin Turner Arthur Date: Fri, 15 Jun 2018 19:07:21 -0500 Subject: [PATCH] Optimize by precompiling regex and using tuple for iterable copies. --- gunicorn/reloader.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/gunicorn/reloader.py b/gunicorn/reloader.py index 4ab868e9..c8798854 100644 --- a/gunicorn/reloader.py +++ b/gunicorn/reloader.py @@ -10,6 +10,8 @@ import sys import time import threading +COMPILED_EXT_RE = re.compile(r'py[co]$') + class Reloader(threading.Thread): def __init__(self, extra_files=None, interval=1, callback=None): @@ -26,8 +28,8 @@ class Reloader(threading.Thread): def get_files(self): fnames = [ - re.sub('py[co]$', 'py', module.__file__) - for module in list(sys.modules.values()) + COMPILED_EXT_RE.sub('py', module.__file__) + for module in tuple(sys.modules.values()) if getattr(module, '__file__', None) ] @@ -92,8 +94,8 @@ if has_inotify: def get_dirs(self): fnames = [ - os.path.dirname(re.sub('py[co]$', 'py', module.__file__)) - for module in list(sys.modules.values()) + os.path.dirname(COMPILED_EXT_RE.sub('py', module.__file__)) + for module in tuple(sys.modules.values()) if hasattr(module, '__file__') ]