https://fedorahosted.org/freeipa/ticket/1336

Lazy initialization of ipalib plugins is used under all contexts, not only when context = cli. Every loaded plugin is pre-finalized - a flag is set, which means, that the plugin needs to be finalized. Then every call of plugin's __gettattr__ checks the flag and finalizes the plugin if necessary. The code was implemented by jcholast. Time reduction of commands execution is quite markable:

patch [s] |   normal [s]    |   command
-----------------------------------------------------------------------
1.468 | 2.287 | ipa user-add jsmith --firt=john --last=smith
1.658      |       2.235       |   ipa user-del jsmith
1.624      |       2.204       |   ipa dnsrecord-find example.com

--
Regards,

Ondrej Hamada
FreeIPA team
jabber: oh...@jabbim.cz
IRC: ohamada

diff --git a/ipalib/plugable.py b/ipalib/plugable.py
index b0e4156..2aed1cd 100644
--- a/ipalib/plugable.py
+++ b/ipalib/plugable.py
@@ -173,6 +173,7 @@ class Plugin(ReadOnly):
     """
 
     label = None
+    __try_finalize = False
 
     def __init__(self):
         self.__api = None
@@ -208,6 +209,16 @@ class Plugin(ReadOnly):
                 )
             )
 
+    def __getattribute__(self, name):
+        if name.startswith('_Plugin__') or name.startswith('_ReadOnly__'):
+            return object.__getattribute__(self, name)
+        if self.__try_finalize:
+            self.__try_finalize = False
+            self.finalize()
+            if not is_production_mode(self.__api):
+                assert islocked(self) is True
+        return object.__getattribute__(self, name)
+
     def __get_api(self):
         """
         Return `API` instance passed to `finalize()`.
@@ -217,6 +228,9 @@ class Plugin(ReadOnly):
         return self.__api
     api = property(__get_api)
 
+    def prefinalize(self):
+        self.__try_finalize = True
+
     def finalize(self):
         """
         """
@@ -638,9 +652,7 @@ class API(DictProxy):
                 assert p.instance.api is self
 
         for p in plugins.itervalues():
-            p.instance.finalize()
-            if not production_mode:
-                assert islocked(p.instance) is True
+            p.instance.prefinalize()
         object.__setattr__(self, '_API__finalized', True)
         tuple(PluginInfo(p) for p in plugins.itervalues())
         object.__setattr__(self, 'plugins',
_______________________________________________
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Reply via email to