From ecc7edda9e1acc43fcf6effc72120e54bda4a655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Helleu?= Date: Wed, 23 Oct 2019 19:41:43 +0200 Subject: core: optimize search of options in configuration sections Since options are sorted in sections, it is faster to search from the last option to the first one. For configuration files with many options in a single section (like plugins.conf), the load of configuration file is about 2 to 3x faster. --- src/core/wee-config-file.c | 48 ++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'src/core') diff --git a/src/core/wee-config-file.c b/src/core/wee-config-file.c index 5b014c27d..5787c3cc8 100644 --- a/src/core/wee-config-file.c +++ b/src/core/wee-config-file.c @@ -416,12 +416,13 @@ config_file_option_find_pos (struct t_config_section *section, const char *name) if (section && name) { - for (ptr_option = section->options; ptr_option; - ptr_option = ptr_option->next_option) + for (ptr_option = section->last_option; ptr_option; + ptr_option = ptr_option->prev_option) { - if (string_strcasecmp (name, ptr_option->name) < 0) - return ptr_option; + if (string_strcasecmp (name, ptr_option->name) >= 0) + return ptr_option->next_option; } + return section->options; } /* position not found (we will add to the end of list) */ @@ -832,14 +833,18 @@ config_file_search_option (struct t_config_file *config_file, { struct t_config_section *ptr_section; struct t_config_option *ptr_option; + int rc; if (section) { - for (ptr_option = section->options; ptr_option; - ptr_option = ptr_option->next_option) + for (ptr_option = section->last_option; ptr_option; + ptr_option = ptr_option->prev_option) { - if (string_strcasecmp (ptr_option->name, option_name) == 0) + rc = string_strcasecmp (ptr_option->name, option_name); + if (rc == 0) return ptr_option; + else if (rc < 0) + return NULL; } } else if (config_file) @@ -847,11 +852,14 @@ config_file_search_option (struct t_config_file *config_file, for (ptr_section = config_file->sections; ptr_section; ptr_section = ptr_section->next_section) { - for (ptr_option = ptr_section->options; ptr_option; - ptr_option = ptr_option->next_option) + for (ptr_option = ptr_section->last_option; ptr_option; + ptr_option = ptr_option->prev_option) { - if (string_strcasecmp (ptr_option->name, option_name) == 0) + rc = string_strcasecmp (ptr_option->name, option_name); + if (rc == 0) return ptr_option; + else if (rc < 0) + return NULL; } } } @@ -876,21 +884,25 @@ config_file_search_section_option (struct t_config_file *config_file, { struct t_config_section *ptr_section; struct t_config_option *ptr_option; + int rc; *section_found = NULL; *option_found = NULL; if (section) { - for (ptr_option = section->options; ptr_option; - ptr_option = ptr_option->next_option) + for (ptr_option = section->last_option; ptr_option; + ptr_option = ptr_option->prev_option) { - if (string_strcasecmp (ptr_option->name, option_name) == 0) + rc = string_strcasecmp (ptr_option->name, option_name); + if (rc == 0) { *section_found = section; *option_found = ptr_option; return; } + else if (rc < 0) + return; } } else if (config_file) @@ -898,14 +910,18 @@ config_file_search_section_option (struct t_config_file *config_file, for (ptr_section = config_file->sections; ptr_section; ptr_section = ptr_section->next_section) { - for (ptr_option = ptr_section->options; ptr_option; - ptr_option = ptr_option->next_option) + for (ptr_option = ptr_section->last_option; ptr_option; + ptr_option = ptr_option->prev_option) { - if (string_strcasecmp (ptr_option->name, option_name) == 0) + rc = string_strcasecmp (ptr_option->name, option_name); + if (rc == 0) { *section_found = ptr_section; *option_found = ptr_option; + return; } + else if (rc < 0) + return; } } } -- cgit v1.2.3