Repository navigation
Reduce decrypt allocations - #765
Merged
Merged
Conversation
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>
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.
Decryption benchmarks comparing
origin/developwith this branch showed lower allocations across all four cases:Input files
agile_AES128_SHA1_CBC_pwd_password.xlsxstandard_AES128_SHA1_ECB_pwd_password.xlsxIssue242_StdRc4PwdPassword.xlsIssue242_XorPwdPassword.xls