-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
src: fix negative values in process.hrtime() #4757
Conversation
LGTM if CI and @trevnorris are happy |
@@ -24,3 +24,9 @@ function validateTuple(tuple) { | |||
assert(isFinite(v)); | |||
}); | |||
} | |||
|
|||
const base = process.hrtime(); | |||
for (let i = 0; i < 1e5; i += 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.
I think using assert(process.hrtime([0, 999999999])[1] >= 0);
as @ChALkeR suggested would be better than my test.
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.
assert(process.hrtime([0, 1e9 - 1])[1] >= 0);
then, I think.
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.
well if we really want to be sure then assert(process.hrtime([0, -1 >>> 0])[1] >= 0)
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? I doubt that's valid input for process.hrtime
.
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.
Because it properly decremented the seconds for any uint32 ns passed previous to my change. Therefore it should probably continue to work?
@bnoordhuis since the code previously was doing
Whereas with this patch (and before the patch) it shows as something like EDIT: Seems the math used to not care how large a value in the uint32 range was passed. e.g.
|
Fix a regression introduced in commit 89f056b ("node: improve performance of hrtime()") where the nanosecond field sometimes had a negative value when calculating the difference between two timestamps. Fixes: nodejs#4751
@trevnorris Is that valid?
|
@ChALkeR Is passing a larger seconds value supposed to return a negative value? If not then there's still a regression with this patch. |
@trevnorris Hm. As I read the doc, the passed value should be a result of some previous call to As it looks to me, I would say that the current behavior introduced by this patch is ok. If it could be altered to return expected values for other cases (when the passed in value is not a result of some a previous call) without considerable drawbacks (speed, code readability, etc), perhaps that should be done to keep us on the safe side. But I don't see a problem in not doing that. |
I see. Not trying to replicate all the old behavior, just the correct part. LGTM. CI is happy. |
@trevnorris Btw, I have just checked the old behavior. It also does not support values that are larger that the current ones, negative values, and arbitrary large numbers in nanosecond field. Examples (on 5.0.0): > process.hrtime()
[ 741222, 372827150 ]
> process.hrtime([0,0])
[ 741226, 252473283 ]
> process.hrtime([740000,0])
[ 1237, 26668592 ]
> process.hrtime([750000,0])
[ 1266866129, 303942850 ]
> process.hrtime([-1,0])
[ 1267616149, 293166365 ]
> process.hrtime([0,999999999])
[ 741274, 29108495 ]
> process.hrtime([0,99999999999999])
[ 741311, 514787310 ]
> process.hrtime([0,0])
[ 741404, 826126044 ]
> process.hrtime([0,-1])
[ 741402, 130741800 ] Everything that has negative values gives wrong results here, |
@ChALkeR If the ns field is a valid uint32 then the seconds used to decrement correctly because the values were combined in a int64 on the native side before doing the subtraction. |
Updated the test to just do Apropos the discussion about "user-cooked" values, I agree with @ChALkeR that GIGO applies. If you throw in the wrong numbers, don't expect the right answers to come out. |
Looks like the linter isn't happy. |
Style fix-up pushed. Running the CI one more time, last time one of the windows buildbots crapped out: https://ci.nodejs.org/job/node-test-pull-request/1320/ |
Cool. Just wanted to clear up the line between accidentally working and expected behavior. Windows CI failures are not related. |
LGTM |
Fix a regression introduced in commit 89f056b ("node: improve performance of hrtime()") where the nanosecond field sometimes had a negative value when calculating the difference between two timestamps. Fixes: nodejs#4751 PR-URL: nodejs#4757 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Landed in 1e5a026. |
Fix a regression introduced in commit 89f056b ("node: improve performance of hrtime()") where the nanosecond field sometimes had a negative value when calculating the difference between two timestamps. Fixes: #4751 PR-URL: #4757 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
89f056b did not land on LTS. As such I am adding a Please feel free to chime in you you have questions or concerns |
Fix a regression introduced in commit 89f056b ("node: improve performance of hrtime()") where the nanosecond field sometimes had a negative value when calculating the difference between two timestamps. Fixes: nodejs#4751 PR-URL: nodejs#4757 Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Fix a regression introduced in commit 89f056b ("node: improve
performance of hrtime()") where the nanosecond field sometimes
had a negative value when calculating the difference between two
timestamps.
Fixes: #4751
R=@trevnorris
[Can't run the CI - https://ci.nodejs.org/ times out.]