Skip to content

Implement discarding Document objects to reclaim space.#14312

Merged
bors-servo merged 5 commits intoservo:masterfrom
asajeffrey:script-discard-documents
Jan 4, 2017
Merged

Implement discarding Document objects to reclaim space.#14312
bors-servo merged 5 commits intoservo:masterfrom
asajeffrey:script-discard-documents

Conversation

@asajeffrey
Copy link
Copy Markdown
Contributor

@asajeffrey asajeffrey commented Nov 21, 2016

This PR implements document discarding. Active documents are kept alive strongly, but inactive documents are only kept alive weakly. When a document is GCd, it is marked as discarded, and if it is every reactivated, a reload of the URL is triggered.

Note that this PR is pretty aggressive about discarding, and can any inactive document (other than those being kept alive by other same-origin pipelines). We might want to damp it down a bit.

Also note that this interacts with browser.html in that the reloading triggered by reactivating a document triggers mozbrowser events.

To test this, I added a -Zdiscard-inactive-documents debug flag, which discards all inactive documents, even ones which are reachable through other same-origin pipelines.


  • ./mach build -d does not report any errors
  • ./mach test-tidy does not report any errors
  • These changes fix Discard documents to reclaim resources #14262.
  • These changes do not require tests because we should be able to use the existing tests with -Zdiscard-inactive-documents.

This change is Reviewable

@asajeffrey asajeffrey added A-content/script Related to the script thread I-memory-leak A memory leak has been observed. labels Nov 21, 2016
@highfive
Copy link
Copy Markdown

Heads up! This PR modifies the following files:

  • @fitzgen: components/script/script_thread.rs, components/script/dom/document.rs
  • @KiChjang: components/script/script_thread.rs, components/script/dom/document.rs

@highfive
Copy link
Copy Markdown

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!

@highfive highfive added the S-awaiting-review There is new code that needs to be reviewed. label Nov 21, 2016
@asajeffrey
Copy link
Copy Markdown
Contributor Author

cc @Ms2ger @ConnorGBrewster @metajack

@metajack
Copy link
Copy Markdown
Collaborator

Maybe the name of that flag needs unsafe in it? It seems a little confusingly worded as is.

@bors-servo
Copy link
Copy Markdown
Contributor

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

@highfive highfive added the S-needs-rebase There are merge conflict errors. label Nov 22, 2016
@asajeffrey
Copy link
Copy Markdown
Contributor Author

@metajack: yes, that option name isn't my best-ever writing. -Zaggressively-discard-documents? -Zdiscard-on-navigation? -Zunsafe-navigation-discard?

@asajeffrey asajeffrey force-pushed the script-discard-documents branch from e398d45 to 376f059 Compare November 22, 2016 14:29
@asajeffrey asajeffrey removed the S-needs-rebase There are merge conflict errors. label Nov 22, 2016
@bors-servo
Copy link
Copy Markdown
Contributor

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

@highfive highfive added the S-needs-rebase There are merge conflict errors. label Nov 23, 2016
@asajeffrey asajeffrey force-pushed the script-discard-documents branch from 376f059 to 39f3cea Compare November 23, 2016 14:59
@asajeffrey asajeffrey removed the S-needs-rebase There are merge conflict errors. label Nov 23, 2016
@asajeffrey
Copy link
Copy Markdown
Contributor Author

Might be worth disabling this by default, and enabling it with a flag?

@metajack
Copy link
Copy Markdown
Collaborator

@asajeffrey Why disable by default?

@asajeffrey
Copy link
Copy Markdown
Contributor Author

@metajack just being conservative. This change makes servo a lot more like FF, in that back/fwd will almost always result in a reload. This is not necessarily bad, but it is different.

@metajack
Copy link
Copy Markdown
Collaborator

Leaving it off by default will make it consume infinite memory, no?

I can see a case for some middle ground setting being desireable, but I assume that is much more complex to guarantee since somehow we will have to keep the appropriate pipelines alive artificially.

@asajeffrey
Copy link
Copy Markdown
Contributor Author

@metajack yes, there are more space leaks without this PR than with it. It wouldn't be too hard to implement whatever heuristic we want, we just add a message from the constellation to the script thread saying when a pipeline is discardable. I just piggybacked freeze/thaw because it was already there.

/// The state that a document managed by this script thread could be in.
#[must_root]
enum DocumentState {
Kept(bool, JS<Document>),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you document what this bool means? I guess it's thawed, but would be ok to have it written here, or alternatively add a binary enum or a Kept { thawed: bool, document: JS<Document> } instead of a plain bool.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

fn trace(&self, trc: *mut JSTracer) {
// Note: we only trace thawed documents.
if let DocumentState::Kept(true, ref doc) = *self {
doc.trace(trc);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed? Just derive the trait on DocumentState.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The derived version would trace documents even if they were Kept(false, doc). The idea is to implement poor-dev's weak references, that don't keep the documents alive.

@KiChjang
Copy link
Copy Markdown
Contributor

KiChjang commented Dec 1, 2016

r? @jdm

@highfive highfive assigned jdm and unassigned KiChjang Dec 1, 2016
@bors-servo
Copy link
Copy Markdown
Contributor

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

@asajeffrey
Copy link
Copy Markdown
Contributor Author

An IRC conversation with @jdm, @Ms2ger and @cbrewster: http://logs.glob.uno/?c=mozilla%23servo&s=4+Jan+2017&e=4+Jan+2017#c586779

TL;DR: this crash is probably caused by us not doing the window proxy to window mapping quite right, since we are generating a new BrowsingContext object for every Window.

@asajeffrey
Copy link
Copy Markdown
Contributor Author

@bors-servo try

@bors-servo
Copy link
Copy Markdown
Contributor

⌛ Trying commit c14c431 with merge 9673fcb...

bors-servo pushed a commit that referenced this pull request Jan 4, 2017
Implement discarding Document objects to reclaim space.

<!-- Please describe your changes on the following line: -->

This PR implements document discarding. Active documents are kept alive strongly, but inactive documents are only kept alive weakly. When a document is GCd, it is marked as discarded, and if it is every reactivated, a reload of the URL is triggered.

Note that this PR is pretty aggressive about discarding, and can any inactive document (other than those being kept alive by other same-origin pipelines). We might want to damp it down a bit.

Also note that this interacts with browser.html in that the reloading triggered by reactivating a document triggers mozbrowser events.

To test this, I added a `-Zdiscard-inactive-documents` debug flag, which discards all inactive documents, even ones which are reachable through other same-origin pipelines.

---
<!-- 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 #14262.
- [X] These changes do not require tests because we should be able to use the existing tests with `-Zdiscard-inactive-documents`.

<!-- 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/14312)
<!-- Reviewable:end -->
@bors-servo
Copy link
Copy Markdown
Contributor

☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css, mac-rel-wpt1, mac-rel-wpt2, windows-gnu-dev, windows-msvc-dev

@jdm
Copy link
Copy Markdown
Member

jdm commented Jan 4, 2017

This is ready to merge from my point of view.

@asajeffrey
Copy link
Copy Markdown
Contributor Author

@bors-servo r=cbrewster cross fingers

@bors-servo
Copy link
Copy Markdown
Contributor

📌 Commit c14c431 has been approved by cbrewster

@highfive highfive assigned cbrewster and unassigned jdm Jan 4, 2017
@highfive highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-awaiting-review There is new code that needs to be reviewed. labels Jan 4, 2017
@cbrewster
Copy link
Copy Markdown
Contributor

@bors-servo retry

@bors-servo
Copy link
Copy Markdown
Contributor

⌛ Testing commit c14c431 with merge 16b0da5...

bors-servo pushed a commit that referenced this pull request Jan 4, 2017
Implement discarding Document objects to reclaim space.

<!-- Please describe your changes on the following line: -->

This PR implements document discarding. Active documents are kept alive strongly, but inactive documents are only kept alive weakly. When a document is GCd, it is marked as discarded, and if it is every reactivated, a reload of the URL is triggered.

Note that this PR is pretty aggressive about discarding, and can any inactive document (other than those being kept alive by other same-origin pipelines). We might want to damp it down a bit.

Also note that this interacts with browser.html in that the reloading triggered by reactivating a document triggers mozbrowser events.

To test this, I added a `-Zdiscard-inactive-documents` debug flag, which discards all inactive documents, even ones which are reachable through other same-origin pipelines.

---
<!-- 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 #14262.
- [X] These changes do not require tests because we should be able to use the existing tests with `-Zdiscard-inactive-documents`.

<!-- 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/14312)
<!-- Reviewable:end -->
@bors-servo
Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-content/script Related to the script thread I-memory-leak A memory leak has been observed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Discard documents to reclaim resources

10 participants