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 1wCdSp-002B2s-23 for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Apr 2026 13:06:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wCdSo-00BwzQ-02 for pgsql-hackers@arkaria.postgresql.org; Tue, 14 Apr 2026 13:05:58 +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.96) (envelope-from ) id 1wCdSn-00BwzH-1u for pgsql-hackers@lists.postgresql.org; Tue, 14 Apr 2026 13:05:58 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wCdSm-00000000yXA-1CTA for pgsql-hackers@postgresql.org; Tue, 14 Apr 2026 13:05:57 +0000 Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-48334ee0aeaso54229205e9.1 for ; Tue, 14 Apr 2026 06:05:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776171954; x=1776776754; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=7s7cTeYDANbwqWf92uFXQ53sUFU3E7XB7HWa1vhi9Xs=; b=ckXsFgzgKRWQ+plP2UkRN2FSigxFNCIGOhwzFTxj39Qb1yVxeY8qWEvpB2nMBWCrdw H0CLiGNyg09DdINR0HXxezWY7H/9ZPFwu6wkJTnGpLp+Eau6dOZ2gUpWsneddYvziw5C tiWRIxIuo2XSEpuE0FLtwyHurlouPB7jgk6cCL+MAy0GdN6hDQMNNiS+XIDgYuNJY7q6 EQLj5n3DZNimjt3xaze5GazFBRKTk/KIBUi23LVNkXEl5s+yLo1jwR1g5rev8jZsQ3bL 5A+Ba3pMH+5Tepl4ebHex6LceuXvVFmRSczEKVk6/wVF0rCAuN53TUtnH+OzHkphGhG3 Pnkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776171954; x=1776776754; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7s7cTeYDANbwqWf92uFXQ53sUFU3E7XB7HWa1vhi9Xs=; b=iUq+n9MCi2F7Nsv8ozi6lOg8cz67UCWs3e2m+L6SHxAAE5pwQ6S2MrQQ+CgJNAD8CY nisXDoRrFw6HctDxDTvFKbk0P+N+IrMtbtEQy3qO2LTpIx3wni+QR71LdJP1OqVrJD39 dtLyQpwWWFhK4DiFPB91YvBDzUUTRoim2bjDg4muDcQA/kGbdrAki/rc1t+8YstyAUQf LI6ex5gOnEqFjWFgm4NLbASwTs3xh5MvLCT48+kkTKvGM227vdjM4anQGFz5csew/0Zd 6P34ULjSNEmobdYI3wUl/Tw7iBBzi7q5ZRKErnAuSh0kzvzCrq7wU025madoFwqA16BT 5hCA== X-Forwarded-Encrypted: i=1; AFNElJ/T9x+9MA//XqNwK3Cb7OSc/HWgOfq8sPB8R0TioSdUuYCGnyxs3OZX6B0A2ZEVxhGfMboJokr09B/l5OS4@postgresql.org X-Gm-Message-State: AOJu0YyOZtD9UXHVxd1mIyiWslxlBWcIfd97uJlddsjrRmz8wMpsR4cE +xTrsXTQlHkl+UeTeS63BSCs6Lv1HoMU9dCdFNOJLpyUOUOJjI736FSc X-Gm-Gg: AeBDieu8CkCcD+1e6g3LqlARQAAhKneiTVn8w5UQrqfJyxSu91uoDiAaMXNytjThWuK hvYzqiYmpDEm35esg+ZcNjCu2JcIt0HcV68fqcErKjsezPV0kardzvIn0miJlIV879xRQ+gjiU9 Shak2K/vjg4p0LOM0AgYqihXmHkz6zKmqJSeEOPubYk4Qn0LEOjbv7wMRUjfhIgrNER3BROiYQm NXde6wGsECF1wu4btDZ4nrHB2Fi0+06nLCzoRdss3OdYwjnAtMOKsOIEGbPPKE4rx9zN1x/Drlt kP30suuwrpa4UqLw8JriClnWw9B3vDulpJSpMyn+vZymaR8DrhF8D5waO116tM0aWvbT/e57M5f CYxJs6PDGTKIojBoH9FikgWTr1lnvhhbPHhqk3iuwVoUYr270keO73DBUE1Qju0aTxFMwWhz7/K R6STvKdBlfT3fxZGq/TYtPTYL7 X-Received: by 2002:a05:600d:4:b0:480:69b6:dfed with SMTP id 5b1f17b1804b1-488d68ab2bfmr196817955e9.24.1776171953456; Tue, 14 Apr 2026 06:05:53 -0700 (PDT) Received: from [172.31.5.233] ([165.225.27.16]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-488ede1de4asm111879155e9.4.2026.04.14.06.05.52 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Apr 2026 06:05:52 -0700 (PDT) Message-ID: <5dce2293-441e-4034-9c54-da87d2e631f5@gmail.com> Date: Tue, 14 Apr 2026 15:05:51 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Reduce build times of pg_trgm GIN indexes From: David Geier To: John Naylor , Heikki Linnakangas Cc: Matthias van de Meent , pgsql-hackers References: <5d366878-2007-4d31-861e-19294b7a583b@gmail.com> <9ac3931a-180e-4283-a7a8-05eb66099206@iki.fi> <2e11134f-02c3-43da-8c39-fb520a1a251d@iki.fi> <66620ec7-0f81-4813-9cf1-b901a56efcc3@gmail.com> <2a76b5ef-4b12-4023-93a1-eed6e64968f3@gmail.com> <6439c655-e281-409d-b884-6586750d5820@iki.fi> <07c79a83-94aa-4d54-95b9-cba2a29565c1@gmail.com> Content-Language: en-US In-Reply-To: <07c79a83-94aa-4d54-95b9-cba2a29565c1@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 13.04.2026 17:05, David Geier wrote: > On 08.04.2026 04:15, John Naylor wrote: >> On Tue, Apr 7, 2026 at 6:27 PM Heikki Linnakangas wrote: >>> But the comments on the pg_cmp functions say: >>> >>>> * NB: If the comparator function is inlined, some compilers may produce >>>> * worse code with these helper functions than with code with the >>>> * following form: >>>> * >>>> * if (a < b) >>>> * return -1; >>>> * if (a > b) >>>> * return 1; >>>> * return 0; >>>> * >>> >>> So, uh, is that really a universal improvement? Is that comment about >>> producing worse code outdated? > > Well spotted. Thanks! > >> >> No, it's quite recent: >> >> https://www.postgresql.org/message-id/20240212230423.GA3519%40nathanxps13 FWICS, this would only matter if btint4cmp() would get inlined somewhere, where the compiler could actually make use of understanding that parts of the if-cascade are not needed. Andres' example was return DO_COMPARE(a, b) < 0 ? (DO_COMPARE(b, c) < 0 ? b : (DO_COMPARE(a, c) < 0 ? c : a)) : (DO_COMPARE(b, c) > 0 ? b : (DO_COMPARE(a, c) < 0 ? a : c)); In the case of btint4cmp(), it's only ever invoked from the function manager, where it cannot be inlined. Or are there ways to invoke btint4cmp() that can be inlined, which I'm unaware of? > In my original benchmarks it was faster. I'll rebase the remaining > commits and do some more analysis. Here is the disassembly and the perf top output of master vs patched. I compiled with GCC 15.2.0. The unpatched version of btint4cmp() contains a conditional jump, which is mispredicted frequently in the sort. The patched version is completely branchless. master ====== Dump of assembler code for function btint4cmp: 0x00005aa9e33ccdb0 <+0>: endbr64 0x00005aa9e33ccdb4 <+4>: mov 0x20(%rdi),%edx 0x00005aa9e33ccdb7 <+7>: mov $0x1,%eax 0x00005aa9e33ccdbc <+12>: cmp %edx,0x30(%rdi) 0x00005aa9e33ccdbf <+15>: jl 0x5aa9e33ccdca 0x00005aa9e33ccdc1 <+17>: setne %al 0x00005aa9e33ccdc4 <+20>: movzbl %al,%eax 0x00005aa9e33ccdc7 <+23>: neg %rax 0x00005aa9e33ccdca <+26>: ret 37.22% pg_trgm.so [.] trigram_qsort_signed.constprop.0 7.99% postgres [.] cmpEntryAccumulator 6.60% postgres [.] ginCombineData 6.03% postgres [.] FunctionCall2Coll 3.19% postgres [.] btint4cmp 2.30% postgres [.] rbt_insert 2.29% pg_trgm.so [.] generate_trgm 2.24% postgres [.] pg_mblen_range 1.77% libc.so.6 [.] __towlower_l 1.73% pg_trgm.so [.] trigram_qsort_signed_med3 1.56% postgres [.] pg_utf2wchar_with_len Patched ======= Dump of assembler code for function btint4cmp: 0x000055a69e87bdb0 <+0>: endbr64 0x000055a69e87bdb4 <+4>: mov 0x20(%rdi),%eax 0x000055a69e87bdb7 <+7>: cmp %eax,0x30(%rdi) 0x000055a69e87bdba <+10>: setl %al 0x000055a69e87bdbd <+13>: setg %dl 0x000055a69e87bdc0 <+16>: movzbl %dl,%edx 0x000055a69e87bdc3 <+19>: movzbl %al,%eax 0x000055a69e87bdc6 <+22>: sub %edx,%eax 0x000055a69e87bdc8 <+24>: cltq 0x000055a69e87bdca <+26>: ret 38.07% pg_trgm.so [.] trigram_qsort_signed.constprop.0 7.69% postgres [.] cmpEntryAccumulator 6.96% postgres [.] ginCombineData 3.90% postgres [.] FunctionCall2Coll 2.54% postgres [.] pg_mblen_range 2.40% postgres [.] btint4cmp 2.38% pg_trgm.so [.] generate_trgm 1.86% postgres [.] rbt_insert 1.80% libc.so.6 [.] __towlower_l 1.73% pg_trgm.so [.] trigram_qsort_signed_med3 1.66% postgres [.] pg_utf2wchar_with_len -- David Geier