Closed Bug 1517254 Opened 7 years ago Closed 11 months ago

Slow load and long delay before search field can be interacted with on https://scouts.org.uk

Categories

(Core :: CSS Parsing and Computation, defect, P2)

defect

Tracking

()

RESOLVED WORKSFORME
Performance Impact medium

People

(Reporter: davehunt, Unassigned)

Details

(Keywords: perf:responsiveness)

I noticed when using the UK Scouts website that performance is particularly bad in Firefox compared to Chrome - perhaps 2-3 times slower. To replicate, load https://scouts.org.uk/ and attempt to interact with the search bar in the top right. In most cases it takes over 10 seconds before I can focus the input field, and when I can focus it earlier, entering text suffers a long delay. In Chrome, I am able to enter text into this field after around 3 seconds. I have created a profile (https://perfht.ml/2GR68cK) and it appears that the site is spending a lot of time in a JavaScript function provided by fontawesome v5.0.20
Whiteboard: [qf]

After load typing in the search bar also janks
https://perfht.ml/2AFyNvM

Looks like time is being spent in Stylo.

Component: Performance → CSS Parsing and Computation
Whiteboard: [qf] → [qf:p2:responsiveness]

From the profile, it's probably the searchPseudoElements function in https://raw.githubusercontent.com/FortAwesome/Font-Awesome/master/js/all.js.

Also, it looks like it's doing it periodically, which is not cool.

Indeed, they have:

    <script>  FontAwesomeConfig = { searchPseudoElements: true }; </script>

On the source.

The function is doing very dumb stuff, like querying the style of all pseudo-elements in the page. For reference:

  function searchPseudoElements(root) {
    if (!IS_DOM) return;
    var end = perf.begin('searchPseudoElements');
    disableObservation(function () {
      toArray(root.querySelectorAll('*')).filter(function (n) {
        return n.parentNode !== document.head && !~TAGNAMES_TO_SKIP_FOR_PSEUDOELEMENTS.indexOf(n.tagName.toUpperCase()) && !n.getAttribute(DATA_FA_PSEUDO_ELEMENT) && (!n.parentNode || n.parentNode.tagName !== 'svg');
      }).forEach(function (node) {
        [':before', ':after'].forEach(function (pos) {
          var children = toArray(node.children);
          var alreadyProcessedPseudoElement = children.filter(function (c) {
            return c.getAttribute(DATA_FA_PSEUDO_ELEMENT) === pos;
          })[0];
          var styles = WINDOW.getComputedStyle(node, pos);
          var fontFamily = styles.getPropertyValue('font-family').match(FONT_FAMILY_PATTERN);
          var fontWeight = styles.getPropertyValue('font-weight');

          if (alreadyProcessedPseudoElement && !fontFamily) {
            // If we've already processed it but the current computed style does not result in a font-family,
            // that probably means that a class name that was previously present to make the icon has been
            // removed. So we now should delete the icon.
            node.removeChild(alreadyProcessedPseudoElement);
          } else if (fontFamily) {
            var content = styles.getPropertyValue('content');
            var prefix = ~['Light', 'Regular', 'Solid', 'Brands'].indexOf(fontFamily[1]) ? STYLE_TO_PREFIX[fontFamily[1]] : FONT_WEIGHT_TO_PREFIX[fontWeight];
            var iconName = byUnicode(prefix, toHex(content.length === 3 ? content.substr(1, 1) : content)); // Only convert the pseudo element in this :before/:after position into an icon if we haven't
            // already done so with the same prefix and iconName

            if (!alreadyProcessedPseudoElement || alreadyProcessedPseudoElement.getAttribute(DATA_PREFIX) !== prefix || alreadyProcessedPseudoElement.getAttribute(DATA_ICON) !== iconName) {
              if (alreadyProcessedPseudoElement) {
                // Delete the old one, since we're replacing it with a new one
                node.removeChild(alreadyProcessedPseudoElement);
              }

              var extra = blankMeta.extra;
              extra.attributes[DATA_FA_PSEUDO_ELEMENT] = pos;
              var abstract = makeInlineSvgAbstract(_objectSpread({}, blankMeta, {
                icons: {
                  main: findIcon(iconName, prefix),
                  mask: emptyCanonicalIcon()
                },
                prefix: prefix,
                iconName: iconName,
                extra: extra,
                watchable: true
              }));
              var element = DOCUMENT.createElement('svg');

              if (pos === ':before') {
                node.insertBefore(element, node.firstChild);
              } else {
                node.appendChild(element);
              }

              element.outerHTML = abstract.map(function (a) {
                return toHtml(a);
              }).join('\n');
            }
          }
        });
      });
    });
    end();
  }

Furthermore they're observing all the changes in the page, including attributes, so each time an attribute changes they redo this process.

    mo.observe(observeMutationsRoot, { // observeMutationsRoot == document.body
      childList: true,
      attributes: true,
      characterData: true,
      subtree: true
    });

You can see the performance difference if you do the following on the console:

var s = performance.now(); document.querySelectorAll("*").forEach(e => { getComputedStyle(e, "::before").fontFamily; getComputedStyle(e, "::after").fontFamily; }); console.log(performance.now() - s)

In FF that takes 50ms ms, in Blink that takes about 20ms the first time, 12-ish later on.

I know the reason for that, and it's that we don't store useless pseudo-element styles to save memory, so when we get asked for them we need to recompute them.

For example, if I do the following to inhibit that memory optimization:

var s = document.createElement("style"); s.innerText = `*::before, *::after { display: inherit; content: inherit; }`; document.body.appendChild(s);

Now running the snippet above takes half the time it originally took. We also have the same optimization for display: none subtrees, which is harder to inhibit.

I'm not sure that performance difference is enough to explain the massive jank, there may be something else going on... Digging a bit more, putting a similar mutation observer on the root, the mutation records we get is for the DOM in the "partners" swiper, which is changing under a timer to animate it.

So there's about 300 mutation records per second, and we're recomputing those styles over and over in order to avoid storing them because they're useless. That's sad :(.

Inhibiting that memory optimization using the trick I posted above makes the page roughly as usable as browsing with Chromium, but of course with a memory usage penalty.

And the TL;DR is that I don't know what we should do about this. We can remove the memory optimization I guess, but that would be very unfortunate.

If we want to fix this, an alternative would probably be to cache somewhere when it's queried via DOM API?

I'm not sure that'd help. That'd be doing twice the work, which is basically what we're doing now.

Priority: -- → P2
Performance Impact: --- → P2
Whiteboard: [qf:p2:responsiveness]
Severity: normal → S3

Tested recently on Win and Mac, current version - works for me. No issues seen in a profile, page loads quickly. please re-open this issue if it continues.

Status: NEW → RESOLVED
Closed: 11 months ago
Resolution: --- → WORKSFORME
You need to log in before you can comment on or make changes to this bug.