[PATCH] Speed up repeat() for larger counts

From: ChenhuiMo <chenhuimo(dot)mch(at)qq(dot)com>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: [PATCH] Speed up repeat() for larger counts
Date: 2026-07-26 05:30:32
Message-ID: tencent_C5BBECF985A270FBC49463EDAF722CD5E005@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
0001-speed-up-repeat-for-larger-counts.patch application/octet-stream 1.7 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message ChenhuiMo 2026-07-26 05:43:51 Re: [PATCH v4] Make NumericVar storage semantics explicit
Previous Message Mats Kindahl 2026-07-26 05:11:06 Re: pg_rewind does not rewind diverging timelines