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

Consolidate nested conditional branches

Depending how a conditional is wrote, its nested conditional can be
consolidated into its own expression by using the `and` or `or` boolean
operators for the same effect. These kinds of refactors give processors
better chances at branch prediction and would make these conditionals
less expensive than they might have been.

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

 lib/portage/checksum.py      |  5 ++---
 lib/portage/data.py          |  9 ++-------
 lib/portage/dispatch_conf.py | 18 +++++++++++-------
 3 files changed, 15 insertions(+), 17 deletions(-)

diff --git a/lib/portage/checksum.py b/lib/portage/checksum.py
index 3b5a10d24..d7e800c47 100644
--- a/lib/portage/checksum.py
+++ b/lib/portage/checksum.py
@@ -427,9 +427,8 @@ class _hash_filter:
         for token in self._tokens:
             if token in matches:
                 return True
-            if token[:1] == "-":
-                if token[1:] in matches:
-                    return False
+            if token[:1] == "-" and token[1:] in matches:
+                return False
         return False
 
 

diff --git a/lib/portage/data.py b/lib/portage/data.py
index de7d88e5e..b73fa8882 100644
--- a/lib/portage/data.py
+++ b/lib/portage/data.py
@@ -158,15 +158,12 @@ def _get_global(k):
                 unprivileged = _unprivileged_mode(eroot_or_parent, eroot_st)
 
         v = 0
-        if uid == 0:
-            v = 2
-        elif unprivileged:
+        if uid == 0 or unprivileged:
             v = 2
         elif _get_global("portage_gid") in os.getgroups():
             v = 1
 
     elif k in ("portage_gid", "portage_uid"):
-
         # Discover the uid and gid of the portage user/group
         keyerror = False
         try:
@@ -357,9 +354,7 @@ def _init(settings):
 
     if "secpass" not in _initialized_globals:
         v = 0
-        if uid == 0:
-            v = 2
-        elif "unprivileged" in settings.features:
+        if uid == 0 or "unprivileged" in settings.features:
             v = 2
         elif portage_gid in os.getgroups():
             v = 1

diff --git a/lib/portage/dispatch_conf.py b/lib/portage/dispatch_conf.py
index c89caf087..d682a9ad0 100644
--- a/lib/portage/dispatch_conf.py
+++ b/lib/portage/dispatch_conf.py
@@ -238,13 +238,17 @@ def rcs_archive(archive, curconf, newconf, mrgconf):
 
         _archive_copy(mystat, newconf, archive)
 
-        if has_branch:
-            if mrgconf and os.path.isfile(archive) and os.path.isfile(mrgconf):
-                # This puts the results of the merge into mrgconf.
-                ret = os.system(f"rcsmerge -p -r{RCS_BRANCH} '{archive}' > 
'{mrgconf}'")
-                os.chmod(mrgconf, mystat.st_mode)
-                os.chown(mrgconf, mystat.st_uid, mystat.st_gid)
-        os.rename(archive, archive + ".dist.new")
+        if (
+            has_branch
+            and mrgconf
+            and os.path.isfile(archive)
+            and os.path.isfile(mrgconf)
+        ):
+            # This puts the results of the merge into mrgconf.
+            ret = os.system(f"rcsmerge -p -r{RCS_BRANCH} '{archive}' > 
'{mrgconf}'")
+            os.chmod(mrgconf, mystat.st_mode)
+            os.chown(mrgconf, mystat.st_uid, mystat.st_gid)
+        os.rename(archive, f"{archive}.dist.new")
 
     return ret
 

Reply via email to