Re: Use bsearch() instead of a manual binary search in syscache.c

Thomas Munro <thomas.munro@gmail.com>

From: Thomas Munro <thomas.munro@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Antonin Houska <ah@cybertec.at>, cca5507@qq.com, pgsql-hackers@lists.postgresql.org
Date: 2025-11-09T01:55:21Z
Lists: pgsql-hackers
On Sun, Nov 9, 2025 at 6:01 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> I'm quite certain that years ago we determined that bsearch()
> was slower than a manually written-out loop, probably because of
> exactly the point that the comparisons would be inline.  Don't
> know whether modern compilers have changed that conclusion.

It looks like glibc's version is inlined, but others I checked aren't.

> There are places where we wouldn't care about such microscopic
> performance details, but I think syscache.c is not one of them.

So we'd probably need our own inline function to keep the playing
field level.  Some tweaked algorithms[1] are also said to speed up
small integer tables, Unicode tables etc.

[1] https://github.com/scandum/binary_search