Repository navigation
Reduce per-row overhead of encrypt and decrypt - #124103
Conversation
Every row of the encrypt/decrypt loops called the virtual IColumn::getDataAt for the input, key, IV and AAD columns (plus once more in the output-size pre-pass), and for a constant argument ColumnConst::getDataAt forwards to a second virtual call into the nested column. The column kinds are fixed for the whole block, so resolve them once: String and FixedString are read through their final classes, a constant is read once, and anything else keeps the virtual call. Co-Authored-By: Claude Opus 5.5 <[email protected]>
In the ECB-composed path of encrypt and aes_encrypt_mysql for aes-*-ecb and aes-*-cbc, every row shorter than one AES block made its own EVP_EncryptUpdate call for a single 16-byte block, and that call chain (EVP_EncryptUpdate -> ossl_cipher_generic_block_update -> cipher_hw_aesni_ecb -> aesni_ecb_encrypt) costs more than the AES rounds themselves. Such a row has no chaining inside it: its ciphertext is ECB(pad(P)) for ECB and ECB(pad(P) XOR IV) for CBC. So build that block in place in the output and encrypt a run of them with one in-place EVP_EncryptUpdate, flushed before the key changes, before any longer row, every 4096 bytes and after the last row. In-place ECB on the same buffer is allowed by EVP and the AES-NI ECB kernel. tests/performance/encrypt_decrypt.xml queries, one thread, 3M rows, release build, fix/base on two CPUs: ECB encrypt 0.51/0.49, CBC encrypt 0.67/0.65, ECB encrypt+decrypt 0.68/0.67, CBC encrypt+decrypt 0.82/0.82, alternating keys 0.72/0.71, per-row IV 0.86/0.85. Rows of 16 bytes and longer keep the previous code. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Wrap literal names in the new FunctionsAES.h comments in inline code and drop a sentence that narrated the deferred-row path. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 findings over 2 runs on the same head).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-11:20261005-212500 |
|
Workflow [PR], commit [60474c4] Summary: ✅
AI ReviewSummaryThis PR reduces per-row overhead in Final Verdict
LLVM Coverage ReportMeasured on commit 60474c4.
Changed lines: Changed C/C++ lines covered: 142/146 (97.26%) · Uncovered code |
|
📊 Cloud Performance Report ✅ AI verdict: This PR only changes the AES encrypt/decrypt functions: it reads String and FixedString arguments without a virtual call per row, and it batches rows shorter than one block into a single ECB call. It also adds a stateless test. Neither clickbench nor tpch_adapted_1_official calls these functions, so no benchmark query runs the changed code. The only flagged result, clickbench Q34 at -5.4% (1256 to 1188 ms), was compared against a thin 5-run baseline and stays inside master's current range of 1093-1370 ms, so it is downgraded to not_sure. To measure the PR's real effect you would need a micro-benchmark that calls encrypt or decrypt on short strings. clickbenchFlagged queries (1 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes7 object files changed (+84.71 KiB total), 0 added.
719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units12 translation units recompiled, 22 s compile time in total, 12 of them have a recent master baseline. |
CI finish ledger - 60474c4Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20261006-040251 |
|
Fixing PR for the Stress test (arm_release) S3_ERROR No such key (05137, format_version.txt): #124298. |
5d34976
A table with `table_disk = true` keeps its parts and its root files (`format_version.txt`, `detached/`, `deduplication_logs/`, mutation entries) at the root of its disk, so two writable tables on one root overwrite and delete each other's data (issue ClickHouse#98512), and the stress test's AST fuzzer clones of such a table (`<name>__fuzz_N`, same disk) raced the original's `DROP` and left a metadata entry whose object was gone (`S3_ERROR` No such key on `__root/format_version.txt` in `Stress test (arm_release)`). CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=124103&sha=60474c44812b8fadcb983207ab8e87a4a1af41f5&name_0=PR&name_1=Stress%20test%20%28arm_release%29 PR: ClickHouse#124103 A writable `table_disk` table now owns the root of its disk (storage endpoint + namespace + disk path; nested paths conflict) until the instance is destroyed; another writable table on that root is refused with `BAD_ARGUMENTS`. Read-only and write-once disks are not owners, so the one-writer/many-readers setup keeps working. The root is not released when `dropAllData` finishes: the rest of the drop (`dropReplica`, removing `store/<uuid>` and the metadata) can still throw and re-queue the table, and after a restart the dropped table is rebuilt from `metadata_dropped` before the other tables are loaded, so a released root could be taken by another table whose parts the retried drop would then delete. `DatabaseCatalog` destroys the dropped storage before it notifies the `DROP ... SYNC` waiters, so `DROP ... SYNC` followed by a `CREATE` on the same disk works; the new `database_catalog_drop_finally_after_notify` failpoint pauses the drop job right after that notification, so a test can check it. A table dropped or detached without `SYNC` keeps the root until its instance is gone, and the error says to retry after that (`SYNC` waits for it). An `ALTER` cannot change the disk or the storage policy of a `table_disk` table: the new disks would join the table without being owned, and `RESET SETTING disk` moved the table onto the `default` disk with the server's data directory as its data path, so a later `DROP TABLE` deleted that whole directory. The default database of `clickhouse-local` is a writable `Overlay` over an `Atomic` database, which keeps a detached table instance until no query uses it. `Overlay` did not forward `checkDetachedTableNotInUse` and `waitDetachedTableNotInUse`, so an `ATTACH` did not first destroy the unused detached instance and `DETACH ... SYNC` did not wait; with the ownership above that refused `DETACH TABLE t; ATTACH TABLE t`. Both are now forwarded to every member, as the server's `Atomic` database does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #81944
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
encryptandaes_encrypt_mysqlare faster on values shorter than 16 bytes in theaes-*-ecbandaes-*-cbcmodes (up to 2x forencryptwithaes-128-ecb).Description
Performance Comparison (amd_release)keeps reportingencrypt_decrypt#0/#1/#2/#4/#5/#13as slower in unrelated PRs and on master (#81944 run). The code ofencryptand of the OpenSSL functions it calls is byte-identical in the slow build and in fast builds on the same base; in the slow build the extra time is in the per-row loop ofencrypt, which for every row makes 3-5 virtualgetDataAtcalls and, for a value shorter than one AES block, oneEVP_EncryptUpdate(three levels of OpenSSL provider dispatch) for a single 16-byte block.Two commits:
StringandFixedStringare read through their final classes, a constant once, anything else keeps the virtual call. All four encrypt/decrypt loops.aes-*-ecb/aes-*-cbcencryption, a row shorter than 16 bytes is one block with no chaining, so the block is built in place in the output and a run of such rows is encrypted with one in-placeEVP_EncryptUpdate. Longer rows and decryption are not batched.tests/performance/encrypt_decrypt.xml, release build, one thread, new time divided by old time, measured on two CPUs:#00.51 and 0.49,#10.67 and 0.65,#40.68 and 0.67,#50.82 and 0.82,#100.72 and 0.71,#130.86 and 0.85,#20.94 and 0.93; the other queries of the test, and encryption of values of 16 bytes or more, are at most 2% slower. Outside the test,aes_encrypt_mysql('aes-128-cbc', ...)on short values: 0.84 and 0.85. This makes the loop cheaper; I have not shown that it stops the placement-dependent flips.New test
05331_encrypt_decrypt_argument_columns: FixedString, constant and String argument mixes for every mode, and hashes of many-row blocks mixing short and long values, key changes and per-row IVs, produced by the previous implementation.Workflow [PR]
Sync PR [sync-upstream/pr/124103]
Version info
26.10.1.1752-master(included in26.10and later)