-
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
Lazy stat dates #12818
Lazy stat dates #12818
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,5 +98,27 @@ fs.stat(__filename, common.mustCall(function(err, s) { | |
console.log(`isSymbolicLink: ${JSON.stringify(s.isSymbolicLink())}`); | ||
assert.strictEqual(false, s.isSymbolicLink()); | ||
|
||
assert.ok(s.atime instanceof Date); | ||
assert.ok(s.mtime instanceof Date); | ||
assert.ok(s.ctime instanceof Date); | ||
assert.ok(s.birthtime instanceof Date); | ||
})); | ||
|
||
fs.stat(__filename, common.mustCall(function(err, s) { | ||
const json = JSON.parse(JSON.stringify(s)); | ||
const keys = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On second thought: either put a comment saying why we exclude There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just found out need to do the same for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i added an |
||
'dev', 'mode', 'nlink', 'uid', | ||
'gid', 'rdev', 'ino', | ||
'size', 'atime', 'mtime', | ||
'ctime', 'birthtime' | ||
]; | ||
if (!common.isWindows) { | ||
keys.push('blocks', 'blksize'); | ||
} | ||
keys.forEach(function(k) { | ||
assert.ok( | ||
json[k] !== undefined && json[k] !== null, | ||
k + ' should not be null or undefined' | ||
); | ||
}); | ||
})); |
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.
Can eliminate some code duplication here by having a factory function for the getters... e.g.
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.
Another idea here would be to use another defineProperty within the getter to replace the value once the date is created...
The getter is called once, during which time it is deleted, and the static value is set... and will be returned every time after. Should have a slightly better performance profile.
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.
@TimothyGu suggested that in the previous PR, it wasn't performant enough for the "single access" case - #12607 (comment)
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.
Ok, that's fine then. I'd still suggest the factory function to avoid the code duplication
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.
Hey, yeah I have tried these variations.
Unfortunately a single
defineProperty
is more expensive than 4 date creations, so you'd lose the benefits of the laziness if you need to access even a single date.this[somevariable]
is slow enough compared tothis.mtime
that the difference becomes pretty significant in a function like stat, that isn't very heavy to begin with. That drops accessing one time-field twice (my test isif (stat.ctime.getTime() !== stat.ctime.getTime()) throw err;
) to a 65% improvement over the non-lazy code, down from a 105% improvement if done the ugly way. But I can definitely change to that if it's preferable.. I didn't write it this way because I think it's beautiful. :)