]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/transaction: do not call job_is_conflicted_by() twice for the same job
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 31 Aug 2025 02:01:24 +0000 (11:01 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Sep 2025 13:19:14 +0000 (22:19 +0900)
The function searches the list, and it potentially takes O(n).
Let's cache the result and avoid duplicated calls.

This also rebreaks comments, and rewrites conditions in an equivalent
form that is easy to read and matches with the comment above.

No functional change, just refactoring.

src/core/transaction.c

index 0f7fe451e7fce6180106f7c8a7c22fbb179e9c30..be8d43a19b47caf6c38c8edc845c2cbd9606b16b 100644 (file)
@@ -163,39 +163,33 @@ static int delete_one_unmergeable_job(Transaction *tr, Job *job) {
                          * drop one of them */
                         if (!j->matters_to_anchor && !k->matters_to_anchor) {
 
-                                /* Both jobs don't matter, so let's
-                                 * find the one that is smarter to
-                                 * remove. Let's think positive and
-                                 * rather remove stops then starts --
-                                 * except if something is being
-                                 * stopped because it is conflicted by
-                                 * another unit in which case we
-                                 * rather remove the start. */
+                                /* Both jobs don't matter, so let's find the one that is smarter to remove.
+                                 * Let's think positive and rather remove stops than starts -- except if
+                                 * something is being stopped because it is conflicted by another unit in
+                                 * which case we rather remove the start. */
+
+                                bool j_is_conflicted_by = job_is_conflicted_by(j),
+                                        k_is_conflicted_by = job_is_conflicted_by(k);
 
                                 /* Update test/units/TEST-87-AUX-UTILS-VM.sh when logs below are changed. */
                                 log_unit_debug(j->unit,
                                                "Looking at job %s/%s conflicted_by=%s",
                                                j->unit->id, job_type_to_string(j->type),
-                                               yes_no(j->type == JOB_STOP && job_is_conflicted_by(j)));
+                                               yes_no(j->type == JOB_STOP && j_is_conflicted_by));
                                 log_unit_debug(k->unit,
                                                "Looking at job %s/%s conflicted_by=%s",
                                                k->unit->id, job_type_to_string(k->type),
-                                               yes_no(k->type == JOB_STOP && job_is_conflicted_by(k)));
-
-                                if (j->type == JOB_STOP) {
-
-                                        if (job_is_conflicted_by(j))
-                                                d = k;
-                                        else
-                                                d = j;
+                                               yes_no(k->type == JOB_STOP && k_is_conflicted_by));
 
-                                } else if (k->type == JOB_STOP) {
-
-                                        if (job_is_conflicted_by(k))
-                                                d = j;
-                                        else
-                                                d = k;
-                                } else
+                                if (j->type == JOB_STOP && j_is_conflicted_by)
+                                        d = k;
+                                else if (k->type == JOB_STOP && k_is_conflicted_by)
+                                        d = j;
+                                else if (j->type == JOB_STOP)
+                                        d = j;
+                                else if (k->type == JOB_STOP)
+                                        d = k;
+                                else
                                         d = j;
 
                         } else if (!j->matters_to_anchor)