[PATCH] Speed up repeat() for larger counts
[PATCH] Speed up repeat() for larger counts
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
Re: [PATCH] Speed up repeat() for larger counts
От:
Jeevan Chalke <jeevan.chalke@enterprisedb.com>
Дата:
On Mon, Jul 27, 2026 at 7:11 PM David Rowley <dgrowleyml@gmail.com> wrote:
On Mon, 27 Jul 2026 at 22:18, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
> Beyond certain size, I'd guess it might even become slower, if the
> string no longer fits in the L0 CPU cache for example. Also, if the
> fast-path used a constant size, like 64 bytes, maybe the compiler could
> optimize the memcpy() into a single SIMD instruction or something.
I read this earlier and wondered the same thing. The thing that might
save it would be the hardware prefetcher seeing the linear memory
access and loading the cachelines back again before there's a stall.
However, that assumes memory can keep up with a modern memcpy(), and I
believe that a good AVX512 memcpy could well outpace even DDR5 RAM
speed. Capping the chunks at something L1-sized might solve that. It
might be worth the author looking at what I did with
ESCAPE_JSON_FLUSH_AFTER in json.c. Benchmarks for that in [1]. I
expect it'll be diminishing returns with memcpy sizes beyond a handful
of kilobytes.
It does feel like a rather incomplete performance patch as it doesn't
come with any results to demonstrate that it actually has a positive
impact on performance. It would be good to see a script posted here
that tests various input lengths and repeat counts that people could
easily run to see if they get the same improvement on their hardware.
+1. While the simple example shows good performance, I agree with David that having actual performance numbers and a script would be helpful here.
David
[1] https://postgr.es/m/CAApHDvpi7FkbNenAB%2BXKPtejDmVMx7iEfxKH%3DLN_rZDGvLoGcA%40mail.gmail.com
Re:[PATCH] Speed up repeat() for larger counts
Hi Jan, Thanks for pointing this out. I updated the benchmark so that the capped strategies also use memset() for single-byte inputs. In that path, the memset() calls are split according to the selected block size, with CHECK_FOR_INTERRUPTS() between chunks. I reran the benchmark three times on the same system and saw the same overall trend in each run. The table below shows the second run. source | repeats | output | master | doubling | nocap | cap512 | cap1k | cap2k | cap4k | cap8k | cap16k | cap32k | cap64k | cap128k | cap256k | cap512k | cap1m | cap2m | cap4m | cap8m | cap16m | fastest --------+-----------+----------+----------+----------+--------+--------+--------+--------+--------+--------+--------+--------+--------+---------+---------+---------+--------+--------+--------+--------+--------+--------------------------- 32 B | 2 | 64 B | 3 ns | 0.97x | 0.89x | 0.83x | 0.83x | 0.83x | 0.83x | 0.83x | 0.86x | 0.89x | 0.89x | 0.89x | 0.89x | 0.89x | 0.89x | 0.89x | 0.89x | 0.89x | 0.89x | master (median: doubling) 32 B | 4 | 128 B | 13 ns | 1.73x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | 1.56x | doubling 32 B | 6 | 192 B | 15 ns | 1.67x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | 1.59x | doubling 32 B | 7 | 224 B | 16 ns | 1.17x | 1.12x | 1.12x | 1.12x | 1.19x | 1.19x | 1.19x | 1.19x | 1.19x | 1.19x | 1.19x | 1.19x | 1.19x | 1.19x | 1.19x | 1.19x | 1.12x | 1.14x | cap8k (median: doubling) 32 B | 8 | 256 B | 15 ns | 1.19x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | 1.15x | doubling 32 B | 9 | 288 B | 17 ns | 1.19x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | 1.16x | doubling 32 B | 12 | 384 B | 19 ns | 1.27x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | 1.25x | doubling 32 B | 16 | 512 B | 21 ns | 1.37x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | doubling 32 B | 64 | 2 KB | 65 ns | 2.45x | 2.19x | 2.19x | 2.28x | 2.19x | 2.19x | 2.19x | 2.19x | 2.20x | 2.19x | 2.19x | 2.19x | 2.19x | 2.19x | 2.19x | 2.19x | 2.19x | 2.19x | doubling 32 B | 4096 | 128 KB | 5.7 us | 2.90x | 2.90x | 2.52x | 2.83x | 3.06x | 3.36x | 3.69x | 3.73x | 2.94x | 2.91x | 2.91x | 2.91x | 2.91x | 2.91x | 2.91x | 2.91x | 2.91x | 2.90x | cap16k 1 B | 16777216 | 16 MB | 17.3 ms | 37.90x | 42.60x | 39.76x | 46.97x | 47.77x | 43.82x | 45.25x | 47.21x | 49.36x | 50.08x | 49.54x | 50.40x | 50.46x | 49.79x | 49.15x | 50.24x | 50.88x | 50.12x | cap8m (median: cap4m) 10 B | 1677721 | 16 MB | 1.4 ms | 2.47x | 2.64x | 3.39x | 3.27x | 2.87x | 3.36x | 3.54x | 3.64x | 3.22x | 3.29x | 3.22x | 3.30x | 2.88x | 2.25x | 2.28x | 2.55x | 2.30x | 2.67x | cap16k 100 B | 167772 | 16 MB | 557.6 us | 1.08x | 0.97x | 1.36x | 1.41x | 1.31x | 1.36x | 1.47x | 1.34x | 1.34x | 1.34x | 1.31x | 1.30x | 1.15x | 1.01x | 1.02x | 1.04x | 1.09x | 1.06x | cap8k (median: cap16k) 1 KB | 16384 | 16 MB | 374.2 us | 0.72x | 0.73x | 1.02x | 1.02x | 1.05x | 0.96x | 1.00x | 1.04x | 0.89x | 0.88x | 0.89x | 0.91x | 0.87x | 0.70x | 0.73x | 0.72x | 0.71x | 0.72x | cap2k (median: cap512) 64 KB | 256 | 16 MB | 525.7 us | 0.88x | 0.77x | 0.96x | 1.02x | 1.19x | 1.02x | 1.21x | 1.18x | 1.20x | 1.12x | 1.04x | 1.09x | 1.03x | 0.85x | 0.86x | 0.78x | 0.83x | 0.88x | cap8k (median: cap128k) 1 MB | 16 | 16 MB | 577.6 us | 1.01x | 1.01x | 1.00x | 1.03x | 1.00x | 1.03x | 1.02x | 1.01x | 1.04x | 1.05x | 1.03x | 1.05x | 1.06x | 1.03x | 1.09x | 1.07x | 1.09x | 1.09x | cap2m (median: cap8m) 1 B | 268435456 | 256 MB | 296.9 ms | 20.72x | 22.10x | 18.76x | 18.77x | 18.95x | 17.11x | 18.43x | 19.68x | 20.18x | 20.26x | 20.40x | 20.56x | 20.57x | 20.40x | 20.82x | 20.58x | 20.50x | 20.96x | nocap (median: cap16m) 10 B | 26843545 | 256 MB | 27.1 ms | 1.99x | 1.98x | 1.75x | 1.80x | 1.61x | 1.70x | 1.81x | 1.81x | 1.83x | 1.82x | 1.84x | 1.82x | 1.74x | 1.59x | 1.59x | 1.64x | 1.61x | 2.67x | cap16m 100 B | 2684354 | 256 MB | 16.7 ms | 1.14x | 1.19x | 1.04x | 1.09x | 0.92x | 1.02x | 1.07x | 1.10x | 1.12x | 1.14x | 1.13x | 1.12x | 1.01x | 0.99x | 0.98x | 0.99x | 0.90x | 1.36x | cap16m 1 KB | 262144 | 256 MB | 14.7 ms | 1.11x | 1.10x | 0.96x | 0.94x | 0.94x | 0.89x | 0.93x | 0.97x | 0.96x | 0.98x | 1.00x | 0.98x | 0.98x | 0.89x | 0.88x | 0.90x | 0.86x | 1.56x | cap16m 64 KB | 4096 | 256 MB | 15.3 ms | 1.14x | 1.16x | 0.98x | 0.98x | 0.98x | 1.00x | 0.98x | 0.99x | 1.03x | 0.98x | 1.04x | 1.02x | 1.00x | 0.92x | 0.94x | 0.94x | 0.89x | 1.66x | cap16m 1 MB | 256 | 256 MB | 16.6 ms | 1.25x | 1.22x | 0.98x | 1.00x | 1.00x | 0.98x | 0.99x | 0.99x | 1.01x | 0.99x | 0.99x | 0.99x | 1.00x | 0.99x | 0.97x | 0.97x | 0.97x | 1.95x | cap16m 1 B | 1000 | 1000 B | 1.1 us | 97.38x | 93.94x | 77.97x | 93.95x | 93.91x | 93.93x | 93.95x | 93.91x | 93.94x | 93.95x | 93.95x | 93.92x | 93.95x | 93.91x | 93.96x | 93.93x | 93.95x | 93.94x | doubling 1 B | 1000000 | 976.6 KB | 1.1 ms | 78.84x | 79.60x | 52.97x | 60.48x | 69.16x | 71.61x | 75.43x | 78.24x | 79.51x | 79.80x | 80.01x | 80.62x | 80.66x | 80.86x | 80.95x | 80.92x | 81.06x | 80.86x | cap8m (median: cap16m) (24 rows) The corrected single-byte results do not change the overall conclusion. For multi-byte inputs, the preferred cap on this machine still varies noticeably with the total output size. In particular, the 16 MB cases tend to prefer relatively small caps, while the 256 MB cases consistently prefer cap16m. For the single-byte cases, splitting the memset() into capped chunks also does not seem to introduce a significant performance penalty. In some cases, the larger capped memset variants are even slightly faster than a single large memset(). I have attached the updated repeatbench.c. The other extension files are unchanged. I am also curious to see what results others get on different systems. Thanks, Chenhui
Re: Re:[PATCH] Speed up repeat() for larger counts
От:
Jan Nidzwetzki <jan@planetscale.com>
Дата:
Hello Chenhui, On 03.09.26 17:20, ChenhuiMo wrote: [...] > > I updated the benchmark so that the capped strategies also use memset() for > single-byte inputs. In that path, the memset() calls are split according to > the selected block size, with CHECK_FOR_INTERRUPTS() between chunks Thanks for the updated benchmark. For comparison, here are my numbers: # Debian 13.5 VM on an Apple M5 Max, 32MB of L2 cache / 24 MB of L3 # cache, 36 GB RAM, gcc 14.2.0 -O2 jan=# select * from repeat_bench(); source | repeats | output | master | doubling | nocap | cap512 | cap1k | cap2k | cap4k | cap8k | cap16k | cap32k | cap64k | cap128k | cap256k | cap512k | cap1m | cap2m | cap4m | cap8m | cap16m | fastest --------+-----------+----------+----------+----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+--------------------------- 32 B | 2 | 64 B | 3 ns | 0.93x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | 0.82x | master 32 B | 4 | 128 B | 5 ns | 1.31x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | 1.13x | doubling 32 B | 6 | 192 B | 7 ns | 1.59x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | 1.42x | doubling 32 B | 7 | 224 B | 7 ns | 1.67x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | 1.50x | doubling 32 B | 8 | 256 B | 7 ns | 1.56x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | 1.65x | cap2k (median: nocap) 32 B | 9 | 288 B | 8 ns | 1.71x | 1.57x | 1.56x | 1.57x | 1.57x | 1.57x | 1.56x | 1.57x | 1.57x | 1.57x | 1.56x | 1.57x | 1.56x | 1.56x | 1.57x | 1.57x | 1.57x | 1.57x | doubling 32 B | 12 | 384 B | 9 ns | 1.27x | 1.52x | 1.44x | 1.55x | 1.40x | 1.56x | 1.55x | 1.56x | 1.55x | 1.46x | 1.40x | 1.46x | 1.43x | 1.55x | 1.40x | 1.56x | 1.55x | 1.56x | cap16k (median: cap1k) 32 B | 16 | 512 B | 12 ns | 1.43x | 1.24x | 1.24x | 1.24x | 1.25x | 1.24x | 1.25x | 1.25x | 1.25x | 1.24x | 1.25x | 1.25x | 1.24x | 1.25x | 1.25x | 1.25x | 1.24x | 1.25x | doubling (median: cap4m) 32 B | 64 | 2 KB | 49 ns | 2.29x | 2.25x | 2.24x | 2.27x | 2.26x | 2.27x | 2.26x | 2.27x | 2.26x | 2.26x | 2.26x | 2.27x | 2.27x | 2.26x | 2.26x | 2.27x | 2.26x | 2.27x | doubling (median: cap16m) 32 B | 4096 | 128 KB | 2.7 us | 2.55x | 2.84x | 2.35x | 2.44x | 2.44x | 2.47x | 2.58x | 2.50x | 2.51x | 2.51x | 2.54x | 2.54x | 2.51x | 2.53x | 2.51x | 2.52x | 2.50x | 2.50x | nocap (median: cap8k) 1 B | 16777216 | 16 MB | 14.8 ms | 119.90x | 120.23x | 71.77x | 73.30x | 74.02x | 67.44x | 39.32x | 61.44x | 71.05x | 93.60x | 105.89x | 112.77x | 116.40x | 118.54x | 119.58x | 120.19x | 120.19x | 120.35x | cap16m 10 B | 1677721 | 16 MB | 1.5 ms | 5.84x | 5.86x | 6.60x | 6.66x | 6.62x | 5.07x | 6.14x | 7.72x | 9.60x | 10.65x | 5.95x | 6.02x | 6.05x | 5.89x | 5.86x | 5.84x | 5.85x | 5.86x | cap64k 100 B | 167772 | 16 MB | 316.4 us | 1.27x | 1.27x | 1.42x | 1.47x | 1.37x | 1.11x | 1.47x | 1.90x | 2.26x | 2.38x | 1.30x | 1.30x | 1.31x | 1.28x | 1.27x | 1.27x | 1.27x | 1.27x | cap64k 1 KB | 16384 | 16 MB | 258.5 us | 1.04x | 1.04x | 1.01x | 1.01x | 1.20x | 1.09x | 0.95x | 1.18x | 1.45x | 1.80x | 1.68x | 1.06x | 1.07x | 1.06x | 1.04x | 1.04x | 1.03x | 1.04x | cap64k 64 KB | 256 | 16 MB | 204.0 us | 0.82x | 0.82x | 1.00x | 1.00x | 1.01x | 1.01x | 1.01x | 1.02x | 1.02x | 1.01x | 1.35x | 0.84x | 0.84x | 0.83x | 0.82x | 0.82x | 0.82x | 0.82x | cap128k 1 MB | 16 | 16 MB | 249.3 us | 0.99x | 0.99x | 1.00x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 0.99x | 0.99x | 0.99x | 0.99x | cap4k (median: cap256k) 1 B | 268435456 | 256 MB | 272.1 ms | 137.59x | 137.80x | 62.59x | 62.03x | 63.57x | 62.09x | 31.27x | 33.47x | 47.84x | 67.43x | 89.15x | 108.70x | 127.66x | 134.98x | 136.54x | 137.14x | 137.42x | 137.55x | nocap (median: cap16m) 10 B | 26843545 | 256 MB | 23.5 ms | 5.65x | 5.66x | 5.74x | 5.77x | 5.64x | 5.21x | 4.19x | 2.95x | 4.26x | 6.20x | 5.29x | 5.72x | 5.94x | 5.86x | 5.84x | 5.84x | 5.74x | 5.73x | cap64k 100 B | 2684354 | 256 MB | 5.2 ms | 1.25x | 1.25x | 1.26x | 1.28x | 1.24x | 0.74x | 0.97x | 0.74x | 1.08x | 1.57x | 1.29x | 1.31x | 1.32x | 1.29x | 1.29x | 1.29x | 1.27x | 1.26x | cap64k 1 KB | 262144 | 256 MB | 4.5 ms | 1.08x | 1.09x | 1.06x | 1.05x | 1.11x | 1.04x | 0.73x | 0.53x | 0.72x | 1.05x | 1.39x | 1.11x | 1.14x | 1.15x | 1.12x | 1.13x | 1.13x | 1.12x | cap128k 64 KB | 4096 | 256 MB | 4.5 ms | 1.08x | 1.09x | 1.02x | 1.00x | 1.00x | 1.01x | 1.01x | 0.99x | 1.00x | 0.99x | 1.34x | 1.08x | 1.14x | 1.14x | 1.12x | 1.13x | 1.12x | 1.12x | cap128k 1 MB | 256 | 256 MB | 4.1 ms | 0.97x | 0.98x | 1.02x | 1.04x | 1.04x | 1.04x | 1.04x | 1.04x | 1.04x | 1.04x | 1.06x | 1.04x | 1.04x | 1.04x | 1.02x | 1.02x | 1.02x | 1.01x | cap128k (median: cap64k) 1 B | 1000 | 1000 B | 1.3 us | 166.38x | 157.59x | 153.17x | 157.57x | 156.83x | 156.83x | 157.57x | 157.57x | 157.57x | 157.59x | 157.57x | 157.57x | 156.83x | 157.57x | 157.59x | 156.83x | 156.83x | 156.83x | doubling 1 B | 1000000 | 976.6 KB | 870.5 us | 112.32x | 112.94x | 72.54x | 75.42x | 76.25x | 75.42x | 49.63x | 70.82x | 87.05x | 99.48x | 107.70x | 113.55x | 112.32x | 112.94x | 112.92x | 112.94x | 112.94x | 113.54x | cap256k (median: nocap) (24 rows) # Debian 13.5 on an Intel Pentium Silver J5005, 4 MB L2 cache, no L3 # cache, 16 GB RAM, gcc 14.2.0 -O2 jan=# select * from repeat_bench(); source | repeats | output | master | doubling | nocap | cap512 | cap1k | cap2k | cap4k | cap8k | cap16k | cap32k | cap64k | cap128k | cap256k | cap512k | cap1m | cap2m | cap4m | cap8m | cap16m | fastest --------+-----------+----------+-----------+----------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+------------------------- 32 B | 2 | 64 B | 13 ns | 1.06x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | 0.81x | doubling 32 B | 4 | 128 B | 22 ns | 1.15x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | 1.02x | doubling 32 B | 6 | 192 B | 31 ns | 1.19x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | 1.09x | doubling 32 B | 7 | 224 B | 36 ns | 1.39x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | 1.27x | doubling 32 B | 8 | 256 B | 42 ns | 1.57x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | 1.43x | doubling 32 B | 9 | 288 B | 47 ns | 1.46x | 1.36x | 1.35x | 1.35x | 1.35x | 1.35x | 1.35x | 1.36x | 1.36x | 1.35x | 1.35x | 1.35x | 1.36x | 1.36x | 1.36x | 1.36x | 1.36x | 1.36x | doubling 32 B | 12 | 384 B | 61 ns | 1.81x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | 1.68x | doubling 32 B | 16 | 512 B | 84 ns | 2.26x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | 2.06x | doubling 32 B | 64 | 2 KB | 316 ns | 3.77x | 3.56x | 3.29x | 3.57x | 3.56x | 3.55x | 3.55x | 3.56x | 3.55x | 3.56x | 3.55x | 3.55x | 3.55x | 3.55x | 3.55x | 3.55x | 3.55x | 3.55x | doubling 32 B | 4096 | 128 KB | 19.1 us | 2.06x | 2.06x | 2.65x | 2.83x | 2.97x | 2.80x | 2.86x | 2.57x | 2.10x | 2.06x | 2.07x | 2.07x | 2.07x | 2.06x | 2.06x | 2.07x | 2.06x | 2.07x | cap2k 1 B | 16777216 | 16 MB | 115.4 ms | 62.63x | 62.87x | 48.66x | 48.17x | 46.47x | 46.12x | 46.15x | 45.99x | 46.26x | 46.42x | 45.77x | 46.02x | 45.57x | 53.15x | 62.82x | 62.85x | 64.64x | 63.37x | cap8m (median: cap16m) 10 B | 1677721 | 16 MB | 12.8 ms | 4.69x | 4.84x | 5.81x | 5.43x | 5.56x | 5.51x | 5.50x | 5.44x | 5.29x | 5.29x | 5.28x | 5.29x | 5.31x | 6.08x | 6.31x | 4.92x | 4.92x | 4.87x | cap2m 100 B | 167772 | 16 MB | 2.2 ms | 0.82x | 0.85x | 1.01x | 0.98x | 0.91x | 0.93x | 0.93x | 0.90x | 0.90x | 0.89x | 0.89x | 0.90x | 0.90x | 1.01x | 1.03x | 0.86x | 0.85x | 0.85x | cap2m 1 KB | 16384 | 16 MB | 2.2 ms | 0.83x | 0.85x | 1.00x | 0.98x | 0.76x | 0.96x | 0.96x | 0.95x | 0.92x | 0.91x | 0.92x | 0.92x | 0.91x | 1.01x | 1.10x | 0.92x | 0.87x | 0.85x | cap2m 64 KB | 256 | 16 MB | 2.4 ms | 0.90x | 0.91x | 1.00x | 1.00x | 0.97x | 0.97x | 0.97x | 0.97x | 0.97x | 0.97x | 0.98x | 0.98x | 0.98x | 1.07x | 1.18x | 0.98x | 0.92x | 0.91x | cap2m 1 MB | 16 | 16 MB | 2.3 ms | 0.87x | 0.85x | 0.98x | 1.06x | 1.06x | 1.06x | 1.06x | 1.06x | 1.06x | 1.06x | 1.06x | 1.06x | 1.06x | 1.06x | 1.05x | 0.91x | 0.83x | 0.85x | cap1k (median: cap16k) 1 B | 268435456 | 256 MB | 1863.4 ms | 63.24x | 64.65x | 49.51x | 49.87x | 49.36x | 49.32x | 48.93x | 49.09x | 48.95x | 49.08x | 49.03x | 48.93x | 49.00x | 61.96x | 63.08x | 62.97x | 63.22x | 63.51x | nocap 10 B | 26843545 | 256 MB | 207.6 ms | 4.45x | 4.47x | 5.43x | 5.22x | 5.49x | 5.50x | 5.44x | 5.38x | 5.25x | 5.20x | 5.24x | 5.22x | 5.21x | 6.37x | 6.38x | 4.60x | 4.53x | 4.47x | cap2m (median: cap1m) 100 B | 2684354 | 256 MB | 38.7 ms | 0.82x | 0.83x | 1.01x | 0.97x | 1.02x | 1.02x | 1.01x | 0.98x | 0.98x | 0.97x | 0.98x | 0.98x | 0.97x | 1.18x | 1.16x | 0.85x | 0.84x | 0.83x | cap1m 1 KB | 262144 | 256 MB | 37.8 ms | 0.81x | 0.81x | 1.00x | 1.00x | 0.70x | 1.00x | 0.99x | 0.99x | 0.95x | 0.95x | 0.95x | 0.95x | 0.95x | 1.16x | 1.16x | 1.03x | 0.82x | 0.82x | cap2m (median: cap1m) 64 KB | 4096 | 256 MB | 39.1 ms | 0.83x | 0.84x | 1.00x | 0.99x | 0.99x | 0.99x | 0.99x | 0.99x | 0.99x | 0.99x | 0.99x | 0.98x | 0.98x | 1.20x | 1.20x | 1.00x | 0.84x | 0.85x | cap2m (median: cap1m) 1 MB | 256 | 256 MB | 32.6 ms | 0.70x | 0.69x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.01x | 1.00x | 0.82x | 0.71x | 0.71x | cap128k 1 B | 1000 | 1000 B | 6.8 us | 240.97x | 225.32x | 201.53x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | 225.38x | doubling 1 B | 1000000 | 976.6 KB | 6.8 ms | 134.42x | 134.55x | 124.58x | 126.56x | 132.95x | 128.51x | 131.56x | 133.10x | 133.85x | 134.27x | 134.37x | 134.51x | 134.58x | 134.53x | 134.55x | 134.57x | 134.52x | 134.54x | cap512k (median: nocap) (24 rows) To understand the different cap behavior on our systems better, could you share the output of the following command from your systems? ld.so --list-tunables | grep non_temporal On my J5005, it looks as follows: $ ld.so --list-tunables | grep non_temporal glibc.cpu.x86_memset_non_temporal_threshold: 0x100000 (min: 0x4040, max: 0xffffffffffffffff) glibc.cpu.x86_non_temporal_threshold: 0x100000 (min: 0x4040, max: 0xfffffffffffffff) The preferred cap on the J5005 is the first one at or above this threshold (0x100000 = 1 MB). Does this match the behavior on your systems as well? On ARM, this tunable isn't present. > The corrected single-byte results do not change the overall conclusion. For > multi-byte inputs, the preferred cap on this machine still varies noticeably > with the total output size. In particular, the 16 MB cases tend to prefer > relatively small caps, while the 256 MB cases consistently prefer cap16m. > > For the single-byte cases, splitting the memset() into capped chunks also does > not seem to introduce a significant performance penalty. In some cases, the > larger capped memset variants are even slightly faster than a single large > memset(). Unfortunately, on the M5 Max it does. Comparing the worst chunked variant (cap8k in all three cases) against nocap, which does the same work in a single memset(): output cap8k nocap ratio 16 MB 39.32x 120.23x 3.1x 256 MB 31.27x 137.80x 4.4x 976.6 KB 49.63x 112.94x 2.3x To move this patch forward, would you consider proposing the single-byte memset() as a separate, smaller patch first? It is a large win with no regression in the benchmarks we have executed. It is also independent of the block size question. So, it could go in while the doubling strategy needs more investigation. Also, the early return for count == 0 || slen == 0 is a second change that could be committed independently of the cap discussion. Best regards Jan -- Jan Nidzwetzki PlanetScale Postgres Core Team
Re: [PATCH] Speed up repeat() for larger counts
От:
Jan Nidzwetzki <jan@planetscale.com>
Дата:
Hi Chenhui,
Thanks for the new version and for splitting the patch set. It applies
to the current master branch, and check-world passes.
On 12.09.26 15:12, ChenhuiMo wrote:
[...]
> Here the non-temporal threshold is 12.625 MiB, so the first tested cap
> at or above it is 16 MiB. This also matches the earlier measurements,
> where cap16m was often the best choice for the larger-output cases.
>
> So the glibc non-temporal threshold appears to correlate well with the
> preferred cap on both of my x86 systems.
That settles it for me. Collecting the three x86 systems measured so far:
system non-temporal threshold first cap at or above it
J5005 1 MiB cap1m
i5-13500H 1.3125 MiB cap2m
i9-12900H 12.625 MiB cap16m
In every case, the preferred cap is the first one at or above the
threshold. So the 64 KB to 16 MB spread we started with is a glibc
setting with three different values, not a hardware property.
Review
======
In patch 2, CHECK_FOR_INTERRUPTS() is called after memset(). Usually,
CHECK_FOR_INTERRUPTS() is called periodically to check whether a
long-running task has to be aborted. After memset() returns, the actual
heavy work is already done. Not sure if we actually need the check.
In addition, I recommend a short comment for the new 'if (slen == 1)'
path that describes that memset() is faster than the loop below.
strings.sql offers only partial test coverage for the added conditions.
I recommend adding at least these three tests to the existing ones to
test the early return and the memset() fast path:
SELECT repeat('', 10);
SELECT repeat('x', 0);
SELECT repeat('x', 4);
Additional comments
===================
Your new patch set has a different author than the previous ones. Please
double-check that it contains the actual name you want visible in the
commit.
When you generate a new patch set, it is common to version it to make it
easier for others to understand which version is most recent. You can do
this by specifying the "-v" option when exporting the patch. For
example, "git format-patch -v3 -2" generates version 3 of the patch
series consisting of the last two commits.
Best regards
Jan
--
Jan Nidzwetzki
PlanetScale Postgres Core Team
Re: [PATCH] Speed up repeat() for larger counts
От:
Sehrope Sarkuni <sehrope@jackdb.com>
Дата:
FYI, the lpad/rpad patch now also uses the same helper for repeat(), so this overlaps with the work here: https://www.postgresql.org/message-id/CAH7T-apj%2BpFg9bRkXVGfGejS2Uu4WzdMd7KoLKTW11mdsrt1Ew%40mail.gmail.com I've credited this thread for the earlier repeat() doubling work and the discussion around capping the copy size. Regards, -- Sehrope Sarkuni Founder & CEO | JackDB, Inc. | https://www.jackdb.com/