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

Using OnceCell<T> from Mitochondria #18059

Merged
merged 1 commit into from Sep 21, 2017
Merged

Using OnceCell<T> from Mitochondria #18059

merged 1 commit into from Sep 21, 2017

Conversation

@sendilkumarn
Copy link
Contributor

sendilkumarn commented Aug 13, 2017


  • ./mach build -d does not report any errors
  • ./mach test-tidy does not report any errors
  • These changes fix #13402 (github issue number if applicable).

This change is Reviewable

@highfive
Copy link

highfive commented Aug 13, 2017

Heads up! This PR modifies the following files:

  • @fitzgen: components/script/Cargo.toml, components/script/lib.rs, components/script/dom/bindings/js.rs
  • @KiChjang: components/script/Cargo.toml, components/script/lib.rs, components/script/dom/bindings/js.rs
@highfive
Copy link

highfive commented Aug 13, 2017

warning Warning warning

  • These commits modify unsafe code. Please review it carefully!
  • These commits modify script code, but no tests are modified. Please consider adding a test!
@sendilkumarn sendilkumarn changed the title Mito using oncecell in Mitochondria Aug 13, 2017
@nox nox changed the title using oncecell in Mitochondria using OnceCell<T> in Mitochondria Aug 13, 2017
@nox nox changed the title using OnceCell<T> in Mitochondria Using OnceCell<T> in Mitochondria Aug 13, 2017
@nox
Copy link
Member

nox commented Aug 13, 2017

Checking files for tidiness...
./components/script/dom/bindings/js.rs:395: trailing whitespace
./components/script/dom/bindings/js.rs:442: trailing whitespace
./components/script/dom/bindings/js.rs:462: trailing whitespace
./components/script/dom/bindings/js.rs:496: trailing whitespace
./components/script/dom/bindings/js.rs:521: trailing whitespace
./components/script/dom/bindings/js.rs:759: no newline at EOF
./components/script/dom/bindings/js.rs:462: extra space after (
@nox nox added the S-fails-tidy label Aug 13, 2017
/// on `JS<T>`.
#[must_root]
pub struct OnceCellJS<T: DomObject> {
ptr: OnceCell<Option<JS<T>>>,

This comment has been minimized.

@nox

nox Aug 13, 2017

Member

There should be no Option<T> here.


/// Retrieve a copy of the current inner value. If it is `None`, it is
/// initialized with the result of `cb` first.
pub fn or_init<F>(&self, cb: F) -> Root<T>

This comment has been minimized.

@nox

nox Aug 13, 2017

Member

This should call OnceCell::init_once.

debug_assert!(thread_state::get().is_layout());
match self.ptr.as_ref() {
Some(ptr_ref) => {
ptr::read(ptr_ref).map(|js| js.to_layout())

This comment has been minimized.

@nox

nox Aug 13, 2017

Member

Why ptr::read?

unsafe {
match self.ptr.as_ref() {
Some(ptr_ref) => {
ptr::read(ptr_ref).map(|o| Root::from_ref(&*o))

This comment has been minimized.

@nox

nox Aug 13, 2017

Member

Why ptr::read?

pub fn get(&self) -> Option<Root<T>> {
debug_assert!(thread_state::get().is_script());
unsafe {
match self.ptr.as_ref() {

This comment has been minimized.

@nox

nox Aug 13, 2017

Member

Use Option::map instead of a match expression.

}

/// Set this `OnceCellJS` to the given value. If it is not already set
pub fn set(&self, val: Option<&T>) {

This comment has been minimized.

@nox

nox Aug 13, 2017

Member

This method shouldn't exist at all.

This comment has been minimized.

@sendilkumarn

sendilkumarn Aug 14, 2017

Author Contributor

using init_once straight away in or_init method causes the following error

expected type `core::option::Option<T>`
 found type `core::option::Option<&dom::bindings::js::JS<T>>`

This comment has been minimized.

@sendilkumarn

sendilkumarn Aug 14, 2017

Author Contributor

and if I pass it to this method it kinda works.

This comment has been minimized.

@nox

nox Aug 14, 2017

Member

This method shouldn't exist anyway.

}

/// Gets the current value out of this object and sets it to `None`.
pub fn take(&self) -> Option<Root<T>> {

This comment has been minimized.

@nox

nox Aug 13, 2017

Member

This method shouldn't exist at all.

}
}

impl<T: DomObject> PartialEq for OnceCellJS<T> {

This comment has been minimized.

@nox

nox Aug 13, 2017

Member

This should be derived.

#[cfg(not(debug_assertions))]
let trace_info = "for DOM object on heap";

match self.ptr.as_ref().clone() {

This comment has been minimized.

@nox

nox Aug 13, 2017

Member

Just call trace on the result of self.ptr.as_ref().

@@ -603,4 +756,4 @@ unsafe impl<T: DomObject> JSTraceable for Root<T> {
unsafe fn trace(&self, _: *mut JSTracer) {
// Already traced.
}
}
}

This comment has been minimized.

@nox

nox Aug 13, 2017

Member

Nit: missing new line.

@nox nox assigned nox and unassigned wafflespeanut Aug 13, 2017

/// Get a rooted value out of this object
#[allow(unrooted_must_root)]
pub fn get(&self) -> Option<Root<T>> {

This comment has been minimized.

@KiChjang

KiChjang Aug 14, 2017

Member

I don't think this is the right return type. The whole idea behind the issue is that whenever I call the get method, I shouldn't need to worry about whether the cell is empty or not, and that it will always get the appropriate value for me.

This comment has been minimized.

@sendilkumarn

sendilkumarn Aug 15, 2017

Author Contributor

That makes sense. Since MutNullableJS and MutJS returns Root<T> , I decided to return something similar. Rather here I will return Option<JS<T>>

This comment has been minimized.

@nox

nox Aug 15, 2017

Member

No, Option<JS<T>> shouldn't be returned, but Option<&T>.

@sendilkumarn sendilkumarn changed the title Using OnceCell<T> in Mitochondria Using OnceCell<T> from Mitochondria Aug 15, 2017
/// Retrieve a copy of the current inner value. If it is `None`, it is
/// initialized with the result of `cb` first.
#[allow(unrooted_must_root)]
pub unsafe fn or_init<F>(&self, cb: F) -> &JS<T>

This comment has been minimized.

@KiChjang

KiChjang Aug 15, 2017

Member

I'm still not convinced this is an improvement over MutNullableJS<T>. In fact as it stands currently, it is an exact copy of it. Let me reiterate the goals of the issue:

Consider the Elements method in HTMLFormElement. It tries to see if the elements field is a Some; if so, it would return that inner value, and if not, it does a whole chunk of initialization for it, and finally setting that inner value into Some.

What I personally would like to see is to have that method body be reduced into simply self.elements.get(), and perhaps in the new method of the OnceCellJS that houses the list of elements, accept a closure/function that gets called whenever the get method encounters a None for its inner value.

This comment has been minimized.

@nox

nox Aug 15, 2017

Member

This should return &T. get should either not exist or return Option<&T>.

This comment has been minimized.

@sendilkumarn

sendilkumarn Aug 15, 2017

Author Contributor

@KiChjang thanks

@nox yeah it should return &T. We can remove the get too use or_init straightaway

@nox nox added S-needs-squash and removed S-fails-tidy labels Aug 16, 2017
@nox
Copy link
Member

nox commented Aug 16, 2017

My comments should be addressed, and then the commits should be squashed together.

Also, we can't land this as long as you don't also migrate at least one MutNullableJS to that new type, finally making use of it.

#[allow(unrooted_must_root)]
unsafe impl<T: DomObject> JSTraceable for OnceCellJS<T> {
unsafe fn trace(&self, trc: *mut JSTracer) {
match self.ptr.as_ref() {

This comment has been minimized.

@nox

nox Aug 16, 2017

Member
if let Some(ptr) = self.ptr.as_ref() {
    ptr.trace(trc);
}
/// Retrieve a copy of the current inner value. If it is `None`, it is
/// initialized with the result of `cb` first.
#[allow(unrooted_must_root)]
pub unsafe fn or_init<F>(&self, cb: F) -> &T

This comment has been minimized.

@nox

nox Aug 16, 2017

Member

This is still uncorrect.

  • It should not be unsafe.
  • F should be FnOnce() -> Root<T>.
  • The method should only call init_once with a wrapper of cb converting the Root<T> to JS<T> and dereferencing the returned JS<T> to &T.

This comment has been minimized.

@KiChjang

KiChjang Aug 16, 2017

Member

I'm still confused as to why this function exists. How is this any different than MutNullableJS?

This comment has been minimized.

@nox

nox Aug 16, 2017

Member

You cannot retrieve a &T from a MutNullableJS<T>. IMO the method should be named init_once just like in Mitochondria.


impl<T: DomObject> OnceCellJS<T> {
/// Create a new `OnceCellJS` with value
pub fn new(initial: Option<&T>) -> OnceCellJS<T> {

This comment has been minimized.

@nox

nox Aug 16, 2017

Member

I'm pretty sure there is no need for an initial value at all here.

This comment has been minimized.

@sendilkumarn

sendilkumarn Aug 16, 2017

Author Contributor

which makes this function similar to Default::default() Instead, we can change the signature to new_with_value similar to mitochondria

This comment has been minimized.

@KiChjang

KiChjang Aug 16, 2017

Member

I don't really seem to see that use case at all... Why would you need to provide an initial value when the purpose is to either get it from the OnceCell or run a closure to produce a value?


impl<T: DomObject> OnceCellJS<T> {
/// Create a new `OnceCellJS` with value
pub fn new() -> OnceCellJS<T> {

This comment has been minimized.

@nox

nox Aug 21, 2017

Member

Make that the impl of Default for OnceCellJS<T> and this is good to go!

Thanks for keeping up with my comments!

This comment has been minimized.

@sendilkumarn

sendilkumarn Aug 22, 2017

Author Contributor

You mean change this method into default or implement default separately.

This comment has been minimized.

@KiChjang

KiChjang Aug 22, 2017

Member

Implement the Default trait on OnceCellJS, and move the method body below to be inside of fn default.

@nox
Copy link
Member

nox commented Sep 7, 2017

Am stupid, the PR should be rebased to land. ><

@nox nox added S-needs-rebase and removed S-awaiting-merge labels Sep 7, 2017
@bors-servo
Copy link
Contributor

bors-servo commented Sep 8, 2017

🔒 Merge conflict

@sendilkumarn sendilkumarn force-pushed the sendilkumarn:mito branch from fe7a5fb to 35bbe5e Sep 8, 2017
@sendilkumarn sendilkumarn force-pushed the sendilkumarn:mito branch from 35bbe5e to b16a209 Sep 8, 2017
let filter = box ElementsFilter { form: Root::from_ref(self) };
let window = window_from_node(self);
let elements = HTMLFormControlsCollection::new(&window, self.upcast(), filter);
elements

This comment has been minimized.

@KiChjang

KiChjang Sep 8, 2017

Member

Can the elements variable be removed here?

adding oncecell for JS references

removing option<JS<T>> to <JS<T>>

changing return types

removing get method and refactoring the function

changing getElements method

lint fixes

moving to default

simplifying return

ordering

Removing elements

linting
@sendilkumarn sendilkumarn force-pushed the sendilkumarn:mito branch from b16a209 to 8b33e0f Sep 9, 2017
@sendilkumarn
Copy link
Contributor Author

sendilkumarn commented Sep 16, 2017

@nox is it pending for any other thing?

@KiChjang
Copy link
Member

KiChjang commented Sep 16, 2017

@bors-servo r=nox

@bors-servo
Copy link
Contributor

bors-servo commented Sep 16, 2017

📌 Commit 8b33e0f has been approved by nox

@bors-servo
Copy link
Contributor

bors-servo commented Sep 16, 2017

🌲 The tree is currently closed for pull requests below priority 9000, this pull request will be tested once the tree is reopened

@bors-servo
Copy link
Contributor

bors-servo commented Sep 17, 2017

Testing commit 8b33e0f with merge b42a2b2918e79820c4b228bfa88cd8cf37eedfe4...

@bors-servo
Copy link
Contributor

bors-servo commented Sep 17, 2017

💔 Test failed - linux-rel-wpt

@bors-servo
Copy link
Contributor

bors-servo commented Sep 21, 2017

Testing commit 8b33e0f with merge 5c797d1...

bors-servo added a commit that referenced this pull request Sep 21, 2017
Using OnceCell<T> from Mitochondria

<!-- Please describe your changes on the following line: -->
---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #13402  (github issue number if applicable).

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18059)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Sep 21, 2017

@bors-servo bors-servo merged commit 8b33e0f into servo:master Sep 21, 2017
2 of 3 checks passed
2 of 3 checks passed
continuous-integration/travis-ci/pr The Travis CI build failed
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
homu Test successful
Details
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.

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