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.96) (envelope-from ) id 1vzbt9-001A0n-0u for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Mar 2026 14:47:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vzbt7-00Grk4-0P for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Mar 2026 14:47:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vzbt6-00Grjv-2I for pgsql-hackers@lists.postgresql.org; Mon, 09 Mar 2026 14:47:17 +0000 Received: from mail-ed1-x52b.google.com ([2a00:1450:4864:20::52b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1vzbt4-00000001oSm-2rvg for pgsql-hackers@postgresql.org; Mon, 09 Mar 2026 14:47:16 +0000 Received: by mail-ed1-x52b.google.com with SMTP id 4fb4d7f45d1cf-660d77cacc2so6035627a12.1 for ; Mon, 09 Mar 2026 07:47:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1773067633; cv=none; d=google.com; s=arc-20240605; b=Sx+R5RYpgsH1v7XEHBXkyvpmzTYrOwhWEr7axR5XzNpdNYWaee2oSHkvDIbvYbr4dc C0ahV3yizoYlOxW6cSCfXI9nlDxbTYGqlzOj9D6fbt8BB2D8msr1wM11b1d+1qbO99WK 5srfgbnphsyqNlnyH+XsqQsJ/RWv4eFXWRb8+R4BK8s1XE8LiH34ai/mrt+vWfn+HneL 3p2CaMTwuFV6Z8NMLnAQD5QIBxDjYbRGfyUtnCzZHcipCb1KSWausfsoKUNwiFfU0ShT NMO086JWjzMdUXJj7z9IojLAEpEws8Yomcc7z1UYc6hln8lOlxRy8YPVzNgzjCprARm3 fK2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=znDSZPsIo4q52CDRESYZ0lYNMyL83uA0/CG3QO+waXw=; fh=TVfGSc1ROJnfT2DI0+LhstR3EAj4703kqaeMLTgraqk=; b=bzs4nGn4Pc6m7UleZ8solvbKZ9zP2FgEJApWRhRVuGq2WBKxP1DtcB0P5XAebG4Ewy nxeo6nbOdDb9Yb27STlZSp33B3xQFVLzBzsJ36xZU8sNoi74gXdpBnlAgQ4JwEja7mvj tJMsJNFbf7Wjkpioho8qwO/GxcYpnjag7bRHvQwjJHwVU5PdtRdG7/mvhABfcX+YFNOj pevhzyXP/2RPQm+2VQ8LM/uwswluXvvDlFzq1qvBFIXjMOKRCfNAHrD+816AbX4fQnvF 9sTcVRXm3KaPdgJFDRlKwKtF7eVZ5BBF3q9mE4DAGqzzjyh8gCU0pb2tvF7EYQpEIxBU l80w==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1773067633; x=1773672433; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=znDSZPsIo4q52CDRESYZ0lYNMyL83uA0/CG3QO+waXw=; b=dL1lnCO7azdeBtiTEPQTBvxqyBWCZucNbc5fP5IngjBDIMWC1zGihogl38V/6tWAuC RTYvCbo6ZnxIcj6IzU953ChABz/hXktFOYgRmdvs4Yu0Q6cSk0z3qq5QqXKMll+QWGIb jjbZThY9UJei9twlzIbLZpVktZ3/Pd2ZfFMfxx5g5nMhAQNZ0L2ZMFgIPO60GsbUAQjQ zKFL70tiztkJhzaDiN8chi7xKk5WmQ/9pkiTHD/SWZQ8jRc9gLmw+EIDiShrP6N+aZWg 9CVFTohv0EaVVajBmt+fVtjnrx3IZX9GNfXHZjwgX/Lv4U2Ul0fGXLBeWH+0FTuRMHmp 9WNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1773067633; x=1773672433; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=znDSZPsIo4q52CDRESYZ0lYNMyL83uA0/CG3QO+waXw=; b=QZDi/c1VqgA4uDlIFBtKSW1bik31Px9BhowlDLf+4qb2uhshCWp2lGUripBUPsGi+y a1FygaWN66omvRlGgKfMk24Ay2F7kFh5pXh9ch0qyFDQKXOvsbyFZLaHwOTdxIHJzt8n Hv9nPemL0fHKznPvYYu1z9eh/RSY6mjepPMSf23YlZkNCTGw0kAYGegUZ8Eu64xGngHk D7zTsZYlnwswEbDWjQBAsO0xkIFyOK64h/nPWfygaSPNfOlgDX8Nw5jjaS1tJj5ygkfi oVtgkZ7KuxpAWhk7g933IQdWpAT779CswCW9g/xTVVx7bSIPqx1e0nrqD+jTTXwYLSat lzKQ== X-Gm-Message-State: AOJu0Yx9t/3aiK8QquBWLd3dMqagYk+TwYMJTWikcTOaudu5eXmNds7D S8H2TnujnZF6O89ydCPDLgmM7ASx9zXjiOXn9PvUIjIjzOErI/OLmc/za4UDtf1oHaoKKjhKQSc TV581mWQz+95yxN/vQAt0QOl+qrByUwwkq21af1M= X-Gm-Gg: ATEYQzzFWtioB/f6tCI7n0giQGndsKIXgi3SDB23kha/6Ah9ZguhsAUefABvGFAHad8 4CzzpB5LjOGJOzThP2qAQPqzbIVhG+sMjVkJWFgwGIg01stjXBaoRZOpQhol6HIda627r41sHJD AOGkWcaJ1kMd5rmKzgIOyaEiRVaKlSM4whKS9N0IhunJPIozAEV27vJv5q862r7m3UrJVcPB0f4 xqXC3Gd/8M3j9pOnfW850OL1b0mVA4RasV7X9P2Sbo1FxCfXYC03h6Y6xSlNmT4Ex6SSmOj+n6P 3g5m73lbWlZ1L4L3BvdgI5vQBCViMF7y23FTe1WEBUF7WB8LTBm+pZMQ0rinddm0M8d5fZg= X-Received: by 2002:a05:6402:5188:b0:65c:b71:e69e with SMTP id 4fb4d7f45d1cf-6619d44466cmr5811453a12.8.1773067632514; Mon, 09 Mar 2026 07:47:12 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Bryan Green Date: Mon, 9 Mar 2026 08:45:56 -0600 X-Gm-Features: AaiRm51alnqOCVQd4oVudGW80U2LMhQfm8L6yW6tyOoryMAO26KiY_xTE_JwJE8 Message-ID: Subject: Re: Avoid multiple calls to memcpy (src/backend/access/index/genam.c) To: Ranier Vilela Cc: Pg Hackers Content-Type: multipart/alternative; boundary="0000000000006619ca064c987ad1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006619ca064c987ad1 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I created an example that is a little bit closer to the actual code and changed the compiler from C++ to C. It is interesting the optimization that the compiler has chosen for version 1 versus version 2. One calls memcpy and one doesn't. There is a good chance the inlining of memcpy as SSE+scalar per iteration will be faster for syscache scans-- which I believe are usually small (1-4 keys?). Probably the only reason to do this patch would be if N is normally large or if this is considered an improvement in code clarity without a detrimental impact on small N syscache scans. I realize you only said "possible small optimization". It might be worthwhile to benchmark the code for different values of n to determine if there is a tipping point either way? https://godbolt.org/z/dM18cGfE6 -- bg On Mon, Mar 9, 2026 at 8:05=E2=80=AFAM Ranier Vilela = wrote: > > Em seg., 9 de mar. de 2026 =C3=A0s 10:16, Ranier Vilela > escreveu: > >> Hi. >> >> In the functions *systable_beginscan* and *systable_beginscan_ordered*, >> is possible a small optimization. >> The array *idxkey* can be constructed in one go with a single call to >> mempcy. >> The excess might not make much of a difference, but I think it's worth >> the effort. >> >> patch attached. >> > Someone asked me if O2 does not do the work. > Apparently not. > > https://godbolt.org/z/h5dndz33x > > best regards, > Ranier Vilela > --0000000000006619ca064c987ad1 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
I created an example that is a little bit closer to the ac= tual code and changed the compiler from C++ to C.=C2=A0

= It is interesting the optimization that the compiler has chosen for version= 1 versus version 2.=C2=A0 One calls
memcpy and one doesn't.= =C2=A0 There is a good chance the inlining of memcpy as SSE+scalar per iter= ation
will be faster for syscache scans-- which I believe are usu= ally small (1-4 keys?).=C2=A0=C2=A0

Probably the o= nly reason to do this patch would be if N is normally large or if this is c= onsidered an
improvement in code clarity without a detrimental=C2= =A0impact on small N syscache scans.=C2=A0=C2=A0
I realize you on= ly said "possible small optimization".=C2=A0 It might be worthwhi= le to benchmark the code for=C2=A0
different values of n to deter= mine if there is a tipping point either way?


On Mon, Mar 9, 20= 26 at 8:05=E2=80=AFAM Ranier Vilela <ranier.vf@gmail.com> wrote:

Em seg., 9 de mar. de 2026 =C3=A0s 10:16,= Ranier Vilela <ranier.vf@gmail.com> escreveu:
Hi.

I= n the functions *systable_beginscan* and *systable_beginscan_ordered*,
is possible a small optimization.
The array *idxkey* can be= constructed in one go with a single call to mempcy.
The excess m= ight not make much of a difference, but I think it's worth the effort.<= /div>

patch attached.
Someo= ne asked me if O2 does not do the work.
Apparently not.


best regard= s,
Ranier Vilela
--0000000000006619ca064c987ad1--