]> git.ipfire.org Git - location/libloc.git/commitdiff
database: Avoid merging the same data twice
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 24 Nov 2020 15:15:59 +0000 (15:15 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 24 Nov 2020 15:15:59 +0000 (15:15 +0000)
When finish splitting networks into many parts, we have
a list of subnets with the excluded subnets and merge them
together first and put them on the stack again.

This is slower than pushing it all onto the stack first
and then popping the first element.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/database.c

index 55460919206c831021d51b2c1893f9d5ad6d18d8..f1f6ae0cd11336d5965cf5b2aadeac2baf071856 100644 (file)
@@ -1360,8 +1360,8 @@ static int __loc_database_enumerator_next_network_flattened(
                return -1;
        }
 
-       // Merge excluded list with subnets
-       r = loc_network_list_merge(subnets, excluded);
+       // Merge subnets onto the stack
+       r = loc_network_list_merge(enumerator->stack, subnets);
        if (r) {
                loc_network_list_unref(subnets);
                loc_network_list_unref(excluded);
@@ -1369,17 +1369,21 @@ static int __loc_database_enumerator_next_network_flattened(
                return r;
        }
 
-       // We no longer need the excluded list
-       loc_network_list_unref(excluded);
-
-       // Replace network with the first one
-       loc_network_unref(*network);
+       // Push excluded list onto the stack
+       r = loc_network_list_merge(enumerator->stack, excluded);
+       if (r) {
+               loc_network_list_unref(subnets);
+               loc_network_list_unref(excluded);
 
-       *network = loc_network_list_pop_first(subnets);
+               return r;
+       }
 
-       // Push the rest onto the stack
-       loc_network_list_merge(enumerator->stack, subnets);
        loc_network_list_unref(subnets);
+       loc_network_list_unref(excluded);
+
+       // Replace network with the first one from the stack
+       loc_network_unref(*network);
+       *network = loc_network_list_pop_first(enumerator->stack);
 
        return 0;
 }