Repository navigation
Reuse the GCM cipher context between rows in encrypt/decrypt - #131001
Merged
Merged
Conversation
encrypt and decrypt in GCM mode set up the cipher context from scratch on every row. Passing the cipher to EVP_EncryptInit_ex makes OpenSSL 3 reset the context: it frees and re-creates the provider context and updates the reference count of the EVP_CIPHER that all threads share. With 16 threads that atomic update took over 40% of the time, and GCM ran about 25x slower than the other modes. #107339 fixed this for the other modes but left the GCM branch as it was. Pass the cipher only on the first row or after a failed decryption, and the key only when it changes, as the other modes already do. A GCM round trip over 100M rows with 16 threads goes from 23.7 s to 0.9 s, and from 3.9 s to 1.1 s on one thread. The output is byte-identical.
Contributor
|
Workflow [PR], commit [14f37e6] Summary: ✅
AI ReviewSummaryReviewed PR #131001 at Missing context / blind spots
Final VerdictStatus: ✅ Approve. No new inline findings; no prior review threads required action. |
alexey-milovidov
approved these changes
Nov 16, 2026
This was referenced Nov 18, 2026
zvonand
added a commit
to Altinity/ClickHouse
that referenced
this pull request
Nov 18, 2026
Antalya 26.3 Backport of ClickHouse#131001 - Reuse the GCM cipher context between rows in encrypt/decrypt
zvonand
added a commit
to Altinity/ClickHouse
that referenced
this pull request
Nov 18, 2026
Antalya 26.6 Backport of ClickHouse#131001 - Reuse the GCM cipher context between rows in encrypt/decrypt
zvonand
added a commit
to Altinity/ClickHouse
that referenced
this pull request
Nov 18, 2026
Stable 26.3 Backport of ClickHouse#131001 - Reuse the GCM cipher context between rows in encrypt/decrypt
zvonand
added a commit
to Altinity/ClickHouse
that referenced
this pull request
Nov 19, 2026
Stable 26.8 Backport of ClickHouse#131001 - Reuse the GCM cipher context between rows in encrypt/decrypt
zvonand
added a commit
to Altinity/ClickHouse
that referenced
this pull request
Nov 20, 2026
Antalya 26.8 Backport of ClickHouse#131001 - Reuse the GCM cipher context between rows in encrypt/decrypt
VighneshPath
pushed a commit
to VighneshPath/ClickHouse
that referenced
this pull request
Nov 23, 2026
Reuse the GCM cipher context between rows in encrypt/decrypt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #107339, which made
encrypt/decryptreuse the cipher context and key schedule between rows for every mode except GCM. GCM still set the context up from scratch on every row. In OpenSSL 3.x that frees and re-creates the provider context and updates the reference count of the cipher object that all threads share. With 16 threads that atomic update alone took about 43% of the time, so GCM barely sped up with more threads.This PR applies the same
CachedKeyStateapproach to the GCM branches. The cipher is passed only on the first row or after a failed decryption. The IV length and IV are set on every row, and the key only when it changes.A GCM round trip on 100M rows went from 23.7 s to 0.9 s (about 27x faster) with 16 threads, and on 10M rows from 3.9 s to 1.1 s (about 3.6x) on one thread. Output is byte-identical.
04337_encrypt_decrypt_block_modesgains GCM coverage, andencrypt_decrypt.xmlgains GCM performance cases.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improve performance of the
encrypt,decryptandtryDecryptfunctions in AES-GCM modes by up to 27x with many threads.Workflow [PR]
Sync PR [sync-upstream/pr/131001]
Version info
26.11.1.2481-master(included in26.11and later)