Re: Optimize multiplications/divisions by 2 using bit shifts in hot paths

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: David Rowley <dgrowleyml(at)gmail(dot)com>
Cc: Postgres hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Optimize multiplications/divisions by 2 using bit shifts in hot paths
Date: 2025-09-19 03:56:57
Message-ID: E5CC9331-446F-408B-BA4C-1D9879594F39@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Sep 19, 2025, at 10:22, David Rowley <dgrowleyml(at)gmail(dot)com> wrote:
>
> On Fri, 19 Sept 2025 at 14:11, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
>> This patch uses left/right shift operators to optimize *2 and /2 operations in some functions that are in critical paths.
>>
>> For unsigned int, *2 and /2 exactly equal to <<1 and >>1.
>
> The compiler is most likely going to do this anyway. Try it out at
> https://godbolt.org/z/Y538Yd4Ka

Thanks for the info.

>
> What maybe is worth looking at is verifying which of these variables
> is signed when it really should be unsigned. Dividing by 2 and >> 1
> aren't the same for negative numbers.
>

Yes, I think I mentioned in the commit comment. For negative numbers, /2 doesn’t equal to >>1. I saw a lot of variables of type “int" named as “xxx_len”, feels like they can be unsigned. But we need to carefully check every one individually if we want change their types.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2025-09-19 05:27:46 Re: Incorrect logic in XLogNeedsFlush()
Previous Message Chao Li 2025-09-19 03:50:00 Re: encode/decode support for base64url