diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2019-05-28 11:06:23 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2019-05-28 16:41:26 +0200 |
commit | 7ac940d278f9bb3fbb1988a74dfa4909960bd998 (patch) | |
tree | 68228a3de97842d00d67217b1e7736a6db94f355 /sw/inc/ndhints.hxx | |
parent | 7376a47680b65cbdfd747a736f288e06f51f7f2d (diff) |
tdf#125372 writer, file with lots of hints very slow to open, part3
There were O(n^2) issues all over the places where we walk these lists.
This takes the opening time from 10m+ to 4m.
(*) Invalidate the sorting on a much less aggressive basis, by having
the SwTextAttr objects tell the SwpHints object when start and end pos
changes
(*) Add a bool field to indicate when the maps become unsorted, so we
can resort them only when we actually need sorted access
(*) Add an API for walking the list of SwTextAttr* without triggering
sorting, which is particularly important when the RedlineManager starts
moving things around.
(*) Add various asserts to catch code that tries to iterate over these
sorted lists but triggers resorting during the loop.
I also moved some of the logic around so that instead of
update hint
delete hint
insert hint
it now goes
delete hint
update hint
insert hint
which means that the needToSort flag does not get set because the
hint is disconnected while it is being updated.
Change-Id: Ie153ddafc9ef9e3470d588db2be2457c676232a8
Reviewed-on: https://gerrit.libreoffice.org/73090
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Tested-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'sw/inc/ndhints.hxx')
-rw-r--r-- | sw/inc/ndhints.hxx | 65 |
1 files changed, 35 insertions, 30 deletions
diff --git a/sw/inc/ndhints.hxx b/sw/inc/ndhints.hxx index c3112844db8e..299218a55399 100644 --- a/sw/inc/ndhints.hxx +++ b/sw/inc/ndhints.hxx @@ -54,23 +54,8 @@ SwTextAttr* MakeRedlineTextAttr( SwDoc & rDoc, SfxPoolItem const & rAttr ); - -/// SwTextAttr's, sorted by start -struct CompareSwpHtStart -{ - bool operator()(SwTextAttr const * const lhs, SwTextAttr const * const rhs) const; -}; -class SwpHtStart : public o3tl::sorted_vector<SwTextAttr*, CompareSwpHtStart, - o3tl::find_partialorder_ptrequals> {}; - -/// SwTextAttr's, sorted by end -struct CompareSwpHtEnd -{ - bool operator()(SwTextAttr const * const lhs, SwTextAttr const * const rhs) const; -}; -class SwpHtEnd : public o3tl::sorted_vector<SwTextAttr*, CompareSwpHtEnd, - o3tl::find_partialorder_ptrequals> {}; - +bool CompareSwpHtEnd( const SwTextAttr* lhs, const SwTextAttr* rhs ); +bool CompareSwpHtWhichStart( const SwTextAttr* lhs, const SwTextAttr* rhs ); /// An SwTextAttr container, stores all directly formatted text portions for a text node. class SwpHints @@ -82,8 +67,8 @@ private: // failure, so just allow SAL_MAX_SIZE-1 hints static const size_t MAX_HINTS = SAL_MAX_SIZE-1; - SwpHtStart m_HintsByStart; - SwpHtEnd m_HintsByEnd; + std::vector<SwTextAttr*> m_HintsByStart; + std::vector<SwTextAttr*> m_HintsByEnd; SwRegHistory* m_pHistory; ///< for Undo @@ -96,6 +81,9 @@ private: mutable bool m_bHiddenByParaField : 1; bool m_bFootnote : 1; ///< footnotes bool m_bDDEFields : 1; ///< the TextNode has DDE fields + // Sort on demand to avoid O(n^2) behaviour + mutable bool m_bStartMapNeedsSorting : 1; + mutable bool m_bEndMapNeedsSorting : 1; /// records a new attribute in m_pHistory. void NoteInHistory( SwTextAttr *pAttr, const bool bNew = false ); @@ -129,18 +117,11 @@ private: bool MergePortions( SwTextNode& rNode ); void Insert( const SwTextAttr *pHt ); - void Resort(); + SW_DLLPUBLIC void Resort() const; + SW_DLLPUBLIC void ResortStartMap() const; + SW_DLLPUBLIC void ResortEndMap() const; - size_t GetIndexOf( const SwTextAttr *pHt ) const - { - SwpHtStart::const_iterator const it = - m_HintsByStart.find(const_cast<SwTextAttr*>(pHt)); - if ( it == m_HintsByStart.end() ) - { - return SAL_MAX_SIZE; - } - return it - m_HintsByStart.begin(); - } + size_t GetIndexOf( const SwTextAttr *pHt ) const; #ifdef DBG_UTIL bool Check(bool) const; @@ -153,12 +134,32 @@ public: bool Contains( const SwTextAttr *pHt ) const; SwTextAttr * Get( size_t nPos ) const { + assert( !(nPos != 0 && m_bStartMapNeedsSorting) && "going to trigger a resort in the middle of an iteration, that's bad" ); + if (m_bStartMapNeedsSorting) + ResortStartMap(); + return m_HintsByStart[nPos]; + } + // Get without triggering resorting - useful if we are modifying start/end pos while iterating + SwTextAttr * GetWithoutResorting( size_t nPos ) const + { return m_HintsByStart[nPos]; } + int GetLastPosSortedByEnd(sal_Int32 nEndPos) const; SwTextAttr * GetSortedByEnd( size_t nPos ) const { + assert( !(nPos != 0 && m_bEndMapNeedsSorting) && "going to trigger a resort in the middle of an iteration, that's bad" ); + if (m_bEndMapNeedsSorting) + ResortEndMap(); return m_HintsByEnd[nPos]; } + /// Trigger the sorting if necessary + void SortIfNeedBe() const + { + if (m_bStartMapNeedsSorting) + ResortStartMap(); + if (m_bEndMapNeedsSorting) + ResortEndMap(); + } SwTextAttr * Cut( const size_t nPosInStart ) { SwTextAttr *pHt = m_HintsByStart[nPosInStart]; @@ -184,6 +185,10 @@ public: // calc current value of m_bHiddenByParaField, returns true iff changed bool CalcHiddenParaField() const; // changes mutable state + + // Marks the hint-maps as needing sorting because the position of something has changed + void StartPosChanged() const { m_bStartMapNeedsSorting = true; m_bEndMapNeedsSorting = true; } + void EndPosChanged() const { m_bStartMapNeedsSorting = true; m_bEndMapNeedsSorting = true; } }; #endif |