codegooglecom / libproxy

Automatically exported from code.google.com/p/libproxy
GNU Lesser General Public License v2.1
0 stars 0 forks source link

importing libproxy.py too slow #177

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It takes about 300ms to import the library (python startup overhead excluded).  
The problem is that ctypes.util.find_library() is extremely slow, does not 
cache results, and is called 4 times.  We know we need libc.so.6 and 
libproxy.so.1 so these can be tried first.  This cuts the import time down to 
some 20ms.  find_library() is a fallback, should the versions ever change.

--- libproxy.py.orig
+++ libproxy.py
@@ -25,21 +25,23 @@

 import sys

+def _load(name, *versions):
+    for ver in versions:
+        try: return ctypes.cdll.LoadLibrary('lib%s.so.%s' % (name, ver))
+        except: pass
+    name_ver = ctypes.util.find_library(name)
+    if name_ver:
+        return ctypes.cdll.LoadLibrary(name_ver)
+    raise ImportError("Unable to find %s library" % name)
+
 # Load C library
 if platform.system() == "Windows":
     _libc = ctypes.cdll.msvcrt
 else:
-    if not ctypes.util.find_library("c"):
-        raise ImportError("Unable to import C Library!?!")
-    _libc = ctypes.cdll.LoadLibrary(ctypes.util.find_library("c"))
-
+    _libc = _load("c", 6)

 # Load libproxy
-if not ctypes.util.find_library("proxy"):
-    raise ImportError("Unable to import libproxy!?!?")
-
-
-_libproxy = ctypes.cdll.LoadLibrary(ctypes.util.find_library("proxy"))
+_libproxy = _load("proxy", 1)
 _libproxy.px_proxy_factory_get_proxies.restype = ctypes.POINTER(ctypes.c_void_p)

 class ProxyFactory(object):

Original issue reported on code.google.com by zde...@gmail.com on 15 Mar 2012 at 8:15

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r839.

Original comment by dominiqu...@gmail.com on 1 Apr 2012 at 12:42