From: Willy Tarreau Date: Wed, 21 Oct 2026 21:05:05 +0000 (+0200) Subject: OPTIM: sample: use smp_prepare_out() in the lower and upper converters X-Git-Url: https://git.kaiwu.me/web/%22data:,/doc/static/gitweb.js?a=commitdiff_plain;h=546ddc5faafe4eb3c88a26d3d04d0be54f966582;p=haproxy.git OPTIM: sample: use smp_prepare_out() in the lower and upper converters These converters used to call smp_make_rw() then convert the string in place. Most of the time their input is read-only (e.g. a header), in which case smp_dup() first copied it into a trash chunk before the conversion performed a second pass over it. By relying on smp_prepare_out() instead, the converted string is directly written into the trash chunk while reading the input, saving one pass. Writable inputs are still converted in place. --- diff --git a/src/sample.c b/src/sample.c index 06f3d75e3..bf5bc34a4 100644 --- a/src/sample.c +++ b/src/sample.c @@ -2477,28 +2477,40 @@ static int sample_conv_length(const struct arg *arg_p, struct sample *smp, void static int sample_conv_str2lower(const struct arg *arg_p, struct sample *smp, void *private) { + const char *src = smp->data.u.str.area; + char *dst; int i; - if (!smp_make_rw(smp)) + /* may alias , we never write ahead of where we read */ + if (!smp_prepare_out(smp)) return 0; + dst = smp->data.u.str.area; for (i = 0; i < smp->data.u.str.data; i++) { - if ((smp->data.u.str.area[i] >= 'A') && (smp->data.u.str.area[i] <= 'Z')) - smp->data.u.str.area[i] += 'a' - 'A'; + if ((src[i] >= 'A') && (src[i] <= 'Z')) + dst[i] = src[i] + 'a' - 'A'; + else + dst[i] = src[i]; } return 1; } static int sample_conv_str2upper(const struct arg *arg_p, struct sample *smp, void *private) { + const char *src = smp->data.u.str.area; + char *dst; int i; - if (!smp_make_rw(smp)) + /* may alias , we never write ahead of where we read */ + if (!smp_prepare_out(smp)) return 0; + dst = smp->data.u.str.area; for (i = 0; i < smp->data.u.str.data; i++) { - if ((smp->data.u.str.area[i] >= 'a') && (smp->data.u.str.area[i] <= 'z')) - smp->data.u.str.area[i] += 'A' - 'a'; + if ((src[i] >= 'a') && (src[i] <= 'z')) + dst[i] = src[i] + 'A' - 'a'; + else + dst[i] = src[i]; } return 1; }