From: | Yura Sokolov <y(dot)sokolov(at)postgrespro(dot)ru> |
---|---|
To: | Zixuan Fu <r33s3n6(at)gmail(dot)com>, pgsql-hackers(at)lists(dot)postgresql(dot)org |
Subject: | Re: Optimize scram_SaltedPassword performance |
Date: | 2025-02-03 07:45:49 |
Message-ID: | 62fbf768-d2d0-4bb2-840d-a8d2880af82b@postgrespro.ru |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
03.02.2025 10:11, Zixuan Fu пишет:
> Hi Hackers,
>
> While profiling a program with `perf`, I noticed that `scram_SaltedPassword`
> consumed more CPU time than expected. After some investigation, I found
> that the function performs many HMAC iterations (4096 rounds for
> SCRAM-SHA-256), and each iteration reinitializes the HMAC context, causing
> excessive overhead.
>
> OpenSSL has an optimization for this case: when the key remains the
> same, the HMAC context can be reused with a lightweight state reset by
> passing NULL as the key. To take advantage of this, I introduced
> `pg_hmac_reuse()`, which replaces the key with NULL when OpenSSL is used.
Good catch.
Since pg_hmac_reuse is not `static`, I'd add some checks that key is
exactly same. At least there should be
Assert(key == prev_key && len == prev_len && hash_bytes(key, len) ==
prev_hash);
Where `prev_key`, `prev_len` and `prev_hash` are static variables, filled
in `pg_hmac_init`.
I don't know, should it be `Assert`, or check that leads to `elog(ERROR)`.
`hash_bytes` is fast enough to not cause measurable slow down in production.
On the other hand, use cases are trivial enough to occasional misuses to be
caught using just `Assert`.
> With this change, the performance improves by approximately **4x** (reducing
> execution time from 4ms to 1ms). The built-in PostgreSQL HMAC implementation
> does not support context reuse, and modifying it would require substantial
> changes. Therefore, `pg_hmac_reuse()` simply calls `pg_hmac_init()` in that
> case, maintaining the existing logic.
-------
regards,
Yura
From | Date | Subject | |
---|---|---|---|
Next Message | Alvaro Herrera | 2025-02-03 07:50:41 | Re: NOT ENFORCED constraint feature |
Previous Message | Vladlen Popolitov | 2025-02-03 07:15:59 | Re: SQL/JSON json_table plan clause |