From: Lennart Poettering Date: Fri, 29 Apr 2016 15:33:29 +0000 (+0200) Subject: hashmap: optimize set_put_strdup() a bit X-Git-Tag: v230~106^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=454f0f8680ebbded135e8575b4d9615b427fdf76;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index 85b8d812b39..49a04795923 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) {