public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Sam James" <sam@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/portage:master commit in: lib/portage/
Date: Sun, 27 Mar 2022 23:07:10 +0000 (UTC)	[thread overview]
Message-ID: <1648422397.13740f43bcc38e787153d9efeabc4f5d878e7af0.sam@gentoo> (raw)

commit:     13740f43bcc38e787153d9efeabc4f5d878e7af0
Author:     Kenneth Raplee <kenrap <AT> kennethraplee <DOT> com>
AuthorDate: Tue Mar 22 07:22:42 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sun Mar 27 23:06:37 2022 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=13740f43

Prefer generator expressions over list comprehensions

List comprehensions do improve the performance compared to using for
loops to build the same lists in Python. However, they are eagerly
evaluated and will create a sequence ahead of time.

The problem is, a fair amount of code would only use these lists as
iterators which does not require them to be sequences. Unless there is a
need to know a list's length, access its items, or alter its size, it's
better to use generators which are lazily evaluated and can be iterated
when necessary. Avoiding the eagerness of list comprehensions will
improve the runtime performance.

Signed-off-by: Kenneth Raplee <kenrap <AT> kennethraplee.com>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 lib/portage/__init__.py      | 22 +++++++++++++---------
 lib/portage/data.py          | 25 ++++++++++++++-----------
 lib/portage/dispatch_conf.py |  2 +-
 lib/portage/eclass_cache.py  |  2 +-
 lib/portage/getbinpkg.py     | 15 ++++++++-------
 lib/portage/glsa.py          | 14 ++++++++++----
 6 files changed, 47 insertions(+), 33 deletions(-)

diff --git a/lib/portage/__init__.py b/lib/portage/__init__.py
index 82e47d0ff..e6dff28ee 100644
--- a/lib/portage/__init__.py
+++ b/lib/portage/__init__.py
@@ -555,10 +555,15 @@ _deprecated_eapis = frozenset(
         "7_pre1",
     ]
 )
+
+from itertools import chain
+
 _supported_eapis = frozenset(
-    [str(x) for x in range(portage.const.EAPI + 1)]
-    + list(_testing_eapis)
-    + list(_deprecated_eapis)
+    chain(
+        (str(x) for x in range(portage.const.EAPI + 1)),
+        _testing_eapis,
+        _deprecated_eapis,
+    )
 )
 
 
@@ -672,8 +677,7 @@ def create_trees(
         # When ROOT != "/" we only want overrides from the calling
         # environment to apply to the config that's associated
         # with ROOT != "/", so pass a nearly empty dict for the env parameter.
-        clean_env = {}
-        for k in (
+        env_sequence = (
             "PATH",
             "PORTAGE_GRPNAME",
             "PORTAGE_REPOSITORIES",
@@ -687,10 +691,10 @@ def create_trees(
             "https_proxy",
             "no_proxy",
             "__PORTAGE_TEST_HARDLINK_LOCKS",
-        ):
-            v = settings.get(k)
-            if v is not None:
-                clean_env[k] = v
+        )
+        env = ((k, settings.get(k)) for k in env_sequence)
+        clean_env = {k: v for k, v in env if v is not None}
+
         if depcachedir is not None:
             clean_env["PORTAGE_DEPCACHEDIR"] = depcachedir
         settings = config(

diff --git a/lib/portage/data.py b/lib/portage/data.py
index e1457c566..8c58ad0fc 100644
--- a/lib/portage/data.py
+++ b/lib/portage/data.py
@@ -229,27 +229,30 @@ def _get_global(k):
             # Get a list of group IDs for the portage user. Do not use
             # grp.getgrall() since it is known to trigger spurious
             # SIGPIPE problems with nss_ldap.
-            cmd = ["id", "-G", _portage_username]
-
             encoding = portage._encodings["content"]
-            cmd = [
+            cmd = (
                 portage._unicode_encode(x, encoding=encoding, errors="strict")
-                for x in cmd
-            ]
+                for x in ("id", "-G", _portage_username)
+            )
             proc = subprocess.Popen(
                 cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
             )
             myoutput = proc.communicate()[0]
             status = proc.wait()
             if os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK:
-                for x in portage._unicode_decode(
-                    myoutput, encoding=encoding, errors="strict"
-                ).split():
+
+                def check(x):
                     try:
-                        v.append(int(x))
+                        return int(x)
                     except ValueError:
-                        pass
-                v = sorted(set(v))
+                        return None
+
+                unicode_decode = portage._unicode_decode(
+                    myoutput, encoding=encoding, errors="strict"
+                )
+                checked_v = (check(x) for x in unicode_decode.split())
+                filtered_v = (x for x in checked_v if x)
+                v = sorted(set(filtered_v))
 
     # Avoid instantiating portage.settings when the desired
     # variable is set in os.environ.

diff --git a/lib/portage/dispatch_conf.py b/lib/portage/dispatch_conf.py
index 6c6036c4e..c89caf087 100644
--- a/lib/portage/dispatch_conf.py
+++ b/lib/portage/dispatch_conf.py
@@ -37,7 +37,7 @@ def diffstatusoutput(cmd, file1, file2):
     # raise a UnicodeDecodeError which makes the output inaccessible.
     args = shlex_split(cmd % (file1, file2))
 
-    args = [portage._unicode_encode(x, errors="strict") for x in args]
+    args = (portage._unicode_encode(x, errors="strict") for x in args)
     proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
     output = portage._unicode_decode(proc.communicate()[0])
     if output and output[-1] == "\n":

diff --git a/lib/portage/eclass_cache.py b/lib/portage/eclass_cache.py
index f1729326d..2545ed9b2 100644
--- a/lib/portage/eclass_cache.py
+++ b/lib/portage/eclass_cache.py
@@ -110,7 +110,7 @@ class cache:
         master_eclasses = {}
         eclass_len = len(".eclass")
         ignored_listdir_errnos = (errno.ENOENT, errno.ENOTDIR)
-        for x in [normalize_path(os.path.join(y, "eclass")) for y in self.porttrees]:
+        for x in (normalize_path(os.path.join(y, "eclass")) for y in self.porttrees):
             try:
                 eclass_filenames = os.listdir(x)
             except OSError as e:

diff --git a/lib/portage/getbinpkg.py b/lib/portage/getbinpkg.py
index 1e89119fb..bf99fd2be 100644
--- a/lib/portage/getbinpkg.py
+++ b/lib/portage/getbinpkg.py
@@ -112,10 +112,12 @@ class ParseLinks(html_parser_HTMLParser):
 
     def handle_starttag(self, tag, attrs):
         if tag == "a":
-            for x in attrs:
-                if x[0] == "href":
-                    if x[1] not in self.PL_anchors:
-                        self.PL_anchors.append(urllib_parse_unquote(x[1]))
+            myarchors = (
+                urllib_parse_unquote(x[1])
+                for x in attrs
+                if x[0] == "href" and x[1] not in self.PL_anchors
+            )
+            self.PL_anchors.extend(myarchors)
 
 
 def create_conn(baseurl, conn=None):
@@ -533,8 +535,7 @@ def file_get(
     from portage.util import varexpand
     from portage.process import spawn
 
-    myfetch = portage.util.shlex_split(fcmd)
-    myfetch = [varexpand(x, mydict=variables) for x in myfetch]
+    myfetch = (varexpand(x, mydict=variables) for x in portage.util.shlex_split(fcmd))
     fd_pipes = {
         0: portage._get_stdin().fileno(),
         1: sys.__stdout__.fileno(),
@@ -986,5 +987,5 @@ class PackageIndex:
             keys = list(metadata)
             keys.sort()
             self._writepkgindex(
-                pkgfile, [(k, metadata[k]) for k in keys if metadata[k]]
+                pkgfile, ((k, metadata[k]) for k in keys if metadata[k])
             )

diff --git a/lib/portage/glsa.py b/lib/portage/glsa.py
index 05de3ade6..4b92d52e0 100644
--- a/lib/portage/glsa.py
+++ b/lib/portage/glsa.py
@@ -126,14 +126,20 @@ def get_glsa_list(myconfig):
         return []
     dirlist = os.listdir(repository)
     prefix = "glsa-"
+    prefix_size = len(prefix)
     suffix = ".xml"
+    suffix_size = len(suffix)
 
-    for f in dirlist:
+    def check(value):
         try:
-            if f[: len(prefix)] == prefix and f[-1 * len(suffix) :] == suffix:
-                rValue.append(f[len(prefix) : -1 * len(suffix)])
+            if value[:prefix_size] == prefix and value[-suffix_size:] == suffix:
+                return value[prefix_size:-suffix_size]
         except IndexError:
-            pass
+            return None
+        return None
+
+    checked_dirlist = (check(f) for f in dirlist)
+    rValue = [f for f in checked_dirlist if f]
     return rValue
 
 


             reply	other threads:[~2022-03-27 23:07 UTC|newest]

Thread overview: 148+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-27 23:07 Sam James [this message]
  -- strict thread matches above, loose matches on Subject: below --
2025-01-21 21:14 [gentoo-commits] proj/portage:master commit in: lib/portage/ Sam James
2025-01-11 16:01 Mike Gilbert
2025-01-11 16:01 Mike Gilbert
2025-01-01  0:33 Zac Medico
2024-11-02 22:12 Zac Medico
2024-11-02 15:48 Zac Medico
2024-11-02 15:48 Zac Medico
2024-09-09 18:08 Ulrich Müller
2024-09-09 18:08 Ulrich Müller
2024-08-14 15:22 Zac Medico
2024-06-09 17:54 Zac Medico
2024-06-02 18:28 Zac Medico
2024-04-26 22:06 Sam James
2024-04-26 22:06 Sam James
2024-02-28 16:01 Sam James
2024-02-28 15:52 Sam James
2024-02-28 15:49 Sam James
2024-02-25  8:25 Sam James
2024-02-24 20:10 Zac Medico
2024-02-21  2:08 Sam James
2024-02-21  2:08 Sam James
2024-02-12  7:58 Zac Medico
2024-02-11 19:57 Zac Medico
2024-02-10  6:09 Zac Medico
2024-02-10  6:06 Zac Medico
2024-02-09  8:51 Sam James
2024-02-09  7:08 Sam James
2024-02-07  2:35 Zac Medico
2024-02-07  2:35 Zac Medico
2024-02-05  1:03 Zac Medico
2024-02-05  1:03 Zac Medico
2024-01-29 17:49 Zac Medico
2024-01-29 16:09 Zac Medico
2023-12-26 23:15 Zac Medico
2023-11-02 14:58 Zac Medico
2023-10-24 21:26 Zac Medico
2023-10-24  1:48 Zac Medico
2023-10-03 15:07 Zac Medico
2023-10-02  2:10 Zac Medico
2023-09-26  5:53 Zac Medico
2023-09-08 20:36 Sam James
2023-09-08 19:49 Sam James
2023-08-24 18:23 Mike Gilbert
2023-08-02  6:31 Sam James
2023-07-29  3:57 Sam James
2023-06-29  8:22 Sam James
2023-03-21  2:30 Sam James
2023-03-21  2:30 Sam James
2023-03-21  2:30 Sam James
2023-03-21  2:30 Sam James
2023-03-21  2:30 Sam James
2023-03-21  2:30 Sam James
2023-02-27  6:15 Sam James
2023-02-17  1:23 Sam James
2023-01-02  5:25 Sam James
2022-11-02 22:58 Sam James
2022-11-02 22:58 Sam James
2022-09-29 21:37 Sam James
2022-09-29 20:45 Sam James
2022-09-28 23:56 Sam James
2022-09-26 17:52 Zac Medico
2022-09-20 19:45 Sam James
2022-09-20  3:39 Sam James
2022-09-18 18:30 Mike Gilbert
2022-08-01 22:39 Sam James
2022-08-01 17:34 Mike Gilbert
2022-07-19 21:39 Sam James
2022-07-18 18:47 Sam James
2022-07-11 23:02 Sam James
2022-07-10 15:07 Mike Gilbert
2022-07-05 22:56 Sam James
2022-06-05 20:25 Zac Medico
2022-04-11 12:11 Mike Gilbert
2022-04-11 12:11 Mike Gilbert
2022-04-09  4:32 Sam James
2022-04-04 19:04 Sam James
2022-04-04 19:04 Sam James
2022-04-04 19:04 Sam James
2022-04-04 19:04 Sam James
2022-04-04 19:04 Sam James
2022-04-04 19:04 Sam James
2022-04-04 19:04 Sam James
2022-04-04 19:04 Sam James
2022-04-01 20:30 Matt Turner
2022-03-30 23:11 Sam James
2022-03-28  1:10 Sam James
2022-03-27 23:07 Sam James
2022-03-27 23:07 Sam James
2022-03-27 23:07 Sam James
2022-03-27 23:07 Sam James
2022-03-15  2:52 Matt Turner
2022-02-09 11:13 Sam James
2021-09-20 20:06 Zac Medico
2021-09-20 19:55 Mike Gilbert
2021-09-07  7:04 Michał Górny
2021-09-04 11:53 Michał Górny
2021-05-24  6:08 Zac Medico
2021-05-24  4:55 Zac Medico
2021-03-28  3:33 Zac Medico
2021-03-11 12:32 Zac Medico
2021-03-07 14:03 Zac Medico
2021-03-06  9:18 Zac Medico
2021-03-06  9:05 Zac Medico
2021-03-06  9:05 Zac Medico
2021-03-06  8:20 Zac Medico
2021-03-06  6:16 Zac Medico
2021-02-08  4:55 Zac Medico
2020-09-11 19:02 Zac Medico
2020-08-04  1:39 Zac Medico
2020-08-03 23:28 Zac Medico
2020-08-03 23:28 Zac Medico
2020-08-03 19:30 Zac Medico
2020-08-03 19:30 Zac Medico
2020-08-03 19:30 Zac Medico
2020-08-03 19:30 Zac Medico
2020-06-27 19:46 Zac Medico
2020-06-09  0:58 Zac Medico
2020-05-17  9:37 Michał Górny
2020-05-07 20:35 Zac Medico
2020-04-20 21:16 Mike Gilbert
2020-03-28 18:57 Michał Górny
2020-03-25 19:18 Zac Medico
2020-03-25  7:57 Zac Medico
2020-03-25  7:57 Zac Medico
2020-02-04  6:43 Zac Medico
2020-02-02  9:00 Zac Medico
2019-12-15 23:04 Zac Medico
2019-11-12 22:25 Zac Medico
2019-09-17  2:59 Zac Medico
2019-09-07  6:40 Zac Medico
2019-08-18 22:15 Zac Medico
2019-08-04 18:03 Zac Medico
2019-08-02 20:03 Mike Gilbert
2019-08-01 19:02 Mike Gilbert
2019-05-28  1:49 Zac Medico
2019-04-27 19:20 Zac Medico
2019-02-20  0:58 Zac Medico
2019-02-20  0:58 Zac Medico
2019-02-20  0:58 Zac Medico
2019-02-18  1:01 Zac Medico
2019-02-11 19:46 Zac Medico
2019-01-04  3:49 Zac Medico
2018-12-31  5:27 Zac Medico
2018-12-04  1:35 Zac Medico
2018-11-25  0:03 Zac Medico
2018-11-24 21:34 Zac Medico
2018-08-07 18:36 Zac Medico

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1648422397.13740f43bcc38e787153d9efeabc4f5d878e7af0.sam@gentoo \
    --to=sam@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox