sc/Library_scfilt.mk | 1 sc/source/filter/inc/patterncache.hxx | 46 +++++++++++++++++++++++++++ sc/source/filter/inc/stylesbuffer.hxx | 5 ++ sc/source/filter/oox/patterncache.cxx | 52 +++++++++++++++++++++++++++++++ sc/source/filter/oox/sheetdatabuffer.cxx | 6 ++- sc/source/filter/oox/stylesbuffer.cxx | 23 +++++++++---- 6 files changed, 124 insertions(+), 9 deletions(-)
New commits: commit 447a4bcac071840e5dd19cdaaefc6897d5d19cbe Author: Dennis Francis <dennis.fran...@collabora.com> AuthorDate: Wed Jun 7 11:34:56 2023 +0530 Commit: Noel Grandin <noel.gran...@collabora.co.uk> CommitDate: Sat Jun 17 09:08:03 2023 +0200 sc: perf: speedup sheets with lots of repetitive... row styles using a small cache of already allocated patterns that are tied to extended format id and number format id. Change-Id: I3136aef9a034635924f7b7b6d2432f9ae5c2bd15 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/152692 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Reviewed-by: Andras Timar <andras.ti...@collabora.com> (cherry picked from commit 92b13ad1f1a87393cf66a35694f3e542db57f150) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/152874 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk> diff --git a/sc/Library_scfilt.mk b/sc/Library_scfilt.mk index 5590a4ab7605..aa4e1fae787c 100644 --- a/sc/Library_scfilt.mk +++ b/sc/Library_scfilt.mk @@ -189,6 +189,7 @@ $(eval $(call gb_Library_add_exception_objects,scfilt,\ sc/source/filter/oox/numberformatsbuffer \ sc/source/filter/oox/ooxformulaparser \ sc/source/filter/oox/pagesettings \ + sc/source/filter/oox/patterncache \ sc/source/filter/oox/pivotcachebuffer \ sc/source/filter/oox/pivotcachefragment \ sc/source/filter/oox/pivottablebuffer \ diff --git a/sc/source/filter/inc/patterncache.hxx b/sc/source/filter/inc/patterncache.hxx new file mode 100644 index 000000000000..25013a930143 --- /dev/null +++ b/sc/source/filter/inc/patterncache.hxx @@ -0,0 +1,46 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include <sal/types.h> + +class ScPatternAttr; + +constexpr sal_Int32 nPatternCacheSize = 16; +class ScPatternCache +{ + struct Entry + { + sal_Int32 nXfId; + sal_Int32 nNumFmtId; + ScPatternAttr* pPattern; + + Entry(); + }; + + Entry maEntries[nPatternCacheSize]; + sal_Int32 nNextPos; + +public: + ScPatternCache(); + + ScPatternAttr* query(sal_Int32 nXfId, sal_Int32 nNumFmtId) const; + void add(sal_Int32 nXfId, sal_Int32 nNumFmtId, ScPatternAttr* pPattern); +}; diff --git a/sc/source/filter/inc/stylesbuffer.hxx b/sc/source/filter/inc/stylesbuffer.hxx index a687a14d3b31..c80eab77241d 100644 --- a/sc/source/filter/inc/stylesbuffer.hxx +++ b/sc/source/filter/inc/stylesbuffer.hxx @@ -35,6 +35,8 @@ #include <attarray.hxx> #include <vector> +class ScPatternCache; + namespace oox { class SequenceInputStream; } namespace oox { class PropertySet; @@ -623,7 +625,8 @@ public: const Alignment& getAlignment() const { return maAlignment; } void applyPatternToAttrList( - AttrList& rAttrs, SCROW nRow1, SCROW nRow2, sal_Int32 nForceScNumFmt ); + AttrList& rAttrs, SCROW nRow1, SCROW nRow2, sal_Int32 nXfId, + sal_Int32 nForceScNumFmt, ScPatternCache& rCache ); void writeToDoc( ScDocumentImport& rDoc, const ScRange& rRange ); diff --git a/sc/source/filter/oox/patterncache.cxx b/sc/source/filter/oox/patterncache.cxx new file mode 100644 index 000000000000..2431a36fed20 --- /dev/null +++ b/sc/source/filter/oox/patterncache.cxx @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <patterncache.hxx> + +ScPatternCache::Entry::Entry() + : nXfId(-1) + , nNumFmtId(-1) + , pPattern(nullptr) +{ +} + +ScPatternCache::ScPatternCache() + : nNextPos(0) +{ +} + +ScPatternAttr* ScPatternCache::query(sal_Int32 nXfId, sal_Int32 nNumFmtId) const +{ + for (const auto& entry : maEntries) + { + if (entry.nXfId == nXfId && entry.nNumFmtId == nNumFmtId) + return entry.pPattern; + } + + return nullptr; +} + +void ScPatternCache::add(sal_Int32 nXfId, sal_Int32 nNumFmtId, ScPatternAttr* pPattern) +{ + Entry& rEntry = maEntries[nNextPos]; + nNextPos = ((nNextPos + 1) % nPatternCacheSize); + rEntry.nXfId = nXfId; + rEntry.nNumFmtId = nNumFmtId; + rEntry.pPattern = pPattern; +} diff --git a/sc/source/filter/oox/sheetdatabuffer.cxx b/sc/source/filter/oox/sheetdatabuffer.cxx index 5632d8356353..0769897e2620 100644 --- a/sc/source/filter/oox/sheetdatabuffer.cxx +++ b/sc/source/filter/oox/sheetdatabuffer.cxx @@ -18,6 +18,7 @@ */ #include <sheetdatabuffer.hxx> +#include <patterncache.hxx> #include <algorithm> #include <com/sun/star/sheet/XArrayFormulaTokens.hpp> @@ -481,6 +482,8 @@ void SheetDataBuffer::finalizeImport() ScDocumentImport::Attrs aPendingAttrParam; SCCOL pendingColStart = -1; SCCOL pendingColEnd = -1; + ScPatternCache aPatternCache; + for ( const auto& [rCol, rRowStyles] : maStylesPerColumn ) { SCCOL nScCol = static_cast< SCCOL >( rCol ); @@ -510,7 +513,8 @@ void SheetDataBuffer::finalizeImport() Xf* pXf = rStyles.getCellXf( rRowStyle.mnNumFmt.first ).get(); if ( pXf ) - pXf->applyPatternToAttrList( aAttrs, rRowStyle.mnStartRow, rRowStyle.mnEndRow, rRowStyle.mnNumFmt.second ); + pXf->applyPatternToAttrList( aAttrs, rRowStyle.mnStartRow, rRowStyle.mnEndRow, + rRowStyle.mnNumFmt.first, rRowStyle.mnNumFmt.second, aPatternCache ); } if (aAttrs.maAttrs.empty() || aAttrs.maAttrs.back().nEndRow != rDoc.MaxRow()) { diff --git a/sc/source/filter/oox/stylesbuffer.cxx b/sc/source/filter/oox/stylesbuffer.cxx index 1dbe7e658269..944ff7778ff6 100644 --- a/sc/source/filter/oox/stylesbuffer.cxx +++ b/sc/source/filter/oox/stylesbuffer.cxx @@ -18,6 +18,7 @@ */ #include <stylesbuffer.hxx> +#include <patterncache.hxx> #include <com/sun/star/awt/FontDescriptor.hpp> #include <com/sun/star/awt/FontFamily.hpp> @@ -2065,13 +2066,16 @@ FontRef Xf::getFont() const return getStyles().getFont( maModel.mnFontId ); } -void Xf::applyPatternToAttrList( AttrList& rAttrs, SCROW nRow1, SCROW nRow2, sal_Int32 nNumFmtId ) +void Xf::applyPatternToAttrList( AttrList& rAttrs, SCROW nRow1, SCROW nRow2, sal_Int32 nXfId, sal_Int32 nNumFmtId, ScPatternCache& rCache ) { - createPattern(); - ScPatternAttr& rPat = *mpPattern; + ScPatternAttr* pCachedPattern = rCache.query(nXfId, nNumFmtId); + if (!pCachedPattern) + createPattern(); + + ScPatternAttr& rPat = pCachedPattern ? *pCachedPattern : *mpPattern; ScDocumentImport& rDocImport = getDocImport(); ScDocument& rDoc = getScDocument(); - if ( isCellXf() ) + if ( !pCachedPattern && isCellXf() ) { StylesBuffer& rStyles = getStyles(); rStyles.createCellStyle( maModel.mnStyleXfId ); @@ -2096,19 +2100,20 @@ void Xf::applyPatternToAttrList( AttrList& rAttrs, SCROW nRow1, SCROW nRow2, sal } } } - if ( nNumFmtId >= 0 ) + if ( !pCachedPattern && nNumFmtId >= 0 ) { ScPatternAttr aNumPat(rDoc.GetPool()); mnScNumFmt = getStyles().writeNumFmtToItemSet( aNumPat.GetItemSet(), nNumFmtId, false ); rPat.GetItemSet().Put(aNumPat.GetItemSet()); } - if (!rDocImport.isLatinScript(mnScNumFmt)) + if (!pCachedPattern && !rDocImport.isLatinScript(mnScNumFmt)) rAttrs.mbLatinNumFmtOnly = false; - if (!rPat.GetStyleName()) + if (!pCachedPattern && !rPat.GetStyleName()) return; + // Check for a gap between the last entry and this one. bool bHasGap = false; if (rAttrs.maAttrs.empty() && nRow1 > 0) @@ -2134,6 +2139,10 @@ void Xf::applyPatternToAttrList( AttrList& rAttrs, SCROW nRow1, SCROW nRow2, sal ScAttrEntry aEntry; aEntry.nEndRow = nRow2; aEntry.pPattern = &rDoc.GetPool()->Put(rPat); + // Put the allocated pattern to cache + if (!pCachedPattern) + rCache.add(nXfId, nNumFmtId, const_cast<ScPatternAttr*>(aEntry.pPattern)); + rAttrs.maAttrs.push_back(aEntry); if (!rDocImport.isLatinScript(*aEntry.pPattern))