[webkit-changes] [WebKit/WebKit] 8858f1: Avoid storing effective language on ElementRareDat...

Cameron McCormack noreply at github.com
Mon Feb 6 19:16:42 PST 2023


  Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: 8858f1de792a602c09ff087c13c342014f27fa62
      https://github.com/WebKit/WebKit/commit/8858f1de792a602c09ff087c13c342014f27fa62
  Author: Cameron McCormack <heycam at apple.com>
  Date:   2023-02-06 (Mon, 06 Feb 2023)

  Changed paths:
    A LayoutTests/fast/css/lang-matching-document-invalidation-expected.txt
    A LayoutTests/fast/css/lang-matching-document-invalidation.html
    M Source/WebCore/dom/Document.cpp
    M Source/WebCore/dom/Document.h
    M Source/WebCore/dom/Element.cpp
    M Source/WebCore/dom/Element.h
    M Source/WebCore/dom/ElementData.cpp
    M Source/WebCore/dom/ElementData.h
    M Source/WebCore/dom/EventTarget.h

  Log Message:
  -----------
  Avoid storing effective language on ElementRareData when it matches the effective document element language
https://bugs.webkit.org/show_bug.cgi?id=251657
<rdar://problem/104987630>

Reviewed by Ryosuke Niwa.

When an element's lang attribute is set, we set the effective language on
the ElementRareData of all elements in the subtree. We have an existing
optimization that avoids this if we're setting it on the document
element. There are some pages, like Wikipedia, that set an explicit
lang on some other element in the body of the page, but which matches
the document element's lang.

We can avoid the memory overhead of allocating ElementRareData to store
the effective language in this case by using a flag on the element. On
large Wikipedia pages this can save several MB.

If the document element language changes later, we must update the
effective lang state on subtrees that are using this flag. A WeakHashSet
is added to Document to track elements that have an explicit lang
attribute that matches the effective document element language.

Three flags on EventTarget are introduced:

- HasLangAttr and HasXMLLangAttr: records that the element has the
  corresponding lang attribute. This allows us to avoid searching
  for an attribute when updating the effective lang state.

- EffectiveLangKnownToMatchDocument: records that the element has an
  effective lang that matches the effective document element language,
  whether it's due to an explicit lang attribute or inherited from an
  ancestor. This flag is used in place of
  ElementRareData::m_effectiveLang.

The EffectiveLangKnownToMatchDocument flag is used in place of the "null
effective language means we've inherited the effective document
language" state so that disconnected subtrees can also make use of this
optimization. Otherwise, for a case like this:

  let e = document.createElement("div");
  e.lang = "en";  // matching document
  e.append(document.createElement("div"));

the child element would not know whether to return nullptr or "en" from
Element::effectiveLang() without looking up the tree to see if there is
an ancestor with a langauge attribute.

The EffectiveLangKnownToMatchDocument flag is not
EffectiveLangMatchesDocument, since we don't set it if the document
element language changes and an existing element starts matching it.
Rather than track all elements with lang attributes to handle such
cases, we leave the effective lang stored on the ElementRareData.

* LayoutTests/fast/css/lang-matching-document-invalidation-expected.txt: Added.
* LayoutTests/fast/css/lang-matching-document-invalidation.html: Added.
* Source/WebCore/dom/Document.cpp:
(WebCore::Document::setDocumentElementLanguage):
(WebCore::Document::addElementWithLangAttrMatchingDocument):
(WebCore::Document::removeElementWithLangAttrMatchingDocument):
* Source/WebCore/dom/Document.h:
* Source/WebCore/dom/Element.cpp:
(WebCore::Element::~Element):
(WebCore::Element::attributeChanged):
(WebCore::Element::setEffectiveLangInSubtree):
(WebCore::Element::didMoveToNewDocument):
(WebCore::Element::setEffectiveLangFromParent):
(WebCore::Element::setEffectiveLang):
(WebCore::Element::insertedIntoAncestor):
(WebCore::Element::hasEffectiveLangState const):
(WebCore::Element::removedFromAncestor):
(WebCore::Element::effectiveLang const):
(WebCore::Element::langFromAttribute const):
(WebCore::Element::langAttrMatchesDocument const):
(WebCore::Element::setLangAttrMatchesDocument):
(WebCore::Element::effectiveLangMatchesDocument const):
(WebCore::Element::setEffectiveLangMatchesDocument):
* Source/WebCore/dom/Element.h:
* Source/WebCore/dom/EventTarget.h:

Canonical link: https://commits.webkit.org/259931@main




More information about the webkit-changes mailing list