diff options
author | Luboš Luňák <l.lunak@collabora.com> | 2018-09-18 15:00:38 +0200 |
---|---|---|
committer | Luboš Luňák <l.lunak@collabora.com> | 2018-10-10 12:58:30 +0200 |
commit | 7600c63424db644065d736158c182cb9498574e9 (patch) | |
tree | ceff327a8ee8b5c45523d7702f1ad383f18bd50d /formula | |
parent | b5680189c930cc58cf523c0dea175a09cb390517 (diff) |
avoid usually needless large allocation for formula token array
Change-Id: I855af060e1aeb91bccfc923ca567ad34d64be757
Reviewed-on: https://gerrit.libreoffice.org/60861
Tested-by: Jenkins
Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
Diffstat (limited to 'formula')
-rw-r--r-- | formula/source/core/api/token.cxx | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/formula/source/core/api/token.cxx b/formula/source/core/api/token.cxx index 58fa8b77ece9..aa3e576a9323 100644 --- a/formula/source/core/api/token.cxx +++ b/formula/source/core/api/token.cxx @@ -776,8 +776,19 @@ FormulaToken* FormulaTokenArray::Add( FormulaToken* t ) return nullptr; } +// 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. + const size_t MAX_FAST_TOKENS = 32; if( !pCode ) - pCode.reset(new FormulaToken*[ FORMULA_MAXTOKENS ]); + pCode.reset(new FormulaToken*[ MAX_FAST_TOKENS ]); + if( nLen == MAX_FAST_TOKENS ) + { + FormulaToken** tmp = new FormulaToken*[ FORMULA_MAXTOKENS ]; + std::copy(&pCode[0], &pCode[MAX_FAST_TOKENS], tmp); + pCode.reset(tmp); + } if( nLen < FORMULA_MAXTOKENS - 1 ) { CheckToken(*t); |