From 61ecf2838991a659127e90c00f8302e430e5076b Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 8 Sep 2021 15:20:45 +0200 Subject: [PATCH] OPTIM: vars: only takes the variables lock on shared entries There's no point taking the variables locks for sess/txn/req/res contexts since these ones always run inside the same thread anyway. This patch conditions the lock on the variable's scope to avoid flushing cache lines when not needed. This showed an improvement of ~5% on a 16-thread machine with 12 variables. --- include/haproxy/vars.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/include/haproxy/vars.h b/include/haproxy/vars.h index 30e6fe0c88..ebd1f15790 100644 --- a/include/haproxy/vars.h +++ b/include/haproxy/vars.h @@ -41,28 +41,32 @@ int vars_unset_by_name_ifexist(const char *name, size_t len, struct sample *smp) int vars_get_by_desc(const struct var_desc *var_desc, struct sample *smp, const struct buffer *def); int vars_check_arg(struct arg *arg, char **err); -/* locks the for writes */ +/* locks the for writes if it's in a shared scope */ static inline void vars_wrlock(struct vars *vars) { - HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock); + if (vars->scope == SCOPE_PROC) + HA_RWLOCK_WRLOCK(VARS_LOCK, &vars->rwlock); } -/* unlocks the for writes */ +/* unlocks the for writes if it's in a shared scope */ static inline void vars_wrunlock(struct vars *vars) { - HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock); + if (vars->scope == SCOPE_PROC) + HA_RWLOCK_WRUNLOCK(VARS_LOCK, &vars->rwlock); } -/* locks the for reads */ +/* locks the for reads if it's in a shared scope */ static inline void vars_rdlock(struct vars *vars) { - HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock); + if (vars->scope == SCOPE_PROC) + HA_RWLOCK_RDLOCK(VARS_LOCK, &vars->rwlock); } -/* unlocks the for reads */ +/* unlocks the for reads if it's in a shared scope */ static inline void vars_rdunlock(struct vars *vars) { - HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock); + if (vars->scope == SCOPE_PROC) + HA_RWLOCK_RDUNLOCK(VARS_LOCK, &vars->rwlock); } #endif -- 2.39.5