[PATCH] Add pg_lfind8_nonzero()

From: cca5507 <cca5507(at)qq(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: [PATCH] Add pg_lfind8_nonzero()
Date: 2025-12-14 13:33:00
Message-ID: tencent_8130B8B635655F06080FF940C78E2CF4A205@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi hackers,

I'd like to add pg_lfind8_nonzero() to optimize some code like this:

```
for (i = 0; i < numberOfAttributes; i++)
{
if (isnull[i])
{
hasnull = true;
break;
}
}
```

With pg_lfind8_nonzero(), we can write the code like this:

```
if (likely(numberOfAttributes > 0))
hasnull = pg_lfind8_nonzero((uint8 *) isnull, numberOfAttributes);
```

The pg_lfind8_nonzero() is faster because we can handle 8 bool values at a time.

v1-0001 add the pg_lfind8_nonzero().

v1-0002 use the pg_lfind8_nonzero() in some places.

v1-0003 add a extension "test_patch" only for testing, the test like this:

```
for (i = 0; i < 1000; i++)
{
for (int j = 0; j < natts; j++)
{
if (isnull[j])
{
hasnull = true;
break;
}
}
}
=======================
for (i = 0; i < 1000; i++)
{
if (likely(natts > 0))
hasnull = pg_lfind8_nonzero((uint8 *) isnull, natts);
}
```

create extension test_patch;
# 4 is the natts
select test_head(4);
select test_patch(4);

Test result:
natts: 4 head: 1984ns patch: 2094ns
natts: 8 head: 3196ns patch: 641ns
natts: 16 head: 4589ns patch: 752ns
natts: 32 head: 8036ns patch: 1152ns
natts: 64 head: 19367ns patch: 2455ns
natts: 128 head: 33445ns patch: 4018ns

Looking forward to your comments!

--
Regards,
ChangAo Chen

Attachment Content-Type Size
v1-0001-Add-pg_lfind8_nonzero.patch application/octet-stream 3.8 KB
v1-0002-Use-pg_lfind8_nonzero.patch application/octet-stream 3.7 KB
v1-0003-Test-pg_lfind8_nonzero.patch.tmp application/octet-stream 3.7 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alexander Korotkov 2025-12-14 13:33:45 Re: Add SPLIT PARTITION/MERGE PARTITIONS commands
Previous Message zengman 2025-12-14 12:51:22 Re: Add SPLIT PARTITION/MERGE PARTITIONS commands