-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
string_decoder: optimize write() #1209
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var common = require('../common.js'); | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
|
||
var bench = common.createBenchmark(main, { | ||
encoding: ['ascii', 'utf8', 'base64-utf8', 'base64-ascii'], | ||
inlen: [32, 128, 1024], | ||
chunk: [16, 64, 256, 1024], | ||
n: [25e4] | ||
}); | ||
|
||
var UTF_ALPHA = 'Bl�b�rsyltet�y'; | ||
var ASC_ALPHA = 'Blueberry jam'; | ||
|
||
function main(conf) { | ||
var encoding = conf.encoding; | ||
var inLen = conf.inlen | 0; | ||
var chunkLen = conf.chunk | 0; | ||
var n = conf.n | 0; | ||
|
||
var alpha; | ||
var chunks = []; | ||
var str = ''; | ||
var isBase64 = (encoding === 'base64-ascii' || encoding === 'base64-utf8'); | ||
|
||
if (encoding === 'ascii' || encoding === 'base64-ascii') | ||
alpha = ASC_ALPHA; | ||
else if (encoding === 'utf8' || encoding === 'base64-utf8') | ||
alpha = UTF_ALPHA; | ||
else | ||
throw new Error('Bad encoding'); | ||
|
||
var sd = new StringDecoder(isBase64 ? 'base64' : encoding); | ||
|
||
for (var i = 0; i < inLen; ++i) { | ||
if (i > 0 && (i % chunkLen) === 0 && !isBase64) { | ||
chunks.push(new Buffer(str, encoding)); | ||
str = ''; | ||
} | ||
str += alpha[i % alpha.length]; | ||
} | ||
if (str.length > 0 && !isBase64) | ||
chunks.push(new Buffer(str, encoding)); | ||
if (isBase64) { | ||
str = new Buffer(str, 'utf8').toString('base64'); | ||
while (str.length > 0) { | ||
var len = Math.min(chunkLen, str.length); | ||
chunks.push(new Buffer(str.substring(0, len), 'utf8')); | ||
str = str.substring(len); | ||
} | ||
} | ||
|
||
var nChunks = chunks.length; | ||
|
||
bench.start(); | ||
for (var i = 0; i < n; ++i) { | ||
for (var j = 0; j < nChunks; ++j) | ||
sd.write(chunks[j]); | ||
} | ||
bench.end(n); | ||
} |
This file contains 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
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.
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.
I wonder if V8 will unroll the subsequent loop (or if there's value in doing so ourselves.)
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.
You mean for the
i = 3
case? I didn't try that since I figured people would frown about code duplication between thei = 3
andi = buflen
cases. I also did not see what kind performance difference it would make though.