Each of these functions is called repeatedly with the same arguments many times. Cache sizes were selected to minimize memory use increase, while still providing about the same speedup compared to a cache with unbounded size. "emerge -uDvpU --with-bdeps=y @world" runtime decreases from 44.32s -> 29.94s -- a 48% speedup, while the maximum value of the RES column in htop increases from 280 MB -> 290 MB.
"emerge -ep @world" time slightly decreases from 18.77s -> 17.93, while max observed RES value actually decreases from 228 MB -> 214 MB (similar values observed across a few before/after runs). --- lib/portage/dep/__init__.py | 107 +++++++++++++++++++++--------------- lib/portage/versions.py | 3 + 2 files changed, 67 insertions(+), 43 deletions(-) diff --git a/lib/portage/dep/__init__.py b/lib/portage/dep/__init__.py index 72988357a..314338f7c 100644 --- a/lib/portage/dep/__init__.py +++ b/lib/portage/dep/__init__.py @@ -17,6 +17,7 @@ __all__ = [ import re, sys import warnings +from functools import lru_cache import portage portage.proxy.lazyimport.lazyimport(globals(), @@ -404,49 +405,10 @@ def paren_enclose(mylist, unevaluated_atom=False, opconvert=False): mystrparts.append(x) return " ".join(mystrparts) -def use_reduce(depstr, uselist=(), masklist=(), matchall=False, excludeall=(), is_src_uri=False, \ - eapi=None, opconvert=False, flat=False, is_valid_flag=None, token_class=None, matchnone=False, - subset=None): - """ - Takes a dep string and reduces the use? conditionals out, leaving an array - with subarrays. All redundant brackets are removed. - - @param depstr: depstring - @type depstr: String - @param uselist: Sequence of use enabled flags - @type uselist: Sequence - @param masklist: Sequence of masked flags (always treated as disabled) - @type masklist: Sequence - @param matchall: Treat all conditionals as active. Used by repoman. - @type matchall: Bool - @param excludeall: Sequence of flags for which negated conditionals are always treated as inactive. - @type excludeall: Sequence - @param is_src_uri: Indicates if depstr represents a SRC_URI - @type is_src_uri: Bool - @param eapi: Indicates the EAPI the dep string has to comply to - @type eapi: String - @param opconvert: Put every operator as first element into it's argument list - @type opconvert: Bool - @param flat: Create a flat list of all tokens - @type flat: Bool - @param is_valid_flag: Function that decides if a given use flag might be used in use conditionals - @type is_valid_flag: Function - @param token_class: Convert all non operator tokens into this class - @type token_class: Class - @param matchnone: Treat all conditionals as inactive. Used by digestgen(). - @type matchnone: Bool - @param subset: Select a subset of dependencies conditional on the given flags - @type subset: Sequence - @rtype: List - @return: The use reduced depend array - """ - if isinstance(depstr, list): - if portage._internal_caller: - warnings.warn(_("Passing paren_reduced dep arrays to %s is deprecated. " + \ - "Pass the original dep string instead.") % \ - ('portage.dep.use_reduce',), DeprecationWarning, stacklevel=2) - depstr = paren_enclose(depstr) - +@lru_cache(1024) +def _use_reduce_cached(depstr, uselist, masklist, matchall, excludeall, \ + is_src_uri, eapi, opconvert, flat, is_valid_flag, token_class, \ + matchnone,subset): if opconvert and flat: raise ValueError("portage.dep.use_reduce: 'opconvert' and 'flat' are mutually exclusive") @@ -769,6 +731,65 @@ def use_reduce(depstr, uselist=(), masklist=(), matchall=False, excludeall=(), i return stack[0] +def use_reduce(depstr, uselist=(), masklist=(), matchall=False, excludeall=(), is_src_uri=False, \ + eapi=None, opconvert=False, flat=False, is_valid_flag=None, token_class=None, matchnone=False, + subset=None): + """ + Takes a dep string and reduces the use? conditionals out, leaving an array + with subarrays. All redundant brackets are removed. + + @param depstr: depstring + @type depstr: String + @param uselist: Sequence of use enabled flags + @type uselist: Sequence + @param masklist: Sequence of masked flags (always treated as disabled) + @type masklist: Sequence + @param matchall: Treat all conditionals as active. Used by repoman. + @type matchall: Bool + @param excludeall: Sequence of flags for which negated conditionals are always treated as inactive. + @type excludeall: Sequence + @param is_src_uri: Indicates if depstr represents a SRC_URI + @type is_src_uri: Bool + @param eapi: Indicates the EAPI the dep string has to comply to + @type eapi: String + @param opconvert: Put every operator as first element into it's argument list + @type opconvert: Bool + @param flat: Create a flat list of all tokens + @type flat: Bool + @param is_valid_flag: Function that decides if a given use flag might be used in use conditionals + @type is_valid_flag: Function + @param token_class: Convert all non operator tokens into this class + @type token_class: Class + @param matchnone: Treat all conditionals as inactive. Used by digestgen(). + @type matchnone: Bool + @param subset: Select a subset of dependencies conditional on the given flags + @type subset: Sequence + @rtype: List + @return: The use reduced depend array + """ + if isinstance(depstr, list): + if portage._internal_caller: + warnings.warn(_("Passing paren_reduced dep arrays to %s is deprecated. " + \ + "Pass the original dep string instead.") % \ + ('portage.dep.use_reduce',), DeprecationWarning, stacklevel=2) + depstr = paren_enclose(depstr) + + if uselist is not None: + uselist = frozenset(uselist) + if masklist is not None: + masklist = frozenset(masklist) + if excludeall is not None: + excludeall = frozenset(excludeall) + if subset is not None: + subset = frozenset(subset) + + result = _use_reduce_cached(depstr, uselist, masklist, matchall, \ + excludeall, is_src_uri, eapi, opconvert, flat, is_valid_flag, \ + token_class, matchnone, subset) + + # The list returned by this function may be modified, so return a copy. + return result[:] + def dep_opconvert(deplist): """ Iterate recursively through a list of deps, if the diff --git a/lib/portage/versions.py b/lib/portage/versions.py index 0c21373cc..100c8b84d 100644 --- a/lib/portage/versions.py +++ b/lib/portage/versions.py @@ -13,6 +13,7 @@ __all__ = [ import re import sys import warnings +from functools import lru_cache if sys.hexversion < 0x3000000: _unicode = unicode @@ -116,6 +117,7 @@ def ververify(myver, silent=1): print(_("!!! syntax error in version: %s") % myver) return False +@lru_cache(1024) def vercmp(ver1, ver2, silent=1): """ Compare two versions @@ -313,6 +315,7 @@ def _pkgsplit(mypkg, eapi=None): _cat_re = re.compile('^%s$' % _cat, re.UNICODE) _missing_cat = 'null' +@lru_cache(10240) def catpkgsplit(mydata, silent=1, eapi=None): """ Takes a Category/Package-Version-Rev and returns a list of each. -- 2.27.0.389.gc38d7665816-goog