-
Notifications
You must be signed in to change notification settings - Fork 653
Windows: update loop time lazily #1526
base: v1.x
Are you sure you want to change the base?
Conversation
cc @saghul , @piscisaureus |
I think I'd like that, actually :-) |
@txdv : to signify that it's private |
@orangemocha we don't really use that convention in libuv, basically everything except the data field in a loop is considered private. "time" is fine. |
Also, note the "txdv commented on 9c100d5 include/uv-win.h:L319 3 hours ago", you can derive by that that I commented on a specific line of code. You can click on that to get to that line and with comment on my comment on my comment directly, this way the conversation stays nicely formatted. |
@saghul That seems a little bug-prone. The unix side is using loop->time directly. I am worried about people adding some common code that accesses loop->time directly. I'd like them to get a compile error on Windows. |
Will you provide a unix patch? |
Currently, I am leaving the unix side as is. I am debating whether I should introduce uv__time() for unix as well. Let me publish the current change so you can see what I mean. Thanks! |
updated |
@saghul Didn't we agree to prefix internal functions with uv__? (double _) uv_update_time would be a perfect target for this. |
loop->cached_time_is_valid = 0; | ||
} | ||
|
||
uint64_t uv__time(const uv_loop_t* loop) { |
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.
uv__loop_time
maybe?
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.
sure
Yes. |
uv_update_time is not internal, I assumed for a moment that it was. Forget my stupid comment. |
Quick (perhaps stupid) idea: get rid of uv_update_time and modify uv_now (remove constness + docs) to do it. Also on Unix. |
To do what exactly? |
To do what uv_update_time currently does. |
Right now the time is being updated at the beginning of the event loop. Then you can call uv_now to get that 'cached' time. Now if you would have long(er) operation, you might want to update the time manually, which is why uv_update_time exists. You call it and then uv_now returns the new most currently cached time. Now if you merge uv_update_time and uv_now, and call uv_now a lot of times in a row, it will be reduce performance. The existence of uv_update_time lets us be as fast as possible in most situations while letting us be as accurate as possible when the developer invokes uv_update_time. I would like to merge that into one function as well, but I don't think that is possible. This makes me just realize that making uv_now lazy is kinda useless, because when the developer invokes uv_update_time he really means to update it now. |
|
I agree with @txdv:
uv_update_time is cheap to call and we call it any time we know the time might have changed. If we didn't use it, we would have to update the time every time we read it, which would be less performant and also change the behavior. It's not uv_now() that we need to worry about, but uv__loop_time(), which is used for timer manipulation. |
We call uv_update_time() whenever we think the loop time should be updated. But we might not need to read the time after every update, and the update operation has non-negligible cost. So I am making uv_update_time() invalidate the loop time. The time will be actually updated only on the first call to uv__time() after it's been invalidated.
Pushed a new commit with changes from code review feedback above. I am thinking it might be better to introduce uv__loop_time on Unix as well, to keep a consistent internal interface, even if doesn't do the caching there. What do you think? |
Why would someone not call uv_now after he called uv_update_time? uv_update_time is used internally though ... maybe it might make sense. |
Correct, uv_update_time is used internally, it seems mostly to support timer operations. |
Not is not, it is an exposed function in the uv.h header |
I meant it's used internally, in addition to being part of the public API. The internal use is for timers and it doesn't imply that uv_now() will be called afterwards. |
Yeah, that makes sense in that case. Saves maybe a few time queries. |
My point is that That said, what I proposed would make |
@piscisaureus mind throwing your 2 cents here? |
We call uv_update_time() whenever we think the loop time should be updated.
But we might not need to read the time after every update, and the update
operation has non-negligible cost.
So I am making uv_update_time() invalidate the loop time. The time will be
actually updated only on the first call to uv_now() after it's been
invalidated.