Skip to content

Using viewport percentage lengths (e.g. vh, vw) causes unnecessary style recalc on resize #10104

Closed

Description

@mbrubeck

As mentioned in #9876 (comment), Servo currently does more work than necessary to recalculate vh/vw/vmin/vmax units when the window size changes. Currently, during parsing, if a stylesheet contains any of these viewport percentage units, we add a dirty_on_viewport_size_change to the stylesheet. When the viewport size changes, if any stylesheet has this flag set, we dirty all nodes and recalculate their styles.

Instead, we could dirty only nodes that match rules containing viewport percentages. During selector matching, Stylist::push_applicable_declarations could set a flag on any node whose applicable declarations contain one of these units. This would require adding a method to DeclaredValue and the various SpecifiedValue types that determines whether the value includes a viewport percentage length.

Activity

  1. added
    A-content/css
    I-perf-slow
    E-more-complex
    on Mar 21, 2016
  2. SimonSapin commented on Mar 22, 2016

    @SimonSapin
    Member

    Instead, we could dirty only nodes that match rules containing viewport percentages.

    The descendants of these nodes may also be affected through CSS inheritance, and likely need to be dirtied as well.

  3. added a commit that references this issue on Mar 22, 2016
  4. roysohan commented on Mar 28, 2016

    @roysohan

    I am a beginner but i want to contribute, and the folks said that e-easy tags are the best place to start. So how do I contribute to this issue.

  5. jrasanen commented on Mar 29, 2016

    @jrasanen
    Contributor

    Hi @roysohan. Checkout wiki and especially Quickstart to Hacking Servo. For any questions regarding Servo, #servo IRC channel on irc.mozilla.org is a fine place. For Rust related questions there is #rust and #rust-beginner channels.

  6. mbrubeck commented on Mar 29, 2016

    @mbrubeck
    ContributorAuthor

    Note: This issue is tagged "less easy", which means that it might not be suitable for a brand new contributor, but could be good as a next step after one or more easy issues. This is mainly because it could require making a large number of small changes; it will probably be tedious but hopefully not too complicated. If you are patient and have enough time, you could probably start out with this issue.

    One way to solve this issue would be to add a trait for asking whether a given CSS value has any viewport percentage lengths:

    trait HasViewportPercentage {
        fn has_viewport_percentage(&self) -> bool;
    }

    Implement this trait for the Length type, returning true if it is a Length::ViewportPercentage and false otherwise. Then implement it for any other types in style::values::specified that can contain Length values; it should return true if any field of the type contains a Length::ViewportPercentage. For example:

    impl HasViewportPercentage for LengthOrNone {
        fn has_viewport_percentage(&self) -> bool {
            match *self {
                Length(length) => length.has_viewport_percentage(),
                None => false
            }
        }
    }

    Next, look at each SpecifiedValue type. (Many of these are aliases to common types, so there are fewer than it appears.) If the type has any fields that might contain Length values, then it should impl the trait and return true if any of those values is a Length::ViewportPercentage:

    impl HasViewportPercentage for line_height::SpecifiedValue {
        fn has_viewport_percentage(&self) -> bool {
            match *self {
                line_height::SpecifiedValue::LengthOrPercentage(ref val) => val.has_viewport_percentage(),
                line_height::SpecifiedValue::Number(_) => false,
                line_height::SpecifiedValue::Normal => false,
            }
        }
    }

    Once all of those impls are written, we can impl HasViewportPercentage for PropertyDeclaration. The implementation will look something like this:

    impl HasViewportPercentage for PropertyDeclaration {
        fn has_viewport_percentage(&self) -> bool {
            match *self {
                // For the properties whose SpecifiedValues can contain lengths,
                // check the SpecifiedValue:
                MarginTop(DeclaredValue::Value(val)) => val.has_viewport_percentage()),
                MarginRight(DeclaredValue::Value(val)) => val.has_viewport_percentage()),
                MarginBottom(DeclaredValue::Value(val)) => val.has_viewport_percentage()),
                MarginLeft(DeclaredValue::Value(val)) => val.has_viewport_percentage()),
                PaddingTop(DeclaredValue::Value(val)) => val.has_viewport_percentage()),
                /// ...etc.
    
                // For other properties, return false:
                _ => false,
            }
        }
    }

    Fortunately we don't need to write or maintain this giant match expression by hand. Instead, it can be generated by the Mako preprocessor from source code in properties.mako.rs. This will require adding a new boolean field to the each "longhand" property in the Mako template, specifying whether it implements HasViewportPercentage.

    Whew. I hope that's enough for someone to get started. Once you've done all that, you will still need to add code in selector_matching.rs to flag elements that match rules containing viewport percentages. I can answer questions about that part when we get there.

  7. KiChjang commented on Mar 30, 2016

    @KiChjang
    Contributor

    @roysohan Could you confirm that you are currently working on this issue?

  8. roysohan commented on Mar 31, 2016

    @roysohan

    I am quite busy, but will try starting to contribute, any hints on which file to start.

    Also I am low on bandwidth, so can you suggest how to contribute in low bandwidth condition

    I am a college student, and I have exams, I have started learning rust as a hobby, and someone suggested on rust irc channels that I should be contributing to servo for better understanding.
    So the problem is I cannot commit myself. I am extremely sorry for that.

    But I will contribute little by little. And this is my first time. So I need your guide and support on where to start, how to do, and which files to edit. I think slowly under your guidance I may learn something.

    I apologise for not being able to commit myself fully.

    Also I am a total beginner at this. I learnt programming upto high school, know little about java and c,ie some basic programming.

  9. jdm commented on Mar 31, 2016

    @jdm
    Member

    Note - @roysohan is looking into other issues instead, so this one is still available.

  10. Shiroy commented on Apr 3, 2016

    @Shiroy
    Contributor

    I'm working on this issue.

    If I understand well, the HasViewportPercentage trait is used to easily identify which node is currently using a vh/vw unit so we must only dirty these nodes when the viewport size change ?

    And dirtying these nodes is done somewhere in selector_matching.rs ?

  11. added
    C-assigned
    on Apr 3, 2016
  12. mbrubeck commented on Apr 4, 2016

    @mbrubeck
    ContributorAuthor

    If I understand well, the HasViewportPercentage trait is used to easily identify which node is currently using a vh/vw unit so we must only dirty these nodes when the viewport size change ?

    Yes.

    And dirtying these nodes is done somewhere in selector_matching.rs ?

    During selector matching, either push_applicable_declarations or one of the functions that calls it (style::traversal::recalc_style_at might be the easiest place to do this) should set this flag on any node whose applicable declarations contain a viewport percentage or whose parent has the flag set, and should clear the flag otherwise. This will require adding a new DIRTY_ON_VIEWPORT_SIZE_CHANGE flag to the script::dom::node::NodeFlags type, and a new method to set this flag in the style::dom::TNode trait. (The new method will be very similar to the TNode::set_dirty method.)

    Then, finally, in LayoutThread::handle_reflow, instead of marking all nodes as dirty when the viewport size changes, call a new function that only dirties nodes with this new flag.

  13. 16 remaining items

  14. jdm commented on May 3, 2016

    @jdm
    Member

    Ok, just let us know if you start working on this again!

  15. removed
    C-assigned
    on May 3, 2016
  16. shinglyu commented on May 23, 2016

    @shinglyu
    Contributor

    I'm interested in this.

  17. KiChjang commented on May 23, 2016

    @KiChjang
    Contributor

    Have at it!

  18. added
    C-assigned
    on May 23, 2016
  19. shinglyu commented on May 24, 2016

    @shinglyu
    Contributor

    (@shinglyu add to my dashboard)

  20. jdm commented on Jun 21, 2016

    @jdm
    Member

    @shinglyu Is this something you're still working on?

  21. shinglyu commented on Jun 22, 2016

    @shinglyu
    Contributor

    Yes, sorry for the delay

  22. shinglyu commented on Jun 22, 2016

    @shinglyu
    Contributor

    I already have a rough picture of how to do it, but @Shiroy 's patch does not apply on the latest master, so I have to do it manually.

  23. shinglyu commented on Jun 23, 2016

    @shinglyu
    Contributor

    Status: I reused part of @Shiroy 's patch, and followed @SimonSapin 's suggestion to create a marker trait for NoViewportPercentage.

    https://github.com/shinglyu/servo/commits/viewport-percent-recalc

    I'll finish implementing the actual HasViewportPercentage trait for properties that may use viewport length, then I'll actually set them and use them in recalc_style_at

  24. shinglyu commented on Jun 27, 2016

    @shinglyu
    Contributor

    I have a patch now: master...shinglyu:viewport-percent-recalc

    I still need to do some testing and profiling to make sure it does improve the performance.

  25. added 3 commits that reference this issue on Jul 11, 2016
    901a6bd
    d45080e
    d87ea67
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/cssC-assignedE-more-complexI-perf-slow

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions