[Zope3-checkins] SVN: Zope3/trunk/src/zope/ Added caching of the adapter_hook method used for simple adapter

Jim Fulton jim at zope.com
Sat Jul 10 08:47:23 EDT 2004


Log message for revision 26391:
Added caching of the adapter_hook method used for simple adapter
lookup.

This provides about a 10% speedup in my test case and should simplify
debugging a little bit.



-=-
Modified: Zope3/trunk/src/zope/app/component/configure.zcml
===================================================================
--- Zope3/trunk/src/zope/app/component/configure.zcml	2004-07-10 12:46:02 UTC (rev 26390)
+++ Zope3/trunk/src/zope/app/component/configure.zcml	2004-07-10 12:47:23 UTC (rev 26391)
@@ -39,6 +39,11 @@
 
   <hook
       module="zope.component"
+      name="adapter_hook"
+      implementation="zope.app.component.hooks.adapter_hook" />
+
+  <hook
+      module="zope.component"
       name="queryView"
       implementation="zope.app.component.hooks.queryView" />
 

Modified: Zope3/trunk/src/zope/app/component/hooks.py
===================================================================
--- Zope3/trunk/src/zope/app/component/hooks.py	2004-07-10 12:46:02 UTC (rev 26390)
+++ Zope3/trunk/src/zope/app/component/hooks.py	2004-07-10 12:47:23 UTC (rev 26391)
@@ -27,42 +27,57 @@
 from zope.app.location import locate
 from zope.component.servicenames import Presentation
 from zope.interface import Interface
+from zope.component.servicenames import Adapters
 import warnings
 import zope.thread
 
-siteinfo = zope.thread.local()
+class read_property(object):
+    def __init__(self, func):
+        self.func = func
 
+    def __get__(self, inst, cls):
+        if inst is None:
+            return self
+
+        return self.func(inst)
+
+class SiteInfo(zope.thread.local):
+    site = None
+    services = serviceManager
+
+    def adapter_hook(self):
+        services = self.services
+        adapters = services.getService(Adapters)
+        adapter_hook = adapters.adapter_hook
+        self.adapter_hook = adapter_hook
+        return adapter_hook
+    
+    adapter_hook = read_property(adapter_hook)
+
+siteinfo = SiteInfo()
+
 def setSite(site=None):
     if site is None:
-        siteinfo.services = None
+        services = serviceManager
     else:
-        siteinfo.services = trustedRemoveSecurityProxy(site.getSiteManager())
+        site = trustedRemoveSecurityProxy(site)
+        services = site.getSiteManager()
 
-def getSite():
+    siteinfo.site = site
+    siteinfo.services = services
     try:
-        services = siteinfo.services
+        del siteinfo.adapter_hook
     except AttributeError:
-        services = siteinfo.services = None
-        
-    if services is None:
-        return None
-
-    return services.__parent__
+        pass
     
-
+def getSite():
+    return siteinfo.site
+    
 def getServices_hook(context=None):
 
     if context is None:
-        try:
-            services = siteinfo.services
-        except AttributeError:
-            services = siteinfo.services = None
+        return siteinfo.services
 
-        if services is None:
-            return serviceManager
-
-        return services
-
     # Deprecated support for a context that isn't adaptable to
     # IServiceService.  Return the default service manager.
     try:
@@ -70,8 +85,13 @@
                                                           serviceManager))
     except ComponentLookupError:
         return serviceManager
+
+def adapter_hook(interface, object, name='', default=None):
+    try:
+        return siteinfo.adapter_hook(interface, object, name, default)
+    except ComponentLookupError:
+        return default
     
-
 def queryView(object, name, request, default=None,
               providing=Interface, context=None):
     views = getService(Presentation, context)

Modified: Zope3/trunk/src/zope/app/tests/setup.py
===================================================================
--- Zope3/trunk/src/zope/app/tests/setup.py	2004-07-10 12:46:02 UTC (rev 26390)
+++ Zope3/trunk/src/zope/app/tests/setup.py	2004-07-10 12:47:23 UTC (rev 26391)
@@ -89,12 +89,13 @@
 
 #------------------------------------------------------------------------
 # Placeful setup
-from zope.app.component.hooks import getServices_hook
+from zope.app.component.hooks import getServices_hook, adapter_hook
 from zope.app.tests.placelesssetup import setUp as placelessSetUp
 from zope.app.tests.placelesssetup import tearDown as placelessTearDown
 def placefulSetUp(site=False):
     placelessSetUp()
     zope.component.getServices.sethook(getServices_hook)
+    zope.component.adapter_hook.sethook(adapter_hook)
     setUpAnnotations()
     setUpDependable()
     setUpTraversal()

Modified: Zope3/trunk/src/zope/component/__init__.py
===================================================================
--- Zope3/trunk/src/zope/component/__init__.py	2004-07-10 12:46:02 UTC (rev 26390)
+++ Zope3/trunk/src/zope/component/__init__.py	2004-07-10 12:47:23 UTC (rev 26391)
@@ -86,12 +86,6 @@
 getServices = hookable(getServices)
 
 def getService(name, context=None):
-    # Deprecated backwards-compatibility hack.
-    if isinstance(context, basestring) and not isinstance(name, basestring):
-        name, context = context, name
-        warnings.warn("getService(context, name) is deprecated."
-                      "  Use getService(name, context=context).",
-                      DeprecationWarning, warningLevel())
     return getServices(context).getService(name)
 
 def getServiceDefinitions(context=None):
@@ -175,26 +169,25 @@
         raise ComponentLookupError(object, interface)
     return adapter
 
-def queryAdapter(object, interface, name, default=None, context=None):
-    adapters = getService(Adapters, context)
-    return adapters.queryAdapter(object, interface, name, default)
-
-def interfaceAdapterHook(iface, ob):
+def adapter_hook(interface, object, name='', default=None):
     try:
         adapters = getService(Adapters)
     except ComponentLookupError:
         # Oh blast, no adapter service. We're probably just running
         # from a test
-        #warnings.warn("There is no adapters service.  Returning the default.",
-        #              DeprecationWarning, warningLevel())
         return None
+    return adapters.queryAdapter(object, interface, name, default)
+adapter_hook = hookable(adapter_hook)
 
-    return adapters.queryAdapter(ob, iface, '')
+import zope.interface.interface
+zope.interface.interface.adapter_hooks.append(adapter_hook)
 
-from zope.interface.interface import adapter_hooks
-adapter_hooks.append(interfaceAdapterHook)
+def queryAdapter(object, interface, name, default=None, context=None):
+    if context is None:
+        return adapter_hook(interface, object, name, default)
+    adapters = getService(Adapters, context)
+    return adapters.queryAdapter(object, interface, name, default)
 
-
 def getMultiAdapter(objects, interface, name=u'', context=None):
     adapter = queryMultiAdapter(objects, interface, name, context=context)
     if adapter is None:



More information about the Zope3-Checkins mailing list