From 5dacb45afceac2d05ea597755c1ca5a1b62cc0fd Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 24 Nov 2020 15:15:59 +0000 Subject: [PATCH] database: Avoid merging the same data twice 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 --- src/database.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/database.c b/src/database.c index 5546091..f1f6ae0 100644 --- a/src/database.c +++ b/src/database.c @@ -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; } -- 2.39.2