2aacef
From 471e4ee0b4fa9c7e9a5ea875fbf22de77fdd25d0 Mon Sep 17 00:00:00 2001
2aacef
From: Yu Watanabe <watanabe.yu+github@gmail.com>
2aacef
Date: Thu, 17 Nov 2022 12:46:45 +0900
2aacef
Subject: [PATCH] core/unit: merge two loops into one
2aacef
2aacef
No functional change, just refactoring.
2aacef
2aacef
(cherry picked from commit 4b7918a65cc2571a2b3fc166229e1b8db463e217)
2aacef
2aacef
Related: #2160477
2aacef
---
2aacef
 src/core/unit.c | 47 +++++++++++++++--------------------------------
2aacef
 1 file changed, 15 insertions(+), 32 deletions(-)
2aacef
2aacef
diff --git a/src/core/unit.c b/src/core/unit.c
2aacef
index dbbf818622..6b49edc2de 100644
2aacef
--- a/src/core/unit.c
2aacef
+++ b/src/core/unit.c
2aacef
@@ -1047,7 +1047,6 @@ static void unit_merge_dependencies(Unit *u, Unit *other) {
2aacef
         Hashmap *deps;
2aacef
         void *dt; /* Actually of type UnitDependency, except that we don't bother casting it here,
2aacef
                    * since the hashmaps all want it as void pointer. */
2aacef
-        int r;
2aacef
 
2aacef
         assert(u);
2aacef
         assert(other);
2aacef
@@ -1074,6 +1073,8 @@ static void unit_merge_dependencies(Unit *u, Unit *other) {
2aacef
                 if (!other_deps)
2aacef
                         break; /* done! */
2aacef
 
2aacef
+                deps = hashmap_get(u->dependencies, dt);
2aacef
+
2aacef
                 /* Now iterate through all dependencies of this dependency type, of 'other'. We refer to the
2aacef
                  * referenced units as 'back'. */
2aacef
                 HASHMAP_FOREACH_KEY(di_back.data, back, other_deps) {
2aacef
@@ -1084,6 +1085,7 @@ static void unit_merge_dependencies(Unit *u, Unit *other) {
2aacef
                                 /* This is a dependency pointing back to the unit we want to merge with?
2aacef
                                  * Suppress it (but warn) */
2aacef
                                 unit_maybe_warn_about_dependency(u, other->id, UNIT_DEPENDENCY_FROM_PTR(dt));
2aacef
+                                hashmap_remove(other_deps, back);
2aacef
                                 continue;
2aacef
                         }
2aacef
 
2aacef
@@ -1102,40 +1104,21 @@ static void unit_merge_dependencies(Unit *u, Unit *other) {
2aacef
                                                           di_move.origin_mask,
2aacef
                                                           di_move.destination_mask) >= 0);
2aacef
                         }
2aacef
-                }
2aacef
 
2aacef
-                /* Now all references towards 'other' of the current type 'dt' are corrected to point to
2aacef
-                 * 'u'. Lets's now move the deps of type 'dt' from 'other' to 'u'. First, let's try to move
2aacef
-                 * them per type wholesale. */
2aacef
-                r = hashmap_put(u->dependencies, dt, other_deps);
2aacef
-                if (r == -EEXIST) {
2aacef
                         /* The target unit already has dependencies of this type, let's then merge this individually. */
2aacef
-
2aacef
-                        assert_se(deps = hashmap_get(u->dependencies, dt));
2aacef
-
2aacef
-                        for (;;) {
2aacef
-                                UnitDependencyInfo di_move;
2aacef
-
2aacef
-                                /* Get first dep */
2aacef
-                                di_move.data = hashmap_steal_first_key_and_value(other_deps, (void**) &back);
2aacef
-                                if (!di_move.data)
2aacef
-                                        break; /* done */
2aacef
-                                if (back == u) {
2aacef
-                                        /* Would point back to us, ignore */
2aacef
-                                        unit_maybe_warn_about_dependency(u, other->id, UNIT_DEPENDENCY_FROM_PTR(dt));
2aacef
-                                        continue;
2aacef
-                                }
2aacef
-
2aacef
-                                assert_se(unit_per_dependency_type_hashmap_update(deps, back, di_move.origin_mask, di_move.destination_mask) >= 0);
2aacef
-                        }
2aacef
-                } else {
2aacef
-                        assert_se(r >= 0);
2aacef
-
2aacef
-                        if (hashmap_remove(other_deps, u))
2aacef
-                                unit_maybe_warn_about_dependency(u, other->id, UNIT_DEPENDENCY_FROM_PTR(dt));
2aacef
-
2aacef
-                        TAKE_PTR(other_deps);
2aacef
+                        if (deps)
2aacef
+                                assert_se(unit_per_dependency_type_hashmap_update(
2aacef
+                                                          deps,
2aacef
+                                                          back,
2aacef
+                                                          di_back.origin_mask,
2aacef
+                                                          di_back.destination_mask) >= 0);
2aacef
                 }
2aacef
+
2aacef
+                /* Now all references towards 'other' of the current type 'dt' are corrected to point to 'u'.
2aacef
+                 * Lets's now move the deps of type 'dt' from 'other' to 'u'. If the unit does not have
2aacef
+                 * dependencies of this type, let's move them per type wholesale. */
2aacef
+                if (!deps)
2aacef
+                        assert_se(hashmap_put(u->dependencies, dt, TAKE_PTR(other_deps)) >= 0);
2aacef
         }
2aacef
 
2aacef
         other->dependencies = hashmap_free(other->dependencies);