Skip to content
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

Timers clean up #8603

Merged
merged 1 commit into from Feb 21, 2016
Merged

Timers clean up #8603

merged 1 commit into from Feb 21, 2016

Conversation

@benschulz
Copy link
Contributor

benschulz commented Nov 19, 2015

This PR splits the ActiveTimers abstraction into

  • OneshotTimers for scheduling "arbitrary" oneshot timers, such as XHR timeouts, and
  • JsTimers, based on OneshotTimers, for scheduling JS timers (setTimeout/setInterval).

The result is mich cleaner and the timer initialization steps now closely resemble the specification.

Notes

  • The second and third commit are strictly renames and code rearrangements.
  • I'm not particularily happy with the OneshotTimerCallback enum and its circular dependency with XHRTimeoutCallback, but I couldn't come up with anything better.

Review on Reviewable

@highfive
Copy link

highfive commented Nov 19, 2015

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!
@jdm jdm self-assigned this Nov 19, 2015
@bors-servo
Copy link
Contributor

bors-servo commented Nov 27, 2015

The latest upstream changes (presumably #8685) made this pull request unmergeable. Please resolve the merge conflicts.

@jdm
Copy link
Member

jdm commented Dec 30, 2015

Dreadfully sorry for the long delay. I started this review several times and it always turned out to be more complex than I expected, and then there was a week long conference and seasonal holidays...
Anyways, I like many things about this refactor. I think there are a couple ways to make some code clearer, but overall it's an improvement!
-S-awaiting-review +S-needs-code-changes


Reviewed 5 of 5 files at r1.
Review status: all files reviewed at latest revision, 8 unresolved discussions, some commit checks failed.


components/script/dom/window.rs, line 425 [r1] (raw file):
nit: these arguments should match the indentation of the previous line


components/script/dom/window.rs, line 434 [r1] (raw file):
nit: indentation


components/script/dom/window.rs, line 448 [r1] (raw file):
nit: indentation


components/script/dom/window.rs, line 457 [r1] (raw file):
nit: indentation


components/script/timers.rs, line 144 [r1] (raw file):
The previous code could become

{
    let mut timers = self.timers.borrow_mut();
    let insertion_index = timers.binary_search(&timer).err().unwrap();
    timers.insert(insertion_index, timer);
}

if self.is_next_timer(OneshotTimerHandler(new_handle)) {

components/script/timers.rs, line 338 [r1] (raw file):
nit: indentation


components/script/timers.rs, line 366 [r1] (raw file):
I find this block confusing, since it would make more sense to add the entry inside initialize_and_schedule and avoid storing an Option value inside JSTimerEntry. We should be able to use the hashtable Entry API to perform a get-or-insert operation, even if it does change the ordering of the steps that we're following.


components/script/timers.rs, line 475 [r1] (raw file):
nit: && should go on the previous line


Comments from the review on Reviewable.io

@benschulz benschulz force-pushed the benschulz:timers-clean-up branch from ca17c22 to 32974b7 Jan 3, 2016
@benschulz
Copy link
Contributor Author

benschulz commented Jan 3, 2016

Again, no need to apologize. ;)

Branch was rebased.


Review status: 0 of 5 files reviewed at latest revision, all discussions resolved.


Comments from the review on Reviewable.io

@jdm
Copy link
Member

jdm commented Jan 5, 2016

@bors-servo: r+
Whee!


Reviewed 5 of 5 files at r2.
Review status: all files reviewed at latest revision, all discussions resolved.


Comments from the review on Reviewable.io

@bors-servo
Copy link
Contributor

bors-servo commented Jan 5, 2016

📌 Commit 32974b7 has been approved by jdm

@bors-servo
Copy link
Contributor

bors-servo commented Jan 5, 2016

Testing commit 32974b7 with merge b6541b3...

bors-servo added a commit that referenced this pull request Jan 5, 2016
Timers clean up

This PR splits the `ActiveTimers` abstraction into

 - `OneshotTimers` for scheduling "arbitrary" oneshot timers, such as XHR timeouts, and
 - `JsTimers`, based on `OneshotTimers`, for scheduling JS timers (`setTimeout`/`setInterval`).

The result is mich cleaner and the timer initialization steps now closely resemble the specification.

**Notes**
 - The second and third commit are strictly renames and code rearrangements.
 - I'm not particularily happy with the `OneshotTimerCallback` enum and its circular dependency with `XHRTimeoutCallback`, but I couldn't come up with anything better.

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8603)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Jan 5, 2016

💔 Test failed - mac-rel-wpt

@jdm
Copy link
Member

jdm commented Jan 5, 2016

  ▶ CRASH [expected OK] /dom/interfaces.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /dom/ranges/Range-compareBoundaryPoints.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /dom/ranges/Range-mutations.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /dom/ranges/Range-set.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /html/dom/interfaces.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /html/dom/reflection-embedded.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /html/dom/reflection-grouping.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /html/dom/reflection-forms.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /html/dom/reflection-tabular.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /html/dom/reflection-text.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /_mozilla/mozilla/weakref.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /_mozilla/mozilla/trace_null.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

  ▶ CRASH [expected OK] /_mozilla/mozilla/prototypes.html
  │ 
  │ 
  └ thread 'ScriptTask PipelineId { namespace_id: PipelineNamespaceId(0), index: PipelineIndex(0) }' has overflowed its stack

...curious

@bors-servo
Copy link
Contributor

bors-servo commented Jan 10, 2016

The latest upstream changes (presumably #9201) made this pull request unmergeable. Please resolve the merge conflicts.

@benschulz
Copy link
Contributor Author

benschulz commented Jan 10, 2016

Ah, the object graph contains the following reference cycle.

OneshotTimers.timers (timers.rs:39)
OneshotTimer.callback (timers.rs:60)
OneshotTimerCallback::JsTimer.0 (timers.rs:70)
JsTimerTask.timers (timers.rs:291)
JsTimerState.oneshots (timers.rs:272)
OneshotTimers.timers (timers.rs:39)

"Obviously" the derived JSTraceables will traverse this cycle endlessly. The only workaround I can come up with right now is exploiting that JsTimerState.oneshots is non-owning, i.e. manually implementing JSTraceable for JsTimerState ignoring the oneshots field. That feels really dirty though. Instead, I'd really like to replace JsTimerState.oneshots with a GlobalRef, but that's a hen-egg-situation. :(

I'll think about it some more.

@benschulz
Copy link
Contributor Author

benschulz commented Jan 11, 2016

Well, I got rid of the cycle by turning the Rc pointer to OneshotTimers into a Weak one. But for that to work I had to impl<T> JSTraceable for Weak<T>. For that to break the cycle, the implementation must not trace whatever is behind the Weak pointer. I'm not sure that's a good idea. In fact, I have a feeling that it's a lousy idea. Comments? Other ideas?

@jdm
Copy link
Member

jdm commented Jan 11, 2016

Eeep. I'll need to think about this problem when I don't have a headache.

@benschulz
Copy link
Contributor Author

benschulz commented Jan 21, 2016

I thoughty about this again today. Originally I introduced the cycle because I didn't want to give JsTimers ownership of OneshotTimers. Code quality-wise that's a valid goal, but it's probably not worth having to consider reference cycles wrt. GC now. Maybe later, when a worthwile feature depends on it. ;)

So, how about I simply transfer ownership of OneshotTimers to JsTimers and get rid of the cycle to begin with?

@jdm
Copy link
Member

jdm commented Feb 16, 2016

Yes, I am in favour of removing the cycle and not having to think about this. Let me know if you'd like me to do the rebase and make the change!

@jdm jdm removed the S-awaiting-review label Feb 16, 2016
@benschulz
Copy link
Contributor Author

benschulz commented Feb 16, 2016

I will get to it tomorrow. Thanks for offering though. =)

@benschulz benschulz force-pushed the benschulz:timers-clean-up branch from a606fcd to c75e740 Feb 17, 2016
@benschulz
Copy link
Contributor Author

benschulz commented Feb 17, 2016

Alright, there was another cycle required to let repeating timers to update JsTimers.active_timers. I got rid of it, but it turned out uglier than I would like. I still like the overall refactoring because it's closer to the spec. Of course it's up to you, whether you still want to merge it. ;)

To facilitate reviewing the change I haven't squashed yet. Feel free to squash yourself or let me know and I'll do it.

@bors-servo
Copy link
Contributor

bors-servo commented Feb 18, 2016

The latest upstream changes (presumably #9683) made this pull request unmergeable. Please resolve the merge conflicts.

@jdm
Copy link
Member

jdm commented Feb 18, 2016

Looks good, although I might be tempted to expose set_js_timer_or_interval and clear_js_timer_or_interval instead of get_js_timers. While rebasing, could you make sure that the indentation is correct for method calls with arguments on multiple lines?

@jdm jdm removed the S-awaiting-review label Feb 18, 2016
The code was split into the following two abstractions.
 - OneshotTimers can be used to schedule arbitrary oneshot timers, such
   as XHR-Timeouts.
 - JsTimers (`setTimeout` and `setInterval`) which use OneshotTimers to
   schedule individual callbacks.

With this change the implementation (of JsTimers in particular) is in
much closer alignment with the specification.
@benschulz benschulz force-pushed the benschulz:timers-clean-up branch from c75e740 to f2d4a7b Feb 20, 2016
@benschulz
Copy link
Contributor Author

benschulz commented Feb 20, 2016

Looks good, although I might be tempted to expose set_js_timer_or_interval and clear_js_timer_or_interval instead of get_js_timers. While rebasing, could you make sure that the indentation is correct for method calls with arguments on multiple lines?

Done and done.

@jdm
Copy link
Member

jdm commented Feb 21, 2016

@bors-servo
Copy link
Contributor

bors-servo commented Feb 21, 2016

📌 Commit f2d4a7b has been approved by jdm

@bors-servo
Copy link
Contributor

bors-servo commented Feb 21, 2016

Testing commit f2d4a7b with merge 8f27810...

bors-servo added a commit that referenced this pull request Feb 21, 2016
Timers clean up

This PR splits the `ActiveTimers` abstraction into

 - `OneshotTimers` for scheduling "arbitrary" oneshot timers, such as XHR timeouts, and
 - `JsTimers`, based on `OneshotTimers`, for scheduling JS timers (`setTimeout`/`setInterval`).

The result is mich cleaner and the timer initialization steps now closely resemble the specification.

**Notes**
 - The second and third commit are strictly renames and code rearrangements.
 - I'm not particularily happy with the `OneshotTimerCallback` enum and its circular dependency with `XHRTimeoutCallback`, but I couldn't come up with anything better.

<!-- Reviewable:start -->
[<img src="https://reviewable.io/review_button.png" height=40 alt="Review on Reviewable"/>](https://reviewable.io/reviews/servo/servo/8603)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Feb 21, 2016

@bors-servo bors-servo merged commit f2d4a7b into servo:master Feb 21, 2016
2 checks passed
2 checks passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
@jdm
Copy link
Member

jdm commented Feb 21, 2016

Thanks @benschulz!

@benschulz benschulz deleted the benschulz:timers-clean-up branch Feb 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.