]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
OPTIM: vars: use multiple name heads in the vars struct
authorWilly Tarreau <w@1wt.eu>
Sun, 15 Sep 2024 11:06:08 +0000 (13:06 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 15 Sep 2024 21:51:51 +0000 (23:51 +0200)
Given that the original list-based version was using a list head as the
root of the variables, while the tree is using a single pointer, it made
sense to reuse that space to place multiple roots, indexed on the lower
bits of the name hash. Two roots slightly increase the performance level,
but the best gain is obtained with 4 roots. The performance is now always
above that of the list, even with small counts, and with 100 vars, it's
21% higher than before, or 67% higher than with the list.

We keep the same lock (it could have made sense to use one lock per head),
because most of the variables in large configs are attached to a stream
or a session, hence are not shared between threads. Thus there's no point
in sharding the pointer.

include/haproxy/vars-t.h
include/haproxy/vars.h
src/vars.c

index 3ad8c73ac6ae8ab1be4ac5ef0eb99c3213bfe508..7a709bda20e20318e4a807d2ca30928d3f4bfb05 100644 (file)
@@ -48,8 +48,9 @@ enum vars_scope {
        SCOPE_CHECK,
 };
 
+#define VAR_NAME_ROOTS 4
 struct vars {
-       struct ceb_node *name_root;
+       struct ceb_node *name_root[VAR_NAME_ROOTS];
        enum vars_scope scope;
        unsigned int size;
        __decl_thread(HA_RWLOCK_T rwlock);
index 86dcf2d46ef1375d4e9a9bd9d7243614b53a3e97..bfadee5edb94d833974c977bb4ae5cc55d61cb2f 100644 (file)
@@ -83,10 +83,13 @@ static inline void vars_prune(struct vars *vars, struct session *sess, struct st
        struct ceb_node *node;
        struct var *var;
        unsigned int size = 0;
+       int i;
 
-       while ((node = cebu64_first(&vars->name_root))) {
-               var = container_of(node, struct var, node);
-               size += var_clear(vars, var, 1);
+       for (i = 0; i < VAR_NAME_ROOTS; i++) {
+               while ((node = cebu64_first(&vars->name_root[i]))) {
+                       var = container_of(node, struct var, node);
+                       size += var_clear(vars, var, 1);
+               }
        }
 
        if (!size)
index 6ba82b7e05c0deb55939d464376c203b37986cc0..7c4bdd4badfae0b359ee8e394fdf6eaa9ed61ee0 100644 (file)
@@ -190,7 +190,7 @@ unsigned int var_clear(struct vars *vars, struct var *var, int force)
        var->data.type = SMP_T_ANY;
 
        if (!(var->flags & VF_PERMANENT) || force) {
-               cebu64_delete(&vars->name_root, &var->node);
+               cebu64_delete(&vars->name_root[var->name_hash % VAR_NAME_ROOTS], &var->node);
                pool_free(var_pool, var);
                size += sizeof(struct var);
        }
@@ -205,10 +205,13 @@ void vars_prune_per_sess(struct vars *vars)
        struct ceb_node *node;
        struct var *var;
        unsigned int size = 0;
+       int i;
 
-       while ((node = cebu64_first(&vars->name_root))) {
-               var = container_of(node, struct var, node);
-               size += var_clear(vars, var, 1);
+       for (i = 0; i < VAR_NAME_ROOTS; i++) {
+               while ((node = cebu64_first(&vars->name_root[i]))) {
+                       var = container_of(node, struct var, node);
+                       size += var_clear(vars, var, 1);
+               }
        }
 
        if (!size)
@@ -223,7 +226,7 @@ void vars_prune_per_sess(struct vars *vars)
 /* This function initializes a variables list head */
 void vars_init_head(struct vars *vars, enum vars_scope scope)
 {
-       vars->name_root = NULL;
+       memset(vars->name_root, 0, sizeof(vars->name_root));
        vars->scope = scope;
        vars->size = 0;
        HA_RWLOCK_INIT(&vars->rwlock);
@@ -333,7 +336,7 @@ static struct var *var_get(struct vars *vars, uint64_t name_hash)
 {
        struct ceb_node *node;
 
-       node = cebu64_lookup(&vars->name_root, name_hash);
+       node = cebu64_lookup(&vars->name_root[name_hash % VAR_NAME_ROOTS], name_hash);
        if (node)
                return container_of(node, struct var, node);
 
@@ -436,7 +439,7 @@ int var_set(const struct var_desc *desc, struct sample *smp, uint flags)
                var->name_hash = desc->name_hash;
                var->flags = flags & VF_PERMANENT;
                var->data.type = SMP_T_ANY;
-               cebu64_insert(&vars->name_root, &var->node);
+               cebu64_insert(&vars->name_root[var->name_hash % VAR_NAME_ROOTS], &var->node);
        }
 
        /* A variable of type SMP_T_ANY is considered as unset (either created