From 454f0f8680ebbded135e8575b4d9615b427fdf76 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 29 Apr 2016 17:33:29 +0200 Subject: hashmap: optimize set_put_strdup() a bit Hashing should be quicker than allocating, hence let's first check if the string already exists and only then allocate a new copy for it. --- src/basic/hashmap.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src/basic') diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index 85b8d812b3..49a0479592 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -1773,20 +1773,18 @@ int set_consume(Set *s, void *value) { int set_put_strdup(Set *s, const char *p) { char *c; - int r; assert(s); assert(p); + if (set_contains(s, (char*) p)) + return 0; + c = strdup(p); if (!c) return -ENOMEM; - r = set_consume(s, c); - if (r == -EEXIST) - return 0; - - return r; + return set_consume(s, c); } int set_put_strdupv(Set *s, char **l) { -- cgit v1.2.3-54-g00ecf