summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLei Zhang <thestig@chromium.org>2018-10-17 20:33:21 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-10-17 20:33:21 +0000
commitd78358d506c2e700a1177f93bdb915154eefae5c (patch)
tree6ae1bba8f43369375f632e4f2f5ef75f75522d18
parenta37989fe53097e4a0b6e87eb6ba01def1e13b675 (diff)
downloadpdfium-d78358d506c2e700a1177f93bdb915154eefae5c.tar.xz
Optimize appends in CFX_XMLNode::InsertChildNode().
Skip to the end of the linked list instead of traversing it. BUG=chromium:895234 Change-Id: I56d6bee3cd099a1a7343eb2b067d522ec69c261a Reviewed-on: https://pdfium-review.googlesource.com/c/44172 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
-rw-r--r--core/fxcrt/xml/cfx_xmlnode.cpp14
1 files changed, 10 insertions, 4 deletions
diff --git a/core/fxcrt/xml/cfx_xmlnode.cpp b/core/fxcrt/xml/cfx_xmlnode.cpp
index 59eb3f77ce..4fbc8bc1de 100644
--- a/core/fxcrt/xml/cfx_xmlnode.cpp
+++ b/core/fxcrt/xml/cfx_xmlnode.cpp
@@ -58,12 +58,18 @@ void CFX_XMLNode::InsertChildNode(CFX_XMLNode* pNode, int32_t index) {
return;
}
- int32_t iCount = 0;
- CFX_XMLNode* pFind = first_child_;
+ CFX_XMLNode* pFind;
// Note, negative indexes, and indexes after the end of the list will result
// in appending to the list.
- while (++iCount != index && pFind->next_sibling_)
- pFind = pFind->next_sibling_;
+ if (index < 0) {
+ // Optimize the negative index case.
+ pFind = last_child_;
+ } else {
+ pFind = first_child_;
+ int32_t iCount = 0;
+ while (++iCount != index && pFind->next_sibling_)
+ pFind = pFind->next_sibling_;
+ }
pNode->prev_sibling_ = pFind;
if (pFind->next_sibling_)