include/svl/IndexedStyleSheets.hxx | 10 ++++++---- svl/source/items/IndexedStyleSheets.cxx | 7 ++++--- svl/source/items/style.cxx | 14 +++++++------- 3 files changed, 17 insertions(+), 14 deletions(-)
New commits: commit 8af3358203e71564b69ae4a687989a57a2a8605e Author: Noel Grandin <[email protected]> AuthorDate: Mon Oct 14 12:10:24 2024 +0200 Commit: Noel Grandin <[email protected]> CommitDate: Mon Oct 14 21:51:26 2024 +0200 tdf#100894 reduce time spent iterating styles Small difference, but an easy optimisation Change-Id: Ic5aeb759fb64af378a6cfcd83977d917be1b9a8e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/174888 Tested-by: Jenkins Reviewed-by: Noel Grandin <[email protected]> diff --git a/include/svl/IndexedStyleSheets.hxx b/include/svl/IndexedStyleSheets.hxx index 1934bc10af89..425c508ea6f3 100644 --- a/include/svl/IndexedStyleSheets.hxx +++ b/include/svl/IndexedStyleSheets.hxx @@ -148,10 +148,12 @@ public: void ReindexOnNameChange(const SfxStyleSheetBase& style, const OUString& rOldName, const OUString& rNewName); - /** Warning: counting for n starts at 0, i.e., the 0th style sheet is the first that is found. */ - SfxStyleSheetBase* GetNthStyleSheetThatMatchesPredicate(sal_Int32 n, - StyleSheetPredicate& predicate, - sal_Int32 startAt = 0); + /** Warning: counting for n starts at 0, i.e., the 0th style sheet is the first that is found. + Returns a pointer if a stylesheet is found, and the position of the found stylesheet + */ + std::pair<SfxStyleSheetBase*, sal_Int32> + GetNthStyleSheetThatMatchesPredicate(sal_Int32 n, StyleSheetPredicate& predicate, + sal_Int32 startAt = 0); /** Get the positions of the style sheets which belong to a certain family. */ diff --git a/svl/source/items/IndexedStyleSheets.cxx b/svl/source/items/IndexedStyleSheets.cxx index 5d2ddf8299f9..b15271de7cb7 100644 --- a/svl/source/items/IndexedStyleSheets.cxx +++ b/svl/source/items/IndexedStyleSheets.cxx @@ -150,7 +150,7 @@ IndexedStyleSheets::GetNumberOfStyleSheetsWithPredicate(StyleSheetPredicate& pre }); } -SfxStyleSheetBase* +std::pair<SfxStyleSheetBase*, sal_Int32> IndexedStyleSheets::GetNthStyleSheetThatMatchesPredicate( sal_Int32 n, StyleSheetPredicate& predicate, @@ -158,7 +158,8 @@ IndexedStyleSheets::GetNthStyleSheetThatMatchesPredicate( { SfxStyleSheetBase* retval = nullptr; sal_Int32 matching = 0; - for (VectorType::const_iterator it = mStyleSheets.begin()+startAt; it != mStyleSheets.end(); ++it) { + VectorType::const_iterator it = mStyleSheets.begin()+startAt; + for (; it != mStyleSheets.end(); ++it) { SfxStyleSheetBase *ssheet = it->get(); if (predicate.Check(*ssheet)) { if (matching == n) { @@ -168,7 +169,7 @@ IndexedStyleSheets::GetNthStyleSheetThatMatchesPredicate( ++matching; } } - return retval; + return { retval, std::distance(mStyleSheets.cbegin(), it) }; } sal_Int32 IndexedStyleSheets::FindStyleSheetPosition(const SfxStyleSheetBase& style) const diff --git a/svl/source/items/style.cxx b/svl/source/items/style.cxx index d79adda0eeaa..b9aa5c2cf533 100644 --- a/svl/source/items/style.cxx +++ b/svl/source/items/style.cxx @@ -441,12 +441,12 @@ SfxStyleSheetBase* SfxStyleSheetIterator::operator[](sal_Int32 nIdx) else { DoesStyleMatchStyleSheetPredicate predicate(this); - rtl::Reference< SfxStyleSheetBase > ref = + std::pair<SfxStyleSheetBase*, sal_Int32> aFound = pBasePool->pImpl->mxIndexedStyleSheets->GetNthStyleSheetThatMatchesPredicate(nIdx, predicate); - if (ref) + if (aFound.first) { - mnCurrentPosition = pBasePool->pImpl->mxIndexedStyleSheets->FindStyleSheetPosition(*ref); - retval = ref.get(); + mnCurrentPosition = aFound.second; + retval = aFound.first; } } @@ -497,12 +497,12 @@ SfxStyleSheetBase* SfxStyleSheetIterator::Next() else { DoesStyleMatchStyleSheetPredicate predicate(this); - rtl::Reference< SfxStyleSheetBase > ref = + std::pair< SfxStyleSheetBase*, sal_Int32> aFound = pBasePool->pImpl->mxIndexedStyleSheets->GetNthStyleSheetThatMatchesPredicate( 0, predicate, mnCurrentPosition+1); - retval = ref.get(); + retval = aFound.first; if (retval != nullptr) { - mnCurrentPosition = pBasePool->pImpl->mxIndexedStyleSheets->FindStyleSheetPosition(*ref); + mnCurrentPosition = aFound.second; } } pCurrentStyle = retval;
