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.94.2) (envelope-from ) id 1qTKk3-009Dbh-2O for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Aug 2023 11:19:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qTKk0-00Fwrw-Cp for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Aug 2023 11:19:08 +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.94.2) (envelope-from ) id 1qTKk0-00Fwrn-24 for pgsql-hackers@lists.postgresql.org; Tue, 08 Aug 2023 11:19:08 +0000 Received: from mail-yb1-xb2a.google.com ([2607:f8b0:4864:20::b2a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qTKjw-001HAy-VW for pgsql-hackers@postgresql.org; Tue, 08 Aug 2023 11:19:07 +0000 Received: by mail-yb1-xb2a.google.com with SMTP id 3f1490d57ef6-d4f022c6c91so2636307276.2 for ; Tue, 08 Aug 2023 04:19:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1691493542; x=1692098342; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=+3vfSET8SODhGlQzkvmQEadDokx70PT6oGxelxM1Gz8=; b=PQEaD7x+BdHhIfKrJuLBiCIyeKG48WnE5L2ihjDs5p+WoMfbjRK+szKDekRrrP3A0D r450TSo1dRtF/VFnTv1QCo0dq22excII6H5MF4l6qyxri+NvZcJObxTCOZqyigfjrzKQ 9LlsXQRiig0st2nsG8dVqRm3UmZQaaJ5zRI3FcLfXl39UbD1827Rj7DuAfUeezhqvp3S k3MfcGcPPUz+J/Z8FisXxNk8bNM1Aq3ePzN+96lOfCJRkjEsD3Cwc0nQlkA/Jp9ZqqoW x5M3WBCQSxl1Nc0VRVwkLsF3kf4xHzcksYTOVH2TeS9Q68u5nuCLKAcBzLET/wL8xUqs VJsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691493542; x=1692098342; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+3vfSET8SODhGlQzkvmQEadDokx70PT6oGxelxM1Gz8=; b=A7ls6ske3wjci7wFjwSSM2uJ2YPuJn6TqnprM8yfWhw3+k1l9qxVOnFWxPcei9J51k 7DRym071VMIH+/JP2Ig6kAmknWlEzKQLGJxv8mbNj4/Yatq18D4KiZOcraOn/6QbJSdp vLbNabHOXMWH6o9h67CurZmZI+u9pRZ9IQ1Xly6Bv/e2vpZyjMou0h7ZttUBaC2LGiGB H5ksXKbeWMppqWS2TnUaxCs5gaClQpWrW3z6A5G+wSVXy0dOIrl4Y83c9j6xLfdU0ggu oTEnDua/Z+aLwIgPpR9HESsVD44EA0lrp2gk/f1AtcGLnOe09xhfY+72gfXNY5iapzjP lfyA== X-Gm-Message-State: AOJu0YzlVqgs1YCcfkZ0kPfUzFDSrY3d2CpIUMWzqaVJnJ0E5vE3PCuh PCyYNqr2m3r7rkXHFssTgiQysly6LUVI1by8kYT3LA== X-Google-Smtp-Source: AGHT+IHiSv7MamJa/Sse/vkzIigEqz/ndmPm1/Sz1MYD3bRw0i6OwhR8/QzS0s2Vw/FVnBCAMyj+3ilyavmYm+iiY9Y= X-Received: by 2002:a81:844c:0:b0:570:6d74:21d5 with SMTP id u73-20020a81844c000000b005706d7421d5mr14058559ywf.13.1691493542568; Tue, 08 Aug 2023 04:19:02 -0700 (PDT) MIME-Version: 1.0 References: <20210719195950.gavgs6ujzmjfaiig@alap3.anarazel.de> <20230719085236.jltxal2eztqrprfg@awork3.anarazel.de> In-Reply-To: <20230719085236.jltxal2eztqrprfg@awork3.anarazel.de> From: John Naylor Date: Tue, 8 Aug 2023 18:18:51 +0700 Message-ID: Subject: Re: Avoid stack frame setup in performance critical routines using tail calls To: Andres Freund Cc: David Rowley , pgsql-hackers@postgresql.org, Robert Haas , Michael Paquier , Tomas Vondra Content-Type: multipart/alternative; boundary="000000000000bec4cb06026787b3" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000bec4cb06026787b3 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jul 19, 2023 at 3:53=E2=80=AFPM Andres Freund = wrote: > > Hi, > > David and I were chatting about this patch, in the context of his bump > allocator patch. Attached is a rebased version that is also split up into two > steps, and a bit more polished. Here is a quick test -- something similar was used to measure the slab improvements last cycle. With radix tree v37 0001-0011 from [1], create extension bench_radix_tree; select avg(load_ms) from generate_series(1,100) x(x), lateral (select * from bench_load_random_int(100 * 1000 * (1+x-x))) a; The backend was pinned and turbo off. Perf runs were separate from timed runs. I included 0002 for completeness. v37 avg --------------------- 27.0400000000000000 32.42% postgres bench_radix_tree.so [.] rt_recursive_set 21.60% postgres postgres [.] SlabAlloc 11.06% postgres [unknown] [k] 0xffffffff930018f7 10.49% postgres bench_radix_tree.so [.] rt_extend_down 7.07% postgres postgres [.] MemoryContextAlloc 4.83% postgres bench_radix_tree.so [.] rt_node_insert_inner 2.19% postgres bench_radix_tree.so [.] rt_grow_node_48 2.16% postgres bench_radix_tree.so [.] rt_set.isra.0 1.50% postgres bench_radix_tree.so [.] MemoryContextAlloc@plt v37 + palloc sibling calls avg --------------------- 26.0700000000000000 v37 + palloc sibling calls + opt aset avg --------------------- 26.0900000000000000 33.78% postgres bench_radix_tree.so [.] rt_recursive_set 23.04% postgres postgres [.] SlabAlloc 11.43% postgres [unknown] [k] 0xffffffff930018f7 11.05% postgres bench_radix_tree.so [.] rt_extend_down 5.52% postgres bench_radix_tree.so [.] rt_node_insert_inner 2.47% postgres bench_radix_tree.so [.] rt_set.isra.0 2.30% postgres bench_radix_tree.so [.] rt_grow_node_48 1.88% postgres postgres [.] MemoryContextAlloc 1.44% postgres bench_radix_tree.so [.] MemoryContextAlloc@plt It's nice to see MemoryContextAlloc go down in the profile. [1] https://www.postgresql.org/message-id/CAD21AoA3gS45DFMOyTE-Wm4fu+BYzsYPVcSM= YggLxwm40cGHZg@mail.gmail.com -- John Naylor EDB: http://www.enterprisedb.com --000000000000bec4cb06026787b3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Wed, Jul 19, 2023 at 3:53=E2=80=AFPM Andres Freund = <andres@anarazel.de> wrote:=
>
> Hi,
>
> David and I were chatting about this p= atch, in the context of his bump
> allocator patch.=C2=A0 Attached is= a rebased version that is also split up into two
> steps, and a bit = more polished.

Here is a quick test -- something similar was used to= =C2=A0measure the slab improvements last cycle. With radix tree v37 0001-00= 11 from [1],

create extension bench_radix_tree;
= select avg(load_ms) from generate_series(1,100) x(x), lateral (select * fro= m bench_load_random_int(100 * 1000 * (1+x-x))) a;

<= div>The backend was pinned and turbo off. Perf runs were separate from time= d runs. I included 0002 for completeness.

v37
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0avg =C2=A0 =C2=A0 =C2=A0 =C2=A0
-----= ----------------
=C2=A027.0400000000000000

=C2=A0 32.42% =C2=A0po= stgres =C2=A0bench_radix_tree.so =C2=A0[.] rt_recursive_set
=C2=A0 21.60= % =C2=A0postgres =C2=A0postgres =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 [= .] SlabAlloc
=C2=A0 11.06% =C2=A0postgres =C2=A0[unknown] =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0[k] 0xffffffff930018f7
=C2=A0 10.49% =C2=A0po= stgres =C2=A0bench_radix_tree.so =C2=A0[.] rt_extend_down
=C2=A0 =C2=A07= .07% =C2=A0postgres =C2=A0postgres =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 [.] MemoryContextAlloc
=C2=A0 =C2=A04.83% =C2=A0postgres =C2=A0bench= _radix_tree.so =C2=A0[.] rt_node_insert_inner
=C2=A0 =C2=A02.19% =C2=A0p= ostgres =C2=A0bench_radix_tree.so =C2=A0[.] rt_grow_node_48
=C2=A0 =C2= =A02.16% =C2=A0postgres =C2=A0bench_radix_tree.so =C2=A0[.] rt_set.isra.0=C2=A0 =C2=A01.50% =C2=A0postgres =C2=A0bench_radix_tree.so =C2=A0[.] Mem= oryContextAlloc@plt

v37 + palloc sibling calls
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0avg =C2=A0 =C2=A0 =C2=A0 =C2=A0
---------------------<= br>=C2=A026.0700000000000000

v37 + palloc sibling calls + opt aset=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0avg =C2=A0 =C2=A0 =C2=A0 =C2=A0
---= ------------------
=C2=A026.0900000000000000

=C2=A0 33.78% =C2=A0= postgres =C2=A0bench_radix_tree.so =C2=A0[.] rt_recursive_set
=C2=A0 23.= 04% =C2=A0postgres =C2=A0postgres =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= [.] SlabAlloc
=C2=A0 11.43% =C2=A0postgres =C2=A0[unknown] =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0[k] 0xffffffff930018f7
=C2=A0 11.05% =C2= =A0postgres =C2=A0bench_radix_tree.so =C2=A0[.] rt_extend_down
=C2=A0 = =C2=A05.52% =C2=A0postgres =C2=A0bench_radix_tree.so =C2=A0[.] rt_node_inse= rt_inner
=C2=A0 =C2=A02.47% =C2=A0postgres =C2=A0bench_radix_tree.so =C2= =A0[.] rt_set.isra.0
=C2=A0 =C2=A02.30% =C2=A0postgres =C2=A0bench_radix= _tree.so =C2=A0[.] rt_grow_node_48
=C2=A0 =C2=A01.88% =C2=A0postgres =C2= =A0postgres =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 [.] MemoryContextAllo= c
=C2=A0 =C2=A01.44% =C2=A0postgres =C2=A0bench_radix_tree.so =C2=A0[.] = MemoryContextAlloc@plt

It's nice to see Me= moryContextAlloc go down in the profile.

--000000000000bec4cb06026787b3--