From 9be7bb1212e390b356e3310c3e29e6eda7f56e59 Mon Sep 17 00:00:00 2001
From: John Naylor <john.naylor@postgresql.org>
Date: Sat, 9 Dec 2023 17:30:39 +0700
Subject: [PATCH v7 08/13] Casefold lazily in guc_name_compare

We can assume that almost all GUC names are lower case,
so compare each byte with strict equality first. If that
fails retry the comparison with down-casing.

TODO: This concept only been tested inlined into simplehash,
so need to see if this makes any difference on its own.
---
 src/backend/utils/misc/guc.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c
index 053be81d14..1484e11a42 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -1305,12 +1305,16 @@ guc_name_compare(const char *namea, const char *nameb)
 		char		cha = *namea++;
 		char		chb = *nameb++;
 
-		if (cha >= 'A' && cha <= 'Z')
-			cha += 'a' - 'A';
-		if (chb >= 'A' && chb <= 'Z')
-			chb += 'a' - 'A';
 		if (cha != chb)
-			return cha - chb;
+		{
+			/* Casefold lazily since we expect lower case */
+			if (cha >= 'A' && cha <= 'Z')
+				cha += 'a' - 'A';
+			if (chb >= 'A' && chb <= 'Z')
+				chb += 'a' - 'A';
+			if (cha != chb)
+				return cha - chb;
+		}
 	}
 	if (*namea)
 		return 1;				/* a is longer */
-- 
2.43.0

