From 16e417b8c211a919a921baeb65660185aac38393 Mon Sep 17 00:00:00 2001 From: Luboš Luňák Date: Tue, 18 Sep 2018 15:01:29 +0200 Subject: add ScTokenArray::Finalize() to explicitly reduce memory usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- formula/source/core/api/token.cxx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'formula') 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 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 ]); -- cgit