Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ter9X-00G3AA-O8 for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Feb 2025 07:45:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1ter9W-00BEnC-Kc for pgsql-hackers@arkaria.postgresql.org; Mon, 03 Feb 2025 07:45:54 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ter9W-00BEmx-Af for pgsql-hackers@lists.postgresql.org; Mon, 03 Feb 2025 07:45:54 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1ter9T-002zuX-0p for pgsql-hackers@lists.postgresql.org; Mon, 03 Feb 2025 07:45:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1738568750; bh=39U0wDiWR6lSHn87LUwqxKTeqqFxAwjo2L5jUnvVbsg=; h=Message-ID:Date:User-Agent:Subject:To:References:From:In-Reply-To: From; b=tbwEnFSoZvrzyWHKQJjoXlFaxKwtLtqmt9HUdU5RWBpQFA0Np+xd/gZXwJqWG0Ijm 3xSnOgXVVdHSYMSQcBzAcoA/9TRKiUv0oauBkysnEq4VIX60ST+tJ1XmLktxPRfK+m zFVYrxIuE0KuepN2C06UqtScklp9vDFECYEEKZmJZRxCogRKWVhp+ou7JeAKOeXOqE uZpABATAPtCGFQnUTfOwAEGxo2RAg0tP+W30wf+8HXJKHBKYQhxJ16e8UC3vTLJLmG nHbo33K1G2A0TQNSJPwbHz4bncqYPIQ7v9yw1T648JaPtxoSjCIVVPPIo0AqFVFNAL ESwTKsN30tcFg== Received: from [172.30.49.26] (unknown [172.30.49.26]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: y.sokolov@postgrespro.ru) by mail.postgrespro.ru (Postfix/465) with ESMTPSA id 4004360382; Mon, 3 Feb 2025 10:45:50 +0300 (MSK) Message-ID: <62fbf768-d2d0-4bb2-840d-a8d2880af82b@postgrespro.ru> Date: Mon, 3 Feb 2025 10:45:49 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Optimize scram_SaltedPassword performance To: Zixuan Fu , pgsql-hackers@lists.postgresql.org References: Content-Language: en-US From: Yura Sokolov In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/02/03 03:45:00 #27199584 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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