← 输入框|github.com|刷新
Skip to content

Reduce decrypt allocations - #765

Merged
appel1 merged 4 commits into
developfrom
perf/reduce-decrypt-allocations
Oct 3, 2026
Merged

appel1 merged 4 commits into
developfrom
perf/reduce-decrypt-allocations

Conversation

@appel1

@appel1 appel1 commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Decryption benchmarks comparing origin/develop with this branch showed lower allocations across all four cases:

Case Mean time (before → after) Allocated (before → after)
Agile XLSX 44.55 → 43.32 ms (2.8% faster) 482.6 → 249.6 KB (48.3% less)
Standard XLSX 25.13 → 22.54 ms (10.3% faster) 26,012 → 229.5 KB (99.1% less)
RC4 XLS 203.4 → 99.69 µs (51.0% faster) 888.1 → 110.8 KB (87.5% less)
XOR XLS 114.0 → 39.24 µs (65.6% faster) 646.9 → 81.5 KB (87.4% less)

Input files

  • Agile XLSX: agile_AES128_SHA1_CBC_pwd_password.xlsx
  • Standard XLSX: standard_AES128_SHA1_ECB_pwd_password.xlsx
  • RC4 XLS: Issue242_StdRc4PwdPassword.xls
  • XOR XLS: Issue242_XorPwdPassword.xls

appel1 and others added 4 commits September 28, 2026 22:05
Decrypting encrypted XLS/XLSX allocated heavily on hot paths. The biggest
offender was XlsBiffStream, which created a MemoryStream and a CryptoStream
for every decrypted chunk. RC4/XOR now call TransformBlock directly through a
static Transform helper, with the CryptoStream path kept as a fallback for
other transforms, and the input/output scratch buffers are reused.

Block decryptor setup ran per 1 KB block and again on every row-block seek.
EncryptionInfo gains a GenerateBlockKey overload that writes into a caller
supplied buffer, RC4Transform can be re-keyed in place via Reset, and the XOR
transform just resets its index, so no per-block key array or transform is
allocated. AgileEncryptedPackageStream reuses one cipher and IV buffer per
segment and uses DecryptCbc where available.

The Standard 50k-iteration key derivation loop allocated about 26 MB per open;
it now reuses the hash instance with TryComputeHash and a stack buffer on
netstandard2.1+, and a reused array on net462.

Measured with the new ReadRealWorldFiles benchmark (10 iterations):

  OldIssue11572_CodePage.xls  net10  146 ms / 760 MB -> 64 ms / 29 MB
                              net462 345 ms / 1420 MB -> 210 ms / 40 MB
  standard_AES128.xlsx        net10  26 ms / 26 MB -> 22 ms / 0.2 MB
                              net462 127 ms / 31 MB -> 119 ms / 20 MB
  agile_AES128.xlsx           net462 259 ms / 48 MB -> 241 ms / 39 MB
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@appel1
appel1 merged commit 8bc82cf into develop Oct 3, 2026
1 check passed
@appel1
appel1 deleted the perf/reduce-decrypt-allocations branch October 3, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant