ElementBase: micro-optimise __getitem__, hands down the most often called function
This makes it go down from 8.767s to 7.960s in a random benchmark. Remove unnecessary assignations, don’t create an OrderedDict from a dict to then convert it to a dict again, only obtain the get_method2 name if get_method wasn’t present. get_method2 (the title-case one) takes about 1/8th of the total time spent in this function, we should eliminate it as soon as possible.
This commit is contained in:
parent
2587d82af8
commit
f0f1698e46
@ -637,7 +637,7 @@ class ElementBase(object):
|
|||||||
plugin.values = value
|
plugin.values = value
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __getitem__(self, attrib):
|
def __getitem__(self, full_attrib):
|
||||||
"""Return the value of a stanza interface using dict-like syntax.
|
"""Return the value of a stanza interface using dict-like syntax.
|
||||||
|
|
||||||
Example::
|
Example::
|
||||||
@ -664,24 +664,16 @@ class ElementBase(object):
|
|||||||
8. The plugin named ``'foo'``
|
8. The plugin named ``'foo'``
|
||||||
9. An empty string.
|
9. An empty string.
|
||||||
|
|
||||||
:param string attrib: The name of the requested stanza interface.
|
:param string full_attrib: The name of the requested stanza interface.
|
||||||
"""
|
"""
|
||||||
full_attrib = attrib
|
attrib, lang, *_ = ('%s|' % full_attrib).split('|')
|
||||||
attrib_lang = ('%s|' % attrib).split('|')
|
|
||||||
attrib = attrib_lang[0]
|
|
||||||
lang = attrib_lang[1] or None
|
|
||||||
|
|
||||||
kwargs = {}
|
kwargs = {'lang': lang} if lang and attrib in self.lang_interfaces else {}
|
||||||
if lang and attrib in self.lang_interfaces:
|
|
||||||
kwargs['lang'] = lang
|
|
||||||
|
|
||||||
kwargs = OrderedDict(kwargs)
|
|
||||||
|
|
||||||
if attrib == 'substanzas':
|
if attrib == 'substanzas':
|
||||||
return self.iterables
|
return self.iterables
|
||||||
elif attrib in self.interfaces or attrib == 'lang':
|
elif attrib in self.interfaces or attrib == 'lang':
|
||||||
get_method = "get_%s" % attrib.lower()
|
get_method = "get_%s" % attrib.lower()
|
||||||
get_method2 = "get%s" % attrib.title()
|
|
||||||
|
|
||||||
if self.plugin_overrides:
|
if self.plugin_overrides:
|
||||||
name = self.plugin_overrides.get(get_method, None)
|
name = self.plugin_overrides.get(get_method, None)
|
||||||
@ -694,7 +686,8 @@ class ElementBase(object):
|
|||||||
|
|
||||||
if hasattr(self, get_method):
|
if hasattr(self, get_method):
|
||||||
return getattr(self, get_method)(**kwargs)
|
return getattr(self, get_method)(**kwargs)
|
||||||
elif hasattr(self, get_method2):
|
get_method2 = "get%s" % attrib.title()
|
||||||
|
if hasattr(self, get_method2):
|
||||||
return getattr(self, get_method2)(**kwargs)
|
return getattr(self, get_method2)(**kwargs)
|
||||||
else:
|
else:
|
||||||
if attrib in self.sub_interfaces:
|
if attrib in self.sub_interfaces:
|
||||||
|
Loading…
Reference in New Issue
Block a user