Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x9QT7-00000001zaQ-3LbN for pgsql-hackers@arkaria.postgresql.org; Wed, 23 Sep 2026 17:09:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x9QT6-00000006VRQ-0vep for pgsql-hackers@arkaria.postgresql.org; Wed, 23 Sep 2026 17:09:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x9QT5-00000006VRI-3Jyp for pgsql-hackers@lists.postgresql.org; Wed, 23 Sep 2026 17:09:15 +0000 Received: from mail-qv2-x0f.google.com ([2607:f8b0:4864:33::f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1x9QT3-00000000t42-0Omp for pgsql-hackers@postgresql.org; Wed, 23 Sep 2026 17:09:14 +0000 Received: by mail-qv2-x0f.google.com with SMTP id 6a1803df08f44-91415b09bf9so3871336d6.3 for ; Wed, 23 Sep 2026 10:09:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790183352; x=1790788152; darn=postgresql.org; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=EsHhSOuhrmf2N37852/J/CSmlONmmzgcwkUmH460aWA=; b=dznNC+mcnYtgVWA8Ptp9GP6SrYKlWrO2f7z5IlYWFMH9TUHPn1NtZCvuuYwR67QqqW s7gPEaRbHb0UVBBA8FWdRXFFdguRQxaNlar2uFTCM5GiAo/9R0q+DJCCHV4NcTDHJHDJ K/d3Vw9TU9dndiyGsrGlnFpJbxOkf2Jev3qb13Rgu4+CvjWXu9En+aZvaAfU+XwvD9nM R/dw/KtPuFWOkT5kG/6Ej1qqz31DShK19UJ5aBd6x0WdGhKR1RvI2wOI1V28cLHqvLhs tajxKCLlwDpAYH24wRhPCg3yEQ5ybDSTVSfXrmzx3bZcPaCvXZ9Bs4QidKKFU0pzRdoC G4IQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790183352; x=1790788152; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=EsHhSOuhrmf2N37852/J/CSmlONmmzgcwkUmH460aWA=; b=FxDFj4EhoiMjpuVBpQRumig3N2PEG59DkG67Z2T1XU1p+ixxoXg0G154DKuZD6FRmY mZZqYF7HQeDkC6qT80TX/ug32G0W414pjMqutvxtbuXG2wUAk+0HC+UGSWYyD3rA/XjG AXFqKz+cQHhQuRvsEeM4Gi03zP5RMyDpNV0o/au9EicRpNWI0teCrouHWIyN1e1becp2 zsxvop6ZaVlqyHVArBpGN3vyyNwhk4fxt18UN8nQlMx+W+IAxpvsDoECYnsy2pASh7h5 oGozrFLldpLeCEOMxc8qpVTm4oTRK8NeOJAy8nEOScTLH4J0cjm3GPB/8gwv89yPpBVT Zhbg== X-Gm-Message-State: AFuF++kagcHdOfhZuspkkC7v9A7PKHXc5Iik6xGFVpfqkeWCXukWBcPU ylrsIoA4Hh+7tduzQmGbmiqknRTfm+t04800EzzAwXhL22NEBu3cHT8j X-Gm-Gg: AYBFou3Gy9YaWW5m4B60CIM47wkz/dnjtQ98/k/5cf6Zj5+Em6+6EorYCL+mhGFrkd7 TaFCG+GJkmTOpeeR1l5gXY9L3nsNvSDANtTfR84BYbcXRGLSrvMo9XsodYEvgT34Q6pnM6Dp4PG LTi4Yddn8OCmiIDOWplv+JxGe2B1eaigMeVj4TF079wn7ACqgEZmxqEoW8mxLFvxYsL5qDXmrkX 5P27MiwQ0udSS6He5TYVMSvFLgvnta5qoSG3CWeXvh9Q9w3Wq/GbzysxyqElmiSAEUGjHiTsHPE SFzZG69HKK0DwUc7SmFVLv7xYTxY/SReBzM7Rova2tF1BeJAsJUxG1MHFSChRGlnyAHq69nmz9x 6AmsthPN00bjV5UdZ8PdrUXDScjvbx3PoGDk9Q1qzpqL4RBfm8cP1WFaKmVH9bogCqi9oCczLNh sg9RID56Cv5nesE1csnlZCO8IR565R3wJflwlUu9rc5CtHC+0FHbdsduGs55/wwccvEDQTMmpeH /gxciZWmdAfktvecO0gkThd2oYyHFj7YFp11X5L4KHswiioVgANZKAmZC56JHSJYNeZ49jqEv8m pP5gTlPgN9VX29FFXZJ6Qf7DYg== X-Received: by 2002:a05:6214:3293:b0:8f2:fbb1:cec4 with SMTP id 6a1803df08f44-9140c30e94amr44994066d6.19.1790183351474; Wed, 23 Sep 2026 10:09:11 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-9140c45e443sm24311226d6.38.2026.09.23.10.09.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Sep 2026 10:09:10 -0700 (PDT) Date: Wed, 23 Sep 2026 12:09:08 -0500 From: Nathan Bossart To: Sehrope Sarkuni Cc: Pg Hackers Subject: Re: Speed up lpad() and rpad() for one-byte padding strings Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="7fFRVEp6g7hhzb9Y" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --7fFRVEp6g7hhzb9Y Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Wed, Sep 23, 2026 at 09:41:36AM -0500, Nathan Bossart wrote: > On Wed, Sep 23, 2026 at 10:10:09AM -0400, Sehrope Sarkuni wrote: >> lpad() and rpad() pad one character at a time, calling >> pg_mblen_range() and memcpy() once per padding char. When the padding >> string is a single byte, e.g., lpad(x, n, '0') or rpad(x, n, ' '), >> the padding is that byte repeated, so the attached patch fills it with >> one memset(). > > I wonder if we could expand these gains by using SIMD whenever the vector > length is divisible by the padding string length. My hunch is that's where > a lot of the memset() gains come from. Actually, I think we can expand this to any padding string length by copying the padding string once, and then copying from the beginning of the padding to the end repeatedly so that we write double the padding each time. This is a bit like what commit c60e520 added for pglz_decompress(). I've attached some proof-of-concept grade patches. This doesn't quite match the performance of your 1-byte fast-path, but it's pretty close and applies to many more cases. -- nathan --7fFRVEp6g7hhzb9Y Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=v2-0001-Factor-the-padding-loop-out-of-lpad-and-rpad.patch