]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: stick-table: switch to rdlock in stktable_lookup() and lookup_key()
authorWilly Tarreau <w@1wt.eu>
Tue, 11 Oct 2022 13:42:54 +0000 (15:42 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 12 Oct 2022 12:19:05 +0000 (14:19 +0200)
These functions do not modify anything in the the table except the refcount
on success. Let's just lock the table for shared accesses and make use of
atomic ops to update the refcount. This brings a nice gain from 425k to
455k under 48 threads (7%), but some contention remains on the exclusive
locks in other parts.

Note that the refcount continues to be updated under the lock because it's
not yet certain whether there are races between it and some of the exclusive
lock on the table. The difference is marginal and we prefer to stay on the
safe side for now.

src/stick_table.c

index 95913b12560cae5dbc006cca1c8e5eb87cf96f50..44a115fd1ac58988876f98b970e9fb1d7f85c9b6 100644 (file)
@@ -335,11 +335,11 @@ struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key
 {
        struct stksess *ts;
 
-       HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
+       HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
        ts = __stktable_lookup_key(t, key);
        if (ts)
-               ts->ref_cnt++;
-       HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
+               HA_ATOMIC_INC(&ts->ref_cnt);
+       HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
 
        return ts;
 }
@@ -373,11 +373,11 @@ struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
 {
        struct stksess *lts;
 
-       HA_RWLOCK_WRLOCK(STK_TABLE_LOCK, &t->lock);
+       HA_RWLOCK_RDLOCK(STK_TABLE_LOCK, &t->lock);
        lts = __stktable_lookup(t, ts);
        if (lts)
-               lts->ref_cnt++;
-       HA_RWLOCK_WRUNLOCK(STK_TABLE_LOCK, &t->lock);
+               HA_ATOMIC_INC(&lts->ref_cnt);
+       HA_RWLOCK_RDUNLOCK(STK_TABLE_LOCK, &t->lock);
 
        return lts;
 }