Add optional buffer argument to Cipher#final#990
Add optional buffer argument to Cipher#final#990segiddins wants to merge 1 commit intoruby:masterfrom
Conversation
This adds an optional buffer argument to the Cipher#final method, matching the behavior of Cipher#update. When a buffer is provided, the output is written to it and the same buffer object is returned. Otherwise, a new string is created and returned. The implementation follows the same pattern as update: - Accepts an optional string buffer parameter - Reuses the buffer if provided, creating a new string if not - Automatically resizes the buffer as needed - Forces ASCII-8BIT encoding for binary cipher output - Raises FrozenError if the buffer is frozen
|
Why is this needed or desirable? |
|
It allows using a cipher to only require a single string allocation, by reusing the buffer passed to update, as opposed to allocating a second string inside |
|
Have you measured the performance impact?
|
|
On one request I was profiling, we were allocating 140784 objects there just for |
|
You'd hit this path 140k times if you encrypt or decrypt 140k strings. But wouldn't the initialization work in OpenSSL dominate the overall time, making any savings here negligible? If we add this, I'd prefer it to be a new method rather than extending |
This adds an optional buffer argument to the Cipher#final method, matching the behavior of Cipher#update. When a buffer is provided, the output is written to it and the same buffer object is returned. Otherwise, a new string is created and returned.
The implementation follows the same pattern as update: