]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
hashmap: optimize set_put_strdup() a bit
authorLennart Poettering <lennart@poettering.net>
Fri, 29 Apr 2016 15:33:29 +0000 (17:33 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 29 Apr 2016 15:35:32 +0000 (17:35 +0200)
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

index 85b8d812b3981fa7e3c55cfc224876a34b83ae34..49a04795923bc07c41adf3c8de77b95d51ca8302 100644 (file)
@@ -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) {