-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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: minor refactoring #1870
fs: minor refactoring #1870
Conversation
311c3e4
to
b926718
Compare
Some points:
Other than that LGTM. |
@benjamingr The points I mentioned in the PR would be good enough to be in the commit messages, right? |
Right. I think so - check out some other PRs. |
statWatchers[filename]; | ||
} | ||
|
||
var statWatchers = Object.create(null); |
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.
Maybe make this const
while you're here.
LGTM with a suggestion. CI: https://jenkins-iojs.nodesource.com/view/iojs/job/iojs+any-pr+multi/755/ |
c18484f
to
40d4c54
Compare
@benjamingr @bnoordhuis I updated the PR as per the suggestions. Please review. |
40d4c54
to
5a5fc58
Compare
1. Changing `Bad arguments` error messages to a more helpful message `options should either be an object or a string`. 2. Made braces consistent. 3. Returning meaningful error message from `fs_event_wrap`'s `FSEvent`'s `Start` function.
5a5fc58
to
de2a7f1
Compare
`nullCheckCallNT` function is not necessary, as we can directly pass `callback` and `er` to `process.nextTick`
@benjamingr @bnoordhuis I included another commit which does very minor refactoring stuff. Please let me know if that commit is not worth sending in. I ll pull that out. |
@@ -583,11 +580,11 @@ fs.read = function(fd, buffer, offset, length, position, callback) { | |||
}; | |||
|
|||
fs.readSync = function(fd, buffer, offset, length, position) { | |||
var legacy = false; | |||
var legacy = false, encoding; |
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.
Would prefer encoding
to be its own var
statement.
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.
@chrisdickinson Done.
0f381df
to
d725865
Compare
I included a commit which makes |
32d9610
to
fcfd07e
Compare
@benjamingr @chrisdickinson @Fishrock123 I wrote a benchmark (this may not be good, please review and give feedback if you have time) and I found that the
The first run was from the latest |
I'm not comfortable with the FSReqWrap change. When I implemented that there were nuances about where it needed to be called. I'll want to review that change in more detail. |
@trevnorris Are you talking about this line? It was just moved up, because inside the |
@thefourtheye I see that now. Wrote my first comment from my phone and it wasn't easy to see the changes completely. Okay, LGTM. |
@trevnorris No problem. Thanks for reviewing :-) |
@@ -1112,14 +1114,14 @@ function writeAll(fd, buffer, offset, length, position, callback) { | |||
} | |||
|
|||
fs.writeFile = function(path, data, options, callback) { | |||
var callback = maybeCallback(arguments[arguments.length - 1]); | |||
callback = maybeCallback(arguments[arguments.length - 1]); |
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.
Reading and assigning to arguments at the same time is a deopt.
Not sure if this matters.
Edit: I was wrong, it is optimized in the strict mode, and in sloppy mode it is deoptimized even with a var callback
if the variable name is the same as one of the arguments.
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 is easy to prevent just by renaming the parameter to something like callback_
and leaving the var keyword where it was.
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.
Yup, there are many places in the file where we do this. So, I am doing this in a commit.
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.
even if writing to /dev/null
I doubt it could be run enough to notice a difference. and from the looks of it, nothing that way was actually changed. Just removed the unnecessary var
.
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 tested it (with v8 4.2) and it seems that I was wrong about the deopt, sorry.
'use strict'
changes some things related to arguments
handling, and it has an effect on the optimization.
@petkaantonov this should be probably mentioned in that example.
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.
@thefourtheye I'm using this test: petkaantonov/bluebird/wiki/Optimization-killers#1-tooling with recent iojs.
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.
@ChALkeR I tried something like this
function containsWith(a_) {
var a = arguments[0] + 1;
}
function containsWith(a) {
a = arguments[0] + 1;
}
but it still says the function is not optimized for both the cases.
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.
@ChALkeR it's actually a wikipage, you should be able to edit it :P
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 updated the PR with a commit which will use a different variable name.
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.
but it still says the function is not optimized for both the cases.
Have you enabled 'use strict'
for the file or for the function?
function unoptimized0(a, b) {
b = arguments[arguments.length - 1] + 1;
return b * 1;
}
function unoptimized1(a, b) {
var b = arguments[arguments.length - 1] + 1;
return b * 1;
}
function optimized0(a, b) {
'use strict';
b = arguments[arguments.length - 1] + 1;
return b * 1;
}
function optimized1(a, b) {
'use strict';
var b = arguments[arguments.length - 1] + 1;
return b * 1;
}
Edit: There was an error in the last function name.
f6cb789
to
858ee0d
Compare
@@ -91,16 +94,12 @@ function nullCheck(path, callback) { | |||
er.code = 'ENOENT'; | |||
if (typeof callback !== 'function') | |||
throw er; | |||
process.nextTick(nullCheckCallNT, callback, er); | |||
process.nextTick(callback, er); |
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.
glad there are others around to clean up my mess. thanks for catching it.
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.
@trevnorris Got your back ;-)
throw new TypeError('Bad arguments'); | ||
} else if (typeof options !== 'object') { | ||
throwOptionsError(options); | ||
} |
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.
looks like the {} were added for stylistic reasons?
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.
@trevnorris Yup, when I was preparing the patch, I am not sure why, but linter was complaining about throwOptionsError
being in that position. I had to use {}
to fix it. Now, linter is fine without {}
. Shall I revert it?
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.
@thefourtheye it is probably this eslint bug again. We already encountered it in #1742
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.
@targos Yup, that looks more like it. But, I just locally ran make lint
without the braces and it is fine. Don't know why. I think its better to leave it as it is now.
think the final list of commits should be:
If can do that if you'd like, or you can take care of it. Either way, LGTM. |
858ee0d
to
365bc72
Compare
@trevnorris I squashed the commits and edited the commit messages a little bit. Please have a look at them. |
Looks great. Just want to run CI one last time for sanity sake then I'll land these. https://jenkins-iojs.nodesource.com/job/iojs+any-pr+multi/797/ |
There were a total of 6 failures (1 on armv7-wheezy, 5 on win2008r2). None of them look related to this patch. Going to land. |
1. Change "Bad arguments" error messages to a more helpful message "options should either be an object or a string". 2. Make braces consistent. 3. Return meaningful error message from fs_event_wrap's FSEvent's Start function. PR-URL: nodejs#1870 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
`nullCheckCallNT()` function is not necessary, as we can directly pass `callback` and `er` to `process.nextTick()`. PR-URL: nodejs#1870 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
Remove `inStatWatchers` function and make `statWatchers` a `Map`. PR-URL: nodejs#1870 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1. Remove a few unnecessary variables to reduce LoC. 2. Remove redundant `var` definitions of variables in same function. 3. Refactor variables which are defined inside a block and used outside as well. 4. Refactor effect-less code. 5. In `rethrow` function, instead of assigning to `err` and throwing `err` directly throw `backtrace` object. 6. Reassign a defined parameter while also mentioning arguments in the body is one of the optimization killers. So, changing `callback` to `callback_` and declaring a new variable called `callback` in the body. PR-URL: nodejs#1870 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
Make SyncWriteStream non-enumerable since it's only for internal use. PR-URL: nodejs#1870 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
Landed in 09f2a67, 67a11b9, 8841132, a011c32 and 53a4eb3 with a few commit message grammatical changes. Thanks much for the work. Quick note, it's preferred to use the present in commit messages. e.g. "Change parameters" instead of "Changing parameters". Also preferred to not use personal pronouns. e.g. "New function foo() can return value x" instead of "We can return the value x from the new function foo()". |
We are getting rid of `inStatWatchers` function and making `statWatchers` a `Map`.
1. Removed a few unnecessary variables to reduce LoC 2. Removed redundant `var` definitions of variables in same function 3. Refactored variables which are defined inside a block and used outside as well 4. Refactored effect-less code 5. In `rethrow` function, instead of assigning to `err` and throwing `err`, we can directly throw `backtrace` object itself. 6. Reassigning a defined parameter while also mentioning arguments in the body is one of the optimization killers. So, changing `callback` to `callback_` and declaring a new variable called `callback` in the body.
As `SyncWriteStream` is only for internal use, it would be better if it is non-enumerable, so that a simple `console.log(require('fs'))` will not reveal it.
@trevnorris Thanks :) I ll remember the suggestions for the next time :) |
Bad arguments
messages tooptions should either be an object or a string
and returning meaningful error message fromfs_event_wrap
'sFSEvent
'sStart
function.nullCheckCallNT
function is not necessary, as we can directly passcallback
ander
toprocess.nextTick
inStatWatchers
function and makingstatWatchers
aMap
.rethrow
function, instead of assigning toerr
and throwingerr
, we can directly throwbacktrace
itself.SyncWriteStream
is only for internal use, it would be better if it is non-enumerable, so that a simpleconsole.log(require('fs'))
will not reveal it.