-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
fs: improve readFile
performance
#718
Conversation
Mostly LGTM. I still need to test it. Are we going to slowly move everything over to |
I think we could use |
@micnic note the messaging seems to me that |
@trevnorris note that at current, // in io.js 1.1.0 repl
(function() {
(function() {
"use strict"
console.time('let')
let result = 0
for (let i = 0; i < 10000000; i++) {
let lettest = 'foo bar baz';
let x = i * i
result += x
}
console.timeEnd('let')
return result
})();
(function() {
"use strict"
console.time('var')
var result = 0
for (var i = 0; i < 10000000; i++) {
var lettest = 'foo bar baz'
var x = i * i
result += x
}
console.timeEnd('var')
return result
})()
})()
// let: 1183ms
// var: 15ms Maybe this benchmark is bogus though. |
@timoxley , |
It seems that the only reasons to introduce for (var i = 0; i < 10; i++) {
setImmediate(function() {
console.log(i);
})
} Fixed simply by swapping That said, if |
@timoxley I wouldn't say it's bogus. You can see with I believe that's mostly been fixed in V8 HEAD but for now, we should probably scale back a little on const/let-ification, at least until we upgrade again. |
3048035
to
480f753
Compare
Removed all |
return callback(er); | ||
}); | ||
} | ||
var req = new FSReqWrap(); |
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.
It would be even neater to fold the FSReqWrap object into the ReadFileContext object. Something for another time, though.
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.
All the FSReqWrap()
object are one use, which means they could be reused for each step. Preventing the need to instantiate more objects. Food for thought.
18ea8d3
to
3875d97
Compare
var length; | ||
|
||
if (size === 0) { | ||
buffer = this.buffer = new Buffer(8192); |
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'd recommend changing this to new SlowBuffer(8192);
. Just do a var SlowBuffer = require('buffer').SlowBuffer;
at the top of the file.
3875d97
to
e863574
Compare
I'm not sure that reusing |
@vkurchatkin Awesome job on the callback hoisting BTW. That had been bothering me for a while. Changes LGTM. |
@trevnorris can you confirm that using one |
@vkurchatkin ah. no, definitely not safe doing that right now. will require some additional code. let's save that for another PR. |
26716f8
to
92a00ff
Compare
This commit improves `readFile` performance by reducing number of closure allocations and using `FSReqWrap` directly.
92a00ff
to
4ae0f04
Compare
This commit improves `readFile` performance by reducing number of closure allocations and using `FSReqWrap` directly. PR-URL: #718 Reviewed-By: Trevor Norris <[email protected]>
@trevnorris thank! dropped that commit, fixed some style issues, squashed and landed in e653080 |
@vkurchatkin great! thanks for your work on this. |
do we have any perf numbers on this for a changelog entry @vkurchatkin? otherwise it's just handwavy. |
@rvagg Numbers would be nice, but if the performance numbers are the same having hoisted out the callbacks is a big win for doing performance analysis. |
it's sort of hard to measure. It doesn't make reading files significantly faster, but it's supposed to block main thread less |
@trevnorris I was looking at lib/fs.js and lib/buffer.js today and I confess it's not immediately obvious to me why a SlowBuffer is better than a regular Buffer. Secondly, I think I spotted a minor bug. This PR calls function SlowBuffer(length) {
length = length >>> 0;
if (length > kMaxLength) {
throw new RangeError('Attempt to allocate Buffer larger than maximum ' +
'size: 0x' + kMaxLength.toString(16) + ' bytes');
}
var b = new NativeBuffer(length);
alloc(b, length);
return b;
} I think the |
@bnoordhuis It's better because |
@bnoordhuis hah, yeah. you're right about |
This PR improves
readFile
performance by reducing number of closure allocations and usingFSReqWrap
directly.see #704
R=@trevnorris, @petkaantonov