summaryrefslogtreecommitdiff
path: root/formula/source
diff options
context:
space:
mode:
authorLuboš Luňák <l.lunak@collabora.com>2018-09-18 15:01:29 +0200
committerLuboš Luňák <l.lunak@collabora.com>2018-10-10 12:59:39 +0200
commit16e417b8c211a919a921baeb65660185aac38393 (patch)
treec3cbc6d190fcc96a92694ed14e5961b7c57cc3ac /formula/source
parent7600c63424db644065d736158c182cb9498574e9 (diff)
add ScTokenArray::Finalize() to explicitly reduce memory usage
Since ScTokenArray::Add() overallocates memory, make sure we do not keep such possibly large arrays. Since any copying of ScTokenArray implicitly finalizes as well, this is not a big problem right now, but then why needlessly do the copies? (next commit) Change-Id: I55398bcd8fb31f1be5a4b8e3f5a71b26649a7594 Reviewed-on: https://gerrit.libreoffice.org/60862 Tested-by: Jenkins Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
Diffstat (limited to 'formula/source')
-rw-r--r--formula/source/core/api/token.cxx15
1 files changed, 14 insertions, 1 deletions
diff --git a/formula/source/core/api/token.cxx b/formula/source/core/api/token.cxx
index aa3e576a9323..cefab324fd53 100644
--- a/formula/source/core/api/token.cxx
+++ b/formula/source/core/api/token.cxx
@@ -579,6 +579,18 @@ FormulaTokenArray::~FormulaTokenArray()
Clear();
}
+void FormulaTokenArray::Finalize()
+{
+ if( nLen && !mbFinalized )
+ {
+ // Add() overallocates, so reallocate to the minimum needed size.
+ std::unique_ptr<FormulaToken*[]> newCode(new FormulaToken*[ nLen ]);
+ std::copy(&pCode[0], &pCode[nLen], newCode.get());
+ pCode = std::move( newCode );
+ mbFinalized = true;
+ }
+}
+
void FormulaTokenArray::Assign( const FormulaTokenArray& r )
{
nLen = r.nLen;
@@ -779,7 +791,8 @@ FormulaToken* FormulaTokenArray::Add( FormulaToken* t )
// Allocating an array of size FORMULA_MAXTOKENS is simple, but that results in relatively large
// allocations that malloc() implementations usually do not handle as efficiently as smaller
// sizes (not only in terms of memory usage but also speed). Since most token arrays are going
-// to be small, start with a small array and resize only if needed.
+// to be small, start with a small array and resize only if needed. Eventually Finalize() will
+// reallocate the memory to size exactly matching the requirements.
const size_t MAX_FAST_TOKENS = 32;
if( !pCode )
pCode.reset(new FormulaToken*[ MAX_FAST_TOKENS ]);