Peformance
Document.UpdatePageLayout called to often, even if document not modified
Issue
Aspose.Words.Document.UpdateFields (1 call -> 233 calls to update page layout)
Aspose.Words.Layout.LayoutCollector.GetStartPageIndex (1526 calls -> 279 calls to update page layout)
calls Aspose.Words.Document.UpdatePageLayout too often and 98% spent for update page layout even if nothing modified
Code
There’s simple enumerator which goes thought Aspose.Words.Document child nodes (deep) and uses LayoutCollector to get start page index
namespace Fiddle
{
using System.Collections.Generic;
using Aspose.Words;
using Aspose.Words.Layout;
using MoreLinq;
public class DocumentAnalyser
{
private readonly Aspose.Words.Document _document;
private readonly IDictionary<int, LinkedList<Node>> _nodesByPages = new Dictionary<int, LinkedList<Node>>();
public DocumentAnalyser(Aspose.Words.Document document)
{
_document = document;
}
public void Analyse()
{
_document.UpdateTableLayout();
_document.UpdatePageLayout();
_document.UpdateFields();
CreateNodesByPageMap();
}
private void CreateNodesByPageMap()
{
var collector = new LayoutCollector(_document);
// in shapshots: there 1526 nodes
_document.GetChildNodes(NodeType.Any, true)
.ForEach(
(node) =>
{
var page = collector.GetStartPageIndex(node);
LinkedList<Node> list = null;
if (_nodesByPages.ContainsKey(page))
{
list = _nodesByPages[page];
}
else
{
list = new LinkedList<Node>();
_nodesByPages[page] = list;
}
list.AddLast(node);
});
}
}
}
Profiler snapshots:
snapshot-1.png (101.1 KB)
snapshot-2.png (94.2 KB)