From de87d249e3f55c38062e563821af9fa32d15e341 Mon Sep 17 00:00:00 2001 From: Laurenz Albe Date: Sat, 17 Feb 2024 17:23:39 +0100 Subject: [PATCH v1 2/3] Speed up uuid_out Since the size of the string representation of an uuid is fixed, there is no benefit in using a StringInfo. Avoiding the overhead makes the function substantially faster. --- src/backend/utils/adt/uuid.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/backend/utils/adt/uuid.c b/src/backend/utils/adt/uuid.c index 73dfd711c7..b48bbf01e1 100644 --- a/src/backend/utils/adt/uuid.c +++ b/src/backend/utils/adt/uuid.c @@ -53,10 +53,11 @@ uuid_out(PG_FUNCTION_ARGS) { pg_uuid_t *uuid = PG_GETARG_UUID_P(0); static const char hex_chars[] = "0123456789abcdef"; - StringInfoData buf; + char *buf, *p; int i; - initStringInfo(&buf); + buf = palloc(2 * UUID_LEN + 5); + p = buf; for (i = 0; i < UUID_LEN; i++) { int hi; @@ -68,16 +69,17 @@ uuid_out(PG_FUNCTION_ARGS) * ("-"). Therefore, add the hyphens at the appropriate places here. */ if (i == 4 || i == 6 || i == 8 || i == 10) - appendStringInfoChar(&buf, '-'); + *p++ = '-'; hi = uuid->data[i] >> 4; lo = uuid->data[i] & 0x0F; - appendStringInfoChar(&buf, hex_chars[hi]); - appendStringInfoChar(&buf, hex_chars[lo]); + *p++ = hex_chars[hi]; + *p++ = hex_chars[lo]; } + *p = '\0'; - PG_RETURN_CSTRING(buf.data); + PG_RETURN_CSTRING(buf); } /* -- 2.43.2