From nobody Fri Apr 26 04:23:31 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=patchew-devel-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1542635644873496.8242361230691; Mon, 19 Nov 2018 05:54:04 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 216CBC04B2D8; Mon, 19 Nov 2018 13:54:04 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0D21C608EC; Mon, 19 Nov 2018 13:54:04 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E9665181B9E4; Mon, 19 Nov 2018 13:54:03 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id wAJDce72025992 for ; Mon, 19 Nov 2018 08:38:40 -0500 Received: by smtp.corp.redhat.com (Postfix) id 9F4F960C67; Mon, 19 Nov 2018 13:38:40 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-56.ams2.redhat.com [10.36.112.56]) by smtp.corp.redhat.com (Postfix) with ESMTP id E28DE19694 for ; Mon, 19 Nov 2018 13:38:36 +0000 (UTC) From: Paolo Bonzini To: patchew-devel@redhat.com Date: Mon, 19 Nov 2018 14:38:35 +0100 Message-Id: <20181119133835.10660-1-pbonzini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: patchew-devel@redhat.com Subject: [Patchew-devel] [PATCH] optimize mod X-BeenThere: patchew-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Patchew development and discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: patchew-devel-bounces@redhat.com Errors-To: patchew-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Mon, 19 Nov 2018 13:54:04 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Avoid looking up the module table repeatedly every time a hook is invoked, by memoizing the list of hooks. As a small additional optimization reuse the result of get_or_create instead of querying the table again. Signed-off-by: Paolo Bonzini --- mod.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/mod.py b/mod.py index e92377a..1da12ec 100644 --- a/mod.py +++ b/mod.py @@ -26,8 +26,7 @@ class PatchewModule(object): def get_model(self): # ALways read from DB to accept configuration update in-flight from api.models import Module as PC - _module_init_config(self.__class__) - return PC.objects.get(name=3Dself.name) + return _module_init_config(self.__class__) =20 def enabled(self): try: @@ -186,14 +185,20 @@ def load_modules(): _loaded_modules[cls.name] =3D cls() print("Loaded module:", cls.name) =20 +memoized_hooks =3D {} def dispatch_module_hook(hook_name, **params): - for i in [x for x in list(_loaded_modules.values()) if x.enabled()]: - if hasattr(i, hook_name): - try: - getattr(i, hook_name)(**params) - except: - print("Cannot invoke module hook: %s.%s" % (i, hook_name)) - traceback.print_exc() + if not hook_name in memoized_hooks: + # The dictionary stores "bound method" objects + memoized_hooks[hook_name] =3D [getattr(x, hook_name) + for x in list(_loaded_modules.values()) + if x.enabled() + and hasattr(x, hook_name)] + for f in memoized_hooks[hook_name]: + try: + f(**params) + except: + print("Cannot invoke module hook: %s.%s" % (type(f.__self__), = hook_name)) + traceback.print_exc() =20 def get_module(name): return _loaded_modules.get(name) --=20 2.19.1 _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel