From 353348a7e26212d596c846461cef49670b8b225d Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 11 Dec 2018 20:43:22 +0100 Subject: array-set: Optimize. * lib/gl_array_set.c (gl_array_search, gl_array_remove): Test equals_fn outside the loop, not inside the loop. --- ChangeLog | 6 ++++++ lib/gl_array_set.c | 50 ++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 82e3a499cc..787d538cef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2018-12-11 Bruno Haible + + array-set: Optimize. + * lib/gl_array_set.c (gl_array_search, gl_array_remove): Test equals_fn + outside the loop, not inside the loop. + 2018-12-11 Bruno Haible times: Fix tests. diff --git a/lib/gl_array_set.c b/lib/gl_array_set.c index dd77ef13be..8e985affc8 100644 --- a/lib/gl_array_set.c +++ b/lib/gl_array_set.c @@ -79,11 +79,22 @@ gl_array_search (gl_set_t set, const void *elt) { gl_setelement_equals_fn equals = set->base.equals_fn; const void **elements = set->elements; - size_t i; + if (equals != NULL) + { + size_t i; - for (i = 0; i < count; i++) - if (equals != NULL ? equals (elements[i], elt) : elements[i] == elt) - return true; + for (i = 0; i < count; i++) + if (equals (elements[i], elt)) + return true; + } + else + { + size_t i; + + for (i = 0; i < count; i++) + if (elements[i] == elt) + return true; + } } return false; } @@ -155,14 +166,29 @@ gl_array_remove (gl_set_t set, const void *elt) { gl_setelement_equals_fn equals = set->base.equals_fn; const void **elements = set->elements; - size_t i; - - for (i = 0; i < count; i++) - if (equals != NULL ? equals (elements[i], elt) : elements[i] == elt) - { - gl_array_remove_at (set, i); - return true; - } + + if (equals != NULL) + { + size_t i; + + for (i = 0; i < count; i++) + if (equals (elements[i], elt)) + { + gl_array_remove_at (set, i); + return true; + } + } + else + { + size_t i; + + for (i = 0; i < count; i++) + if (elements[i] == elt) + { + gl_array_remove_at (set, i); + return true; + } + } } return false; } -- cgit v1.2.1