]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
OPTIM: sample: don't check casts for samples of same type
authorWilly Tarreau <w@1wt.eu>
Sun, 15 Sep 2024 10:40:25 +0000 (12:40 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 15 Sep 2024 10:43:56 +0000 (12:43 +0200)
Originally when converters were created, they were mostly for casting
types. Nowadays we have many artithmetic converters to perform operations
on integers, and a number of converters operating on strings. Both of
these categories most often do not need any cast since the input and
output types are the same, which is visible as the cast function is
c_none. However, profiling shows that when heavily using arithmetic
converters, it's possible to spend up to ~7% of the time in
sample_process_cnv(), a good part of which is only in accessing the
sample_casts[] array. Simply avoiding this lookup when input and ouput
types are equal saves about 2% CPU on such setups doing intensive use
of converters.

src/sample.c

index 61d51fa36dc59eaaa916ae3ecb93a8fbd0bdf156..087367da356030c500b93bc53ba7f66cedac63f5 100644 (file)
@@ -1318,12 +1318,14 @@ int sample_process_cnv(struct sample_expr *expr, struct sample *p)
                 *  - c_none => nothing to do (let's optimize it)
                 *  - other  => apply cast and prepare to fail
                 */
-               if (!sample_casts[p->data.type][conv_expr->conv->in_type])
-                       return 0;
+               if (p->data.type != conv_expr->conv->in_type) {
+                       if (!sample_casts[p->data.type][conv_expr->conv->in_type])
+                               return 0;
 
-               if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
-                   !sample_casts[p->data.type][conv_expr->conv->in_type](p))
-                       return 0;
+                       if (sample_casts[p->data.type][conv_expr->conv->in_type] != c_none &&
+                           !sample_casts[p->data.type][conv_expr->conv->in_type](p))
+                               return 0;
+               }
 
                /* OK cast succeeded */
 
@@ -1690,12 +1692,14 @@ struct sample *sample_fetch_as_type(struct proxy *px, struct session *sess,
                return NULL;
        }
 
-       if (!sample_casts[smp->data.type][smp_type])
-               return NULL;
+       if (smp->data.type != smp_type) {
+               if (!sample_casts[smp->data.type][smp_type])
+                       return NULL;
 
-       if (sample_casts[smp->data.type][smp_type] != c_none &&
-           !sample_casts[smp->data.type][smp_type](smp))
-               return NULL;
+               if (sample_casts[smp->data.type][smp_type] != c_none &&
+                   !sample_casts[smp->data.type][smp_type](smp))
+                       return NULL;
+       }
 
        smp->flags &= ~SMP_F_MAY_CHANGE;
        return smp;