Re: [PATCH] Use cached hash to skip unnecessary key comparisons in dshash

From: Nathan Bossart <nathandbossart(at)gmail(dot)com>
To: CharSyam <charsyam(at)gmail(dot)com>
Cc: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: Re: [PATCH] Use cached hash to skip unnecessary key comparisons in dshash
Date: 2026-04-10 18:07:35
Message-ID: adk8Z0HGDaXSRIgF@nathan
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Apr 11, 2026 at 01:09:33AM +0900, CharSyam wrote:
> This patch adds a hash pre-check (item->hash == hash) before calling
> equal_keys() in both find_in_bucket() and delete_key_from_bucket().
> Items with non-matching hash values are skipped with a single integer
> comparison, avoiding the expensive key comparison entirely.

This relies on the fact that matching keys will have matching hashes, but
matching hashes does not necessarily imply matching keys. IIUC this is a
safe assumption, although a short comment to this effect might be a nice
addition.

> Test | Before | After | Improvement
> -------------------------+-----------+-----------+------------
> INSERT 10000 entries | 14.99 ms | 9.46 ms | ~37%
> LOOKUP 10000 hits | 10.40 ms | 5.52 ms | ~47%
> LOOKUP 10000 misses | 8.41 ms | 4.95 ms | ~41%
> LOOKUP 50000 hits (x5) | 33.48 ms | 26.44 ms | ~21%
>
> The improvement is most significant when bucket chains are longer and key
> comparison is expensive (e.g., string keys).

Nice results. Are there any regressions when the bucket chains are short
or when key comparisons are inexpensive?

> I believe this patch is ready for review. I look forward to any feedback
> and am happy to make revisions.

We are in feature-freeze for PostgreSQL v19 now, so this will unfortunately
need to wait until July when v20 development begins. Please add an entry
to the commitfest site to ensure this doesn't get lost:

https://commitfest.postgresql.org/

--
nathan

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2026-04-10 18:10:42 Re: [PATCH] Use cached hash to skip unnecessary key comparisons in dshash
Previous Message SATYANARAYANA NARLAPURAM 2026-04-10 18:06:05 Re: Bug: Missing collation assignment for GRAPH_TABLE COLUMNS expressions