Skip to content

Commit

Permalink
speedup schema cache by avoiding providedBy call if not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Oct 1, 2019
1 parent c8d7916 commit 5547ef2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
2 changes: 2 additions & 0 deletions news/113.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Performance enhancement in schema cache by factor ~1.5.
[jensens]
32 changes: 24 additions & 8 deletions plone/dexterity/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import functools
import logging
import six
import types
import warnings

Expand Down Expand Up @@ -50,15 +51,30 @@ def volatile(func):
@functools.wraps(func)
def decorator(self, portal_type):
"""lookup fti from portal_type and cache
input can be either a portal_type as string or as the utility instance.
return value is always a FTI-ultiliy or None
"""
if portal_type is not None:
if IDexterityFTI.providedBy(portal_type):
fti = portal_type
else:
fti = queryUtility(IDexterityFTI, name=portal_type)
# this function is called very often

# shortcut None input
if portal_type is None:
return func(self, None)
# if its a string lookup fti
if isinstance(portal_type, six.string_types):
# looking up a utility is expensive
fti = queryUtility(IDexterityFTI, name=portal_type)
if fti is None:
return func(self, None)
elif IDexterityFTI.providedBy(portal_type):
# its already an IDexterityFTI instance
fti = portal_type
else:
fti = None
if fti is not None and self.cache_enabled:
raise ValueError(
'portal_type has to either string or IDexterityFTI instance but is '
'{0!r}'.format(portal_type)
)
if self.cache_enabled:
key = '_v_schema_%s' % func.__name__
cache = getattr(fti, key, _MARKER)
if cache is not _MARKER:
Expand All @@ -68,7 +84,7 @@ def decorator(self, portal_type):

value = func(self, fti)

if fti is not None and self.cache_enabled:
if self.cache_enabled:
setattr(fti, key, (fti._p_mtime, value))

return value
Expand Down

0 comments on commit 5547ef2

Please sign in to comment.