-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reduce buffer creation for ctr mode #48
Conversation
self._cache.writeUInt32BE(out[0], offset + 0) | ||
self._cache.writeUInt32BE(out[1], offset + 4) | ||
self._cache.writeUInt32BE(out[2], offset + 8) | ||
self._cache.writeUInt32BE(out[3], offset + 12) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this really better than a copy
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The writeUint
is not better, but the reason I am doing this is that it avoids creating intermediary buffers. Before encryptBlock
creates a new buffer, fills it with those four values, just so that the buffer can be copied in here. By using the array directly there is now one less buffer allocation in the loop and together with the preallocation, no buffer allocations are in the loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, my mistake, I thought out
was a Buffer
.
ACK :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👌🏽
Why are you renaming the method?
…On Thu, Aug 17, 2017, 5:25 AM Daniel Cousens ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In modes/ctr.js
<#48 (comment)>
:
> exports.encrypt = function (self, chunk) {
- while (self._cache.length < chunk.length) {
- self._cache = Buffer.concat([self._cache, getBlock(self)])
+ var chunkNum = Math.ceil(chunk.length / blockSize)
+ var start = self._cache.length
+ self._cache = Buffer.concat([
+ self._cache,
+ Buffer.allocUnsafe(chunkNum * blockSize)
+ ])
+ for (var i = 0; i < chunkNum; i++) {
+ var out = getBlock(self)
+ var offset = start + i * blockSize
+ self._cache.writeUInt32BE(out[0], offset + 0)
+ self._cache.writeUInt32BE(out[1], offset + 4)
+ self._cache.writeUInt32BE(out[2], offset + 8)
+ self._cache.writeUInt32BE(out[3], offset + 12)
Nevermind, my mistake, I thought out was a Buffer.
ACK :)
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#48 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABE4nxIilA_HN1mmMna5KyGEAqdGWuR9ks5sZAckgaJpZM4O5kwc>
.
|
@calvinmetcalf that's just githubs diff I pulled part of the function into it's own function, as I needed that functionality |
oh my bad |
@calvinmetcalf could you release? |
hey @calvinmetcalf @dcousens , When do you think this can be released ? |
@calvinmetcalf @dcousens yes please! :) |
Can't release yet, my local tests throw:
Investingating within 24 hours |
When using ctr mode, I saw a large amount of buffer creation + gc when running perf analysis. This avoids generating most of the temporary buffers when running update in ctr mode improving perf in the browser nicely.