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 | 106 +++++++++++++++++++++---------------
 lib/portage/versions.py     |   3 +
 2 files changed, 66 insertions(+), 43 deletions(-)

diff --git a/lib/portage/dep/__init__.py b/lib/portage/dep/__init__.py
index 72988357a..4d91a411a 100644
--- a/lib/portage/dep/__init__.py
+++ b/lib/portage/dep/__init__.py
@@ -23,6 +23,7 @@ portage.proxy.lazyimport.lazyimport(globals(),
        'portage.util:cmp_sort_key,writemsg',
 )
 
+from functools import lru_cache
 from portage import _encodings, _unicode_decode, _unicode_encode
 from portage.eapi import _get_eapi_attrs
 from portage.exception import InvalidAtom, InvalidData, InvalidDependString
@@ -404,49 +405,9 @@ 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 +730,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 = tuple(uselist)
+       if masklist is not None:
+               masklist = tuple(masklist)
+       if excludeall is not None:
+               excludeall = tuple(excludeall)
+       if subset is not None:
+               subset = tuple(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..9ede67230 100644
--- a/lib/portage/versions.py
+++ b/lib/portage/versions.py
@@ -25,6 +25,7 @@ portage.proxy.lazyimport.lazyimport(globals(),
        'portage.repository.config:_gen_valid_repo',
        'portage.util:cmp_sort_key',
 )
+from functools import lru_cache
 from portage import _unicode_decode
 from portage.eapi import _get_eapi_attrs
 from portage.exception import InvalidData
@@ -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.383.g050319c2ae-goog


Reply via email to