[master] c0a9d7319 Optimize VRE_sub() by avoiding VSB_putc()

Nils Goroll nils.goroll at uplex.de
Mon Feb 27 14:49:03 UTC 2023


commit c0a9d7319793ff4d7ffe0756163cffbc09b93c37
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Wed Feb 22 16:13:16 2023 +0100

    Optimize VRE_sub() by avoiding VSB_putc()
    
    We keep s as a pointer to the start of an unaltered section and
    move e to be able to call VSB_bcat() when a backslash is encountered
    or substitution is complete.

diff --git a/lib/libvarnish/vre.c b/lib/libvarnish/vre.c
index 96b7ef677..c0f87c0c4 100644
--- a/lib/libvarnish/vre.c
+++ b/lib/libvarnish/vre.c
@@ -276,7 +276,7 @@ VRE_sub(const vre_t *code, const char *subject, const char *replacement,
 	txt groups[10];
 	size_t count;
 	int i, offset = 0;
-	const char *s;
+	const char *s, *e;
 	unsigned x;
 
 	CHECK_OBJ_NOTNULL(code, VRE_MAGIC);
@@ -301,21 +301,21 @@ VRE_sub(const vre_t *code, const char *subject, const char *replacement,
 		/* Copy prefix to match */
 		s = subject + offset;
 		VSB_bcat(vsb, s, pdiff(s, groups[0].b));
-		for (s = replacement; *s != '\0'; s++ ) {
-			if (*s != '\\' || s[1] == '\0') {
-				VSB_putc(vsb, *s);
+		for (s = e = replacement; *e != '\0'; e++ ) {
+			if (*e != '\\' || e[1] == '\0')
 				continue;
-			}
-			s++;
-			if (isdigit(*s)) {
-				x = *s - '0';
+			VSB_bcat(vsb, s, pdiff(s, e));
+			s = ++e;
+			if (isdigit(*e)) {
+				s++;
+				x = *e - '0';
 				if (x >= count)
 					continue;
 				VSB_bcat(vsb, groups[x].b, Tlen(groups[x]));
 				continue;
 			}
-			VSB_putc(vsb, *s);
 		}
+		VSB_bcat(vsb, s, pdiff(s, e));
 		offset = pdiff(subject, groups[0].e);
 		if (!all)
 			break;


More information about the varnish-commit mailing list