Re: [PATCH] Use cached hash to skip unnecessary key comparisons in dshash - Mailing list pgsql-hackers

From Nathan Bossart
Subject Re: [PATCH] Use cached hash to skip unnecessary key comparisons in dshash
Date
Msg-id adk8Z0HGDaXSRIgF@nathan
Whole thread Raw
In response to [PATCH] Use cached hash to skip unnecessary key comparisons in dshash  (CharSyam <charsyam@gmail.com>)
Responses Re: [PATCH] Use cached hash to skip unnecessary key comparisons in dshash
List 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



pgsql-hackers by date:

Previous
From: SATYANARAYANA NARLAPURAM
Date:
Subject: Re: Bug: Missing collation assignment for GRAPH_TABLE COLUMNS expressions
Next
From: Nathan Bossart
Date:
Subject: Re: [PATCH] Use cached hash to skip unnecessary key comparisons in dshash