[PATCH python] Optimize callback lookup in event handlers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

The event handler code currently invokes PyImport_ImportModule
which is very heavyweight. This is not in fact required, since
we know the libvirt module has already been imported. We can
thus use PyImport_ImportModuleNoBlock and do away with the
global variables caching the imported module reference.

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 libvirt-override.c | 35 +++++++++++------------------------
 1 file changed, 11 insertions(+), 24 deletions(-)

diff --git a/libvirt-override.c b/libvirt-override.c
index 03aab89..aaee6b8 100644
--- a/libvirt-override.c
+++ b/libvirt-override.c
@@ -4929,39 +4929,26 @@ cleanup:
  * Helper functions to avoid importing modules
  * for every callback
  *******************************************/
-static PyObject *libvirt_module    = NULL;
-static PyObject *libvirt_dict      = NULL;
 
 static PyObject *
-getLibvirtModuleObject(void) {
-    if (libvirt_module)
-        return libvirt_module;
-
-    // PyImport_ImportModule returns a new reference
-    /* Bogus (char *) cast for RHEL-5 python API brokenness */
-    libvirt_module = PyImport_ImportModule((char *)"libvirt");
-    if (!libvirt_module) {
-        DEBUG("%s Error importing libvirt module\n", __FUNCTION__);
+getLibvirtDictObject(void) {
+    PyObject *libvirt_mod;
+    PyObject *libvirt_dict;
+
+    libvirt_mod = PyImport_ImportModuleNoBlock("libvirt");
+    if (!libvirt_mod) {
+        DEBUG("%s Error finding libvirt in imports\n",
+	      __FUNCTION__);
         PyErr_Print();
         return NULL;
     }
-
-    return libvirt_module;
-}
-
-static PyObject *
-getLibvirtDictObject(void) {
-    if (libvirt_dict)
-        return libvirt_dict;
-
-    // PyModule_GetDict returns a borrowed reference
-    libvirt_dict = PyModule_GetDict(getLibvirtModuleObject());
+    libvirt_dict = PyModule_GetDict(libvirt_mod);
     if (!libvirt_dict) {
-        DEBUG("%s Error importing libvirt dictionary\n", __FUNCTION__);
+        DEBUG("%s Error finding libvirt dict\n",
+	      __FUNCTION__);
         PyErr_Print();
         return NULL;
     }
-
     Py_INCREF(libvirt_dict);
     return libvirt_dict;
 }
-- 
1.8.3.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]