From ad2f1d2675ce480abf06db280fc551372b3cbed3 Mon Sep 17 00:00:00 2001 From: Eike Rathke Date: Mon, 23 Oct 2017 16:15:49 +0200 Subject: Assume 6GB memory could be consumed by matrices ... if SC_MAX_MATRIX_ELEMENTS is not set. Change-Id: I27100d5fc75e2d5288892c54997147854de781e7 --- sc/source/core/tool/scmatrix.cxx | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'sc/source') diff --git a/sc/source/core/tool/scmatrix.cxx b/sc/source/core/tool/scmatrix.cxx index 9fa822fad412..32dc362245bd 100644 --- a/sc/source/core/tool/scmatrix.cxx +++ b/sc/source/core/tool/scmatrix.cxx @@ -339,13 +339,23 @@ private: static bool bElementsMaxFetched; static size_t nElementsMax; -/// The maximum number of elements a matrix may have at runtime. -static size_t GetElementsMax() +/** The maximum number of elements a matrix or the pool may have at runtime. + + @param nMemory + If 0, the arbitrary limit of one matrix is returned. + If >0, the given memory pool divided by the average size of a + matrix element is returned, which is used to initialize + nElementsMax. + */ +static size_t GetElementsMax( size_t nMemory ) { // Arbitrarily assuming 12 bytes per element, 8 bytes double plus // overhead. Stored as an array in an mdds container it's less, but for // strings or mixed matrix it can be much more.. constexpr size_t nPerElem = 12; + if (nMemory) + return nMemory / nPerElem; + // Arbitrarily assuming 1GB memory. Could be dynamic at some point. constexpr size_t nMemMax = 0x40000000; // With 1GB that's ~85M elements, or 85 whole columns. @@ -2797,8 +2807,21 @@ bool ScMatrix::IsSizeAllocatable( SCSIZE nC, SCSIZE nR ) if (!bElementsMaxFetched) { - nElementsMax = std::getenv("SC_MAX_MATRIX_ELEMENTS") ? std::atoi(std::getenv("SC_MAX_MATRIX_ELEMENTS")) - : GetElementsMax(); + const char* pEnv = std::getenv("SC_MAX_MATRIX_ELEMENTS"); + if (pEnv) + { + // Environment specifies the overall elements pool. + nElementsMax = std::atoi(pEnv); + } + else + { + // GetElementsMax() uses an (~arbitrary) elements limit. + // Assume 6GB memory could be consumed by matrices. + // The actual allocation depends on the types of individual matrix + // elements and is averaged for type double. + constexpr size_t nMemMax = 0x180000000; + nElementsMax = GetElementsMax( nMemMax); + } bElementsMaxFetched = true; } -- cgit