Improve the performance of ActiveSupport::MessageEncryptor #43924
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.
Summary
Following up on the work I did in #42919, this introduces the same logic to
ActiveSupport::MessageEncryptor
.By calculating the position of the IV and auth tags, instead of using
String#split
, we increase our resistance to invalid messages, those that don't feature the separator. The new code is not sensitive to the size of the input whenever it doesn't feature the separator.Besides the above, I'm making sure that the auth tag is 16 bytes, OpenSSL bindings' current default. A change to that default would break things for us, namely making it impossible to decrypt previously encrypted messages.
Other Information
Benchmarks
4 kB
Attempting to decrypt an invalid message (no separator).
Currently:
This PR:
A 1.37x speedup for
#decrypt_and_verify
, and a 1.10x speedup for#encrypt_and_sign
.Attempting to decrypt a valid message.
Currently:
This PR:
A 1.09x speedup for
#decrypt_and_verify
, and a 1.07x speedup for#encrypt_and_sign
.4 MB
Attempting to decrypt an invalid message (no separator).
Currently:
This PR:
A 326x speedup for
#decrypt_and_verify
, and a 1.16x speedup for#encrypt_and_sign
.Attempting to decrypt a valid message.
Currently:
This PR:
A 1.52x speedup for
#decrypt_and_verify
, and a 1.12x speedup for#encrypt_and_sign
.