Gentoo Archives: gentoo-portage-dev

From: Chun-Yu Shei <cshei@××××××.com>
To: gentoo-portage-dev@l.g.o
Cc: Chun-Yu Shei <cshei@××××××.com>
Subject: [gentoo-portage-dev] [PATCH 2/3] Add caching to use_reduce function
Date: Sat, 27 Jun 2020 06:34:32
Message-Id: 20200627063415.936177-3-cshei@google.com
In Reply to: [gentoo-portage-dev] Add caching to a few commonly used functions by Chun-Yu Shei
1 This function is called extremely frequently with similar arguments, so
2 this optimization reduces "emerge -uDvpU --with-bdeps=y @world" runtime from
3 43.5 -> 34.5s -- a 25.8% speedup.
4 ---
5 lib/portage/dep/__init__.py | 26 ++++++++++++++++++++++++++
6 1 file changed, 26 insertions(+)
7
8 diff --git a/lib/portage/dep/__init__.py b/lib/portage/dep/__init__.py
9 index 72988357a..df296dd81 100644
10 --- a/lib/portage/dep/__init__.py
11 +++ b/lib/portage/dep/__init__.py
12 @@ -404,6 +404,8 @@ def paren_enclose(mylist, unevaluated_atom=False, opconvert=False):
13 mystrparts.append(x)
14 return " ".join(mystrparts)
15
16 +_use_reduce_cache = {}
17 +
18 def use_reduce(depstr, uselist=(), masklist=(), matchall=False, excludeall=(), is_src_uri=False, \
19 eapi=None, opconvert=False, flat=False, is_valid_flag=None, token_class=None, matchnone=False,
20 subset=None):
21 @@ -440,6 +442,27 @@ def use_reduce(depstr, uselist=(), masklist=(), matchall=False, excludeall=(), i
22 @rtype: List
23 @return: The use reduced depend array
24 """
25 + uselist_key = None
26 + masklist_key = None
27 + excludeall_key = None
28 + subset_key = None
29 + if uselist is not None:
30 + uselist_key = tuple(uselist)
31 + if masklist is not None:
32 + masklist_key = tuple(masklist)
33 + if excludeall is not None:
34 + excludeall_key = tuple(excludeall)
35 + if subset is not None:
36 + subset_key = tuple(subset)
37 + cache_key = (depstr, uselist_key, masklist_key, matchall, excludeall_key, \
38 + is_src_uri, eapi, opconvert, flat, is_valid_flag, token_class, \
39 + matchnone, subset_key)
40 +
41 + cache_entry = _use_reduce_cache.get(cache_key)
42 + if cache_entry is not None:
43 + # The list returned by this function may be modified, so return a copy.
44 + return cache_entry[:]
45 +
46 if isinstance(depstr, list):
47 if portage._internal_caller:
48 warnings.warn(_("Passing paren_reduced dep arrays to %s is deprecated. " + \
49 @@ -767,6 +790,9 @@ def use_reduce(depstr, uselist=(), masklist=(), matchall=False, excludeall=(), i
50 raise InvalidDependString(
51 _("Missing file name at end of string"))
52
53 + # The list returned by this function may be modified, so store a copy.
54 + _use_reduce_cache[cache_key] = stack[0][:]
55 +
56 return stack[0]
57
58 def dep_opconvert(deplist):
59 --
60 2.27.0.212.ge8ba1cc988-goog