Tim Starling has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/259653

Change subject: Avoid nextSibling, regex
......................................................................

Avoid nextSibling, regex

* In cleanupFormattingTagFixup, use childNodes iteration instead of
  nextSibling, for 3s of improvement on the current test case.
* In findAutoInsertedTags, use the same trick for unfortunately just a
  few milliseconds improvement.
* In findAutoInsertedTags, replace a dynamic regex with startsWith() for
  a 1.6s improvement with the current test case.

Bug: T119883
Change-Id: I593b1d148788a365efb5a88d8df6df253a9bc8e8
---
M lib/wt2html/pp/cleanupFormattingTagFixup.js
M lib/wt2html/pp/markTreeBuilderFixups.js
2 files changed, 24 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid 
refs/changes/53/259653/1

diff --git a/lib/wt2html/pp/cleanupFormattingTagFixup.js 
b/lib/wt2html/pp/cleanupFormattingTagFixup.js
index 830fdad..f8e49ad 100644
--- a/lib/wt2html/pp/cleanupFormattingTagFixup.js
+++ b/lib/wt2html/pp/cleanupFormattingTagFixup.js
@@ -2,9 +2,11 @@
 
 var DU = require('../../utils/DOMUtils.js').DOMUtils;
 
-function cleanupFormattingTagFixup(node, env) {
-       node = node.firstChild;
-       while (node !== null) {
+function cleanupFormattingTagFixup(parent, env) {
+       var nodes = parent.childNodes;
+       var i;
+       for (i = 0; i < nodes.length; i++) {
+               var node = nodes[i];
                if (DU.isGeneratedFigure(node)) {
                        // Find path of formatting elements.
                        // NOTE: <a> is a formatting elts as well and should be 
explicitly skipped
@@ -53,7 +55,6 @@
                } else if (DU.isElt(node)) {
                        cleanupFormattingTagFixup(node, env);
                }
-               node = node.nextSibling;
        }
 }
 
diff --git a/lib/wt2html/pp/markTreeBuilderFixups.js 
b/lib/wt2html/pp/markTreeBuilderFixups.js
index 72b9889..6452a97 100644
--- a/lib/wt2html/pp/markTreeBuilderFixups.js
+++ b/lib/wt2html/pp/markTreeBuilderFixups.js
@@ -16,13 +16,17 @@
                node);
 }
 
+/**
+ * Add placeholder meta
+ * Return true if the meta was added, false otherwise.
+ */
 function addPlaceholderMeta(env, node, dp, name, opts) {
        // If node is in a position where the placeholder
        // node will get fostered out, dont bother adding one
        // since the browser and other compliant clients will
        // move the placeholder out of the table.
        if (DU.isFosterablePosition(node)) {
-               return;
+               return false;
        }
 
        var src = dp.src;
@@ -50,6 +54,9 @@
 
                // Insert the placeHolder
                node.parentNode.insertBefore(placeHolder, node);
+               return true;
+       } else {
+               return false;
        }
 }
 
@@ -156,16 +163,18 @@
 // and adds autoInsertedEnd/Start flags if it detects the tags to be inserted 
by
 // the HTML tree builder
 function findAutoInsertedTags(env, node) {
-       var c = node.firstChild;
+       var nodes = node.childNodes;
+       var i, c;
        var sibling, expectedName, reg;
 
-       while (c !== null) {
+       for (i = 0; i < nodes.length; i++) {
+               c = nodes[i];
                // Skip over template/extension content
                if (DU.isTplOrExtToplevelNode(node)) {
                        var about = node.getAttribute('about');
-                       c = c.nextSibling;
+                       c = nodes[++i];
                        while (c && node.getAttribute('about') === about) {
-                               c = c.nextSibling;
+                               c = nodes[++i];
                        }
 
                        if (!c) {
@@ -214,11 +223,9 @@
                                        }
 
                                        expectedName = cNodeName + ":" + 
dp.tagId;
-                                       reg = new RegExp("^" + expectedName + 
"(:.*)?$");
-
                                        if (fc &&
                                                DU.isMarkerMeta(fc, 
"mw:StartTag") &&
-                                               
reg.test(fc.getAttribute('data-stag'))
+                                               
fc.getAttribute('data-stag').startsWith(expectedName)
                                        ) {
                                                // Strip start-tag marker metas 
that has its matching node
                                                deleteShadowMeta(fc);
@@ -240,7 +247,9 @@
                                        if (!sibling || 
sibling.nodeName.toLowerCase() !== expectedName) {
                                                // Not found, the tag was 
stripped. Insert an
                                                // mw:Placeholder for 
round-tripping
-                                               addPlaceholderMeta(env, c, dp, 
expectedName, {end: true});
+                                               if (addPlaceholderMeta(env, c, 
dp, expectedName, {end: true})) {
+                                                       i++;
+                                               }
                                        } else if (dp.stx) {
                                                // Transfer stx flag
                                                var siblingDP = 
DU.getDataParsoid(sibling);
@@ -258,15 +267,9 @@
                                                }
                                                siblingDP.stx = dp.stx;
                                        }
-                               } else {
-                                       // Jump over this meta tag, but 
preserve it
-                                       c = c.nextSibling;
-                                       continue;
-                               }
+                               } // else Jump over this meta tag, but preserve 
it
                        }
                }
-
-               c = c.nextSibling;
        }
 }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/259653
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I593b1d148788a365efb5a88d8df6df253a9bc8e8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Tim Starling <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to