From d78358d506c2e700a1177f93bdb915154eefae5c Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 17 Oct 2018 20:33:21 +0000 Subject: 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 Commit-Queue: Lei Zhang --- core/fxcrt/xml/cfx_xmlnode.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'core') 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_) -- cgit v1.2.3