pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: ChenhuiMo <chenhuimo.mch@qq.com>
To: pgsql-hackers <pgsql-hackers@postgresql.org>
Subject: [PATCH] Speed up repeat() for larger counts
Date: Sun, 26 Jul 2026 13:30:32 +0800
Message-ID: <tencent_C5BBECF985A270FBC49463EDAF722CD5E005@qq.com> (raw)

Hi all,

This patch optimizes repeat(text, int).

The current implementation constructs the result by copying the source text
once per repetition.  For larger repeat counts, that leads to many small
memcpy() calls.  This patch keeps the existing linear path for small
repeat counts, and uses a doubling strategy for larger ones by repeatedly
copying the already-generated prefix of the destination buffer.

In local testing, this reduced total runtime in a small repeat()-focused
microbenchmark, with the largest gains appearing in medium and large repeat
count cases.  Small-count cases did not show obvious regressions after
keeping the original path below a small cutoff.

The patch is attached.

Thanks for your time, and any feedback would be appreciated.

Best regards,
Chenhui Mo

Attachments:

0001-speed-up-repeat-for-larger-counts.patch (application/octet-stream, 1.7K) download From d9011a17edca0915401c3a658947a35f0b8e338f Mon Sep 17 00:00:00 2001 From: Chenhui Mo <chenhuimo.mch@qq.com> Date: Tue, 16 Jun 2026 11:28:06 +0800 Subject: [PATCH] speed up repeat() for larger counts Use the existing linear copy path for small repeat counts, but switch to a doubling copy strategy for larger ones. This reduces the number of memcpy() calls needed to build the result string, improving repeat() performance for larger counts while preserving the existing behavior for small counts. --- src/backend/utils/adt/oracle_compat.c | 28 +++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/adt/oracle_compat.c b/src/backend/utils/adt/oracle_compat.c index 5b0d098bd07..bb20c502576 100644 --- a/src/backend/utils/adt/oracle_compat.c +++ b/src/backend/utils/adt/oracle_compat.c @@ -1150,7 +1150,6 @@ repeat(PG_FUNCTION_ARGS) text *result; int slen, tlen; - int i; char *cp, *sp; @@ -1171,11 +1170,36 @@ repeat(PG_FUNCTION_ARGS) SET_VARSIZE(result, tlen); cp = VARDATA(result); sp = VARDATA_ANY(string); - for (i = 0; i < count; i++) + + if (count == 0 || slen == 0) + PG_RETURN_TEXT_P(result); + + if (count < 8) + { + int i; + for (i = 0; i < count; i++) + { + memcpy(cp, sp, slen); + cp += slen; + CHECK_FOR_INTERRUPTS(); + } + } + else { + int curcount = 1; memcpy(cp, sp, slen); cp += slen; CHECK_FOR_INTERRUPTS(); + + while (curcount < count) + { + int chunk = Min(curcount, count - curcount); + + memcpy(cp, VARDATA(result), chunk * slen); + cp += chunk * slen; + curcount += chunk; + CHECK_FOR_INTERRUPTS(); + } } PG_RETURN_TEXT_P(result); -- 2.43.0

view thread (11+ messages)  latest in thread

Message-ID: <tencent_C5BBECF985A270FBC49463EDAF722CD5E005@qq.com>
Permalink:  ../tencent_C5BBECF985A270FBC49463EDAF722CD5E005@qq.com/
Also on:    postgresql.org/message-id/tencent_C5BBECF985A270FBC49463EDAF722CD5E005@qq.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: chenhuimo.mch@qq.com
  Subject: Re: [PATCH] Speed up repeat() for larger counts
  In-Reply-To: <tencent_C5BBECF985A270FBC49463EDAF722CD5E005@qq.com>
  References: <tencent_C5BBECF985A270FBC49463EDAF722CD5E005@qq.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox