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 1vR4tg-005rB3-2z for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Dec 2025 08:41:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vR4te-001glX-1l for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Dec 2025 08:41:06 +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 1vR4te-001glP-0p for pgsql-hackers@lists.postgresql.org; Thu, 04 Dec 2025 08:41:06 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vR4ta-0034Jt-06 for pgsql-hackers@lists.postgresql.org; Thu, 04 Dec 2025 08:41:06 +0000 Received: by mail-pl1-x631.google.com with SMTP id d9443c01a7336-29844c68068so8976015ad.2 for ; Thu, 04 Dec 2025 00:41:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764837659; x=1765442459; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=woiuJxpte4H/rWNP2a9jcfpzb54d9Si7oiSRoGQ4nI4=; b=BoBz9TTZ/YTKcmSbFP453ii+TNb78+aWBMkYE63ngIfmQxIc+PFScQD9O5WJqUba3L HxX2D1xNUX0/IGVKOR+sSb6Csx7vqCOTTXCbOj+JIXNCivMg7SlY9AmYCQIn/RgrCtav 10DvuXX/d8Ykup2IF85kjsnUWRh0Oq4xIaMSbbNq3vN6LZu9141yndP3qxFzkq1q1Tt0 TMqZFbnAw+sR7ak+C2TL9dRLqm7raSQ8LRbNZCjRRqJX05BJ+RjTw4ymN5Cqq4od4XQ6 +Z1OQCdOwoDKo3tt8BK4WoYxyHQb4GlDw9lnuT4b9wweS8NEpOmOvtBXNbmBb5RWzdrN l68A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764837659; x=1765442459; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=woiuJxpte4H/rWNP2a9jcfpzb54d9Si7oiSRoGQ4nI4=; b=Da8jSZrp4Fc90ZU9T2piGgYKzeV/isAFbHrLn00LsmdIdFawM66cyMOygUH9mFzDen ixvRh4P8z8Te/9226fKgVrUhVmgR0FDlQiW924BT3D7OqH610o8cxOpWo8N7ue4YJe0C mpQ1w9lhPxiW8rJTwJjVlbplPFkU1OcAbkAlZaH5fqdSY6vajV4OcWcImVifSYgc360f UAAPnTCJTLT5ewOwk9f8dQphJcA0FKPHjwl1hR8dJ+XMoKZtn4A5jJpuqn2lmW8ftqSm 1znYTOGMQu2Zez2Acj1ymrG3o+JBmsrZ+Z9mvB7kDWr2xLjShAvVqqIQwsnDGtFw5GYH mdZA== X-Forwarded-Encrypted: i=1; AJvYcCU1wsEAaD1QtdKAI+6JTnCZn/Tz6lHX7KjSx97flG4KkF2pglDodZnA5m5bpTy/hJ2B6SYqRRn4+NCUBS+q@lists.postgresql.org X-Gm-Message-State: AOJu0YyC8rfL0CCjuj1HZHhiWeej/3VunedjxtKtFouyMjcySjIEgD6f D+OYPA1U5rALdVBWr+fcMIG4hMjmYpDtCgwbiqszTB4jSRxGdrJ9fyTF X-Gm-Gg: ASbGncvlE0VqQx3iffcf8JJ8yjLzx48feGkXcosVqYzvimZjYlgZcmG6NYHorkNu4oV zfadjdixu9jRlB5BtuXtuSCxfyiUa12dxjc6USGxNmGKlXq1eN5kWM5ywTQDXLiFej+EuKeVs/4 /5/b9lYessYZeJPAOT0WNiGV8UbDin0PVfOskPPCm3Js9M0T6tvGWcqQfft483vm9mFB5zD3cNr Zl2CcEyAED2m8uID/TjCQQ6bGN9wnEcotF9FslKj78WaOVwIy45j2g/fj+npna51Wu+ljaJVkub AT4w4ILb8flN3olBoj4lFDq8OHPc4iP3upLoWzF/v8r7W/hb3IWJJ1w4BRhLEwJydw2kxzPuFmW cfqFoo4EKPGNZaCunaBLO+xbgphypt9DuWVcoZG0gim3ekO/ubtPP89edU69JnBCCK6RKhFBHuZ pbtLFv39g+LNpU4F+3NA0= X-Google-Smtp-Source: AGHT+IHZv9fwAron3zheTAN8LTLkbCBkFMOUeXg0Z2SLzxmGhVfS2zXW53BuNhtcql/pJTf1QaXhjw== X-Received: by 2002:a17:903:249:b0:295:8a09:bcf2 with SMTP id d9443c01a7336-29d682b2b1cmr59198805ad.8.1764837658570; Thu, 04 Dec 2025 00:40:58 -0800 (PST) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29daeae71eesm12075225ad.100.2025.12.04.00.40.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Dec 2025 00:40:58 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3826.700.81\)) Subject: Re: tuple radix sort From: Chao Li In-Reply-To: Date: Thu, 4 Dec 2025 16:40:18 +0800 Cc: Chengpeng Yan , "pgsql-hackers@lists.postgresql.org" , Peter Geoghegan Content-Transfer-Encoding: quoted-printable Message-Id: References: <269A8FB9-6D43-43CF-A6FE-52D28CBDB8A9@Outlook.com> <606C775A-4C1A-482B-BE7D-2E7A46AE14B9@gmail.com> To: John Naylor X-Mailer: Apple Mail (2.3826.700.81) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Dec 4, 2025, at 13:30, John Naylor wrote: >=20 > On Wed, Dec 3, 2025 at 3:22=E2=80=AFPM Chao Li = wrote: >> I played with this again today and found an optimization that seems = to dramatically improve the performance: >>=20 >> ``` >> +static void >> +radix_sort_tuple(SortTuple *begin, size_t n_elems, int level, = Tuplesortstate *state) >> +{ >> + RadixPartitionInfo partitions[256] =3D {0}; >> + uint8_t remaining_partitions[256] =3D {0}; >> ``` >>=20 >> Here partitions and remaining_partitions are just temporary buffers, = allocating memory from stack and initialize them seems slow. By passing = them as function parameters are much faster. See attached diff for my = change. >=20 > The lesson here is: you can make it as fast as you like if you > accidentally blow away the state that we needed for this to work > correctly. >=20 Yeah, I quickly realized I was wrong after I clicked =E2=80=9Csend". I = was trying the firs two optimizations as I suggested in my previous = email, but the first didn=E2=80=99t help much, and the second just = didn=E2=80=99t work. After several hours debugging, I guess my brain got = stuck and came out the weird idea. I continued playing with this again today. I think the execution time is = mainly spent on the in-place element switching, which uses 3 levels of = loops (while->for->for). If we can use an extra temp array to hold the = sorted result, then the 3-level loop can be optimized to 1-level, but = that will cost a lot of extra memory which I am afraid not affordable. Anyway, it=E2=80=99s a fun of playing with this optimization thing. I = may play with it again once I get some time. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/