fdo#75741: Share the context objects to avoid poor performance.

Change-Id: I1015dbfa9e2220cd23244dae17ee8dc4828c6a67
diff --git a/sc/inc/column.hxx b/sc/inc/column.hxx
index 42f2779..9318a77 100644
--- a/sc/inc/column.hxx
+++ b/sc/inc/column.hxx
@@ -372,8 +372,10 @@ public:
    void        SetTabNo(SCTAB nNewTab);
    void        FindRangeNamesInUse(SCROW nRow1, SCROW nRow2, std::set<sal_uInt16>& rIndexes) const;

    void PreprocessRangeNameUpdate();
    void PostprocessRangeNameUpdate();
    void PreprocessRangeNameUpdate(
        sc::EndListeningContext& rEndListenCxt, sc::CompileFormulaContext& rCompileCxt );

    void PostprocessRangeNameUpdate( sc::CompileFormulaContext& rCompileCxt );

    const SfxPoolItem*      GetAttr( SCROW nRow, sal_uInt16 nWhich ) const;
    const ScPatternAttr*    GetPattern( SCROW nRow ) const;
diff --git a/sc/inc/table.hxx b/sc/inc/table.hxx
index b325c80..7da8177 100644
--- a/sc/inc/table.hxx
+++ b/sc/inc/table.hxx
@@ -847,8 +847,10 @@ public:
    void SetRangeName(ScRangeName* pNew);
    ScRangeName* GetRangeName() const;

    void PreprocessRangeNameUpdate();
    void PostprocessRangeNameUpdate();
    void PreprocessRangeNameUpdate(
        sc::EndListeningContext& rEndListenCxt, sc::CompileFormulaContext& rCompileCxt );

    void PostprocessRangeNameUpdate( sc::CompileFormulaContext& rCompileCxt );

    ScConditionalFormatList* GetCondFormList();
    const ScConditionalFormatList* GetCondFormList() const;
diff --git a/sc/source/core/data/column4.cxx b/sc/source/core/data/column4.cxx
index 7a360d0..c658124 100644
--- a/sc/source/core/data/column4.cxx
+++ b/sc/source/core/data/column4.cxx
@@ -554,14 +554,14 @@ namespace {
class PreRangeNameUpdateHandler
{
    ScDocument* mpDoc;
    boost::shared_ptr<sc::EndListeningContext> mpEndListenCxt;
    boost::shared_ptr<sc::CompileFormulaContext> mpCompileFormulaCxt;
    sc::EndListeningContext& mrEndListenCxt;
    sc::CompileFormulaContext& mrCompileFormulaCxt;

public:
    PreRangeNameUpdateHandler( ScDocument* pDoc ) :
    PreRangeNameUpdateHandler( ScDocument* pDoc, sc::EndListeningContext& rEndListenCxt, sc::CompileFormulaContext& rCompileCxt ) :
        mpDoc(pDoc),
        mpEndListenCxt(new sc::EndListeningContext(*pDoc)),
        mpCompileFormulaCxt(new sc::CompileFormulaContext(pDoc)) {}
        mrEndListenCxt(rEndListenCxt),
        mrCompileFormulaCxt(rCompileCxt) {}

    void operator() ( sc::FormulaGroupEntry& rEntry )
    {
@@ -589,7 +589,7 @@ public:
        if (bRecompile)
        {
            // Get the formula string.
            OUString aFormula = pTop->GetFormula(*mpCompileFormulaCxt);
            OUString aFormula = pTop->GetFormula(mrCompileFormulaCxt);
            sal_Int32 n = aFormula.getLength();
            if (pTop->GetMatrixFlag() != MM_NONE && n > 0)
            {
@@ -604,13 +604,13 @@ public:
                for (; pp != ppEnd; ++pp)
                {
                    ScFormulaCell* p = *pp;
                    p->EndListeningTo(*mpEndListenCxt);
                    p->EndListeningTo(mrEndListenCxt);
                    mpDoc->RemoveFromFormulaTree(p);
                }
            }
            else
            {
                rEntry.mpCell->EndListeningTo(*mpEndListenCxt);
                rEntry.mpCell->EndListeningTo(mrEndListenCxt);
                mpDoc->RemoveFromFormulaTree(rEntry.mpCell);
            }

@@ -623,12 +623,12 @@ public:
class PostRangeNameUpdateHandler
{
    ScDocument* mpDoc;
    boost::shared_ptr<sc::CompileFormulaContext> mpCompileFormulaCxt;
    sc::CompileFormulaContext& mrCompileFormulaCxt;

public:
    PostRangeNameUpdateHandler( ScDocument* pDoc ) :
    PostRangeNameUpdateHandler( ScDocument* pDoc, sc::CompileFormulaContext& rCompileCxt ) :
        mpDoc(pDoc),
        mpCompileFormulaCxt(new sc::CompileFormulaContext(pDoc)) {}
        mrCompileFormulaCxt(rCompileCxt) {}

    void operator() ( sc::FormulaGroupEntry& rEntry )
    {
@@ -639,7 +639,7 @@ public:

            // Create a new token array from the hybrid formula string, and
            // set it to the group.
            ScCompiler aComp(*mpCompileFormulaCxt, pTop->aPos);
            ScCompiler aComp(mrCompileFormulaCxt, pTop->aPos);
            ScTokenArray* pNewCode = aComp.CompileString(aFormula);
            ScFormulaCellGroupRef xGroup = pTop->GetCellGroup();
            assert(xGroup);
@@ -662,7 +662,7 @@ public:
            OUString aFormula = pCell->GetHybridFormula();

            // Create token array from formula string.
            ScCompiler aComp(*mpCompileFormulaCxt, pCell->aPos);
            ScCompiler aComp(mrCompileFormulaCxt, pCell->aPos);
            ScTokenArray* pNewCode = aComp.CompileString(aFormula);

            // Generate RPN tokens.
@@ -677,21 +677,22 @@ public:

}

void ScColumn::PreprocessRangeNameUpdate()
void ScColumn::PreprocessRangeNameUpdate(
    sc::EndListeningContext& rEndListenCxt, sc::CompileFormulaContext& rCompileCxt )
{
    // Collect all formula groups.
    std::vector<sc::FormulaGroupEntry> aGroups = GetFormulaGroupEntries();

    PreRangeNameUpdateHandler aFunc(pDocument);
    PreRangeNameUpdateHandler aFunc(pDocument, rEndListenCxt, rCompileCxt);
    std::for_each(aGroups.begin(), aGroups.end(), aFunc);
}

void ScColumn::PostprocessRangeNameUpdate()
void ScColumn::PostprocessRangeNameUpdate( sc::CompileFormulaContext& rCompileCxt )
{
    // Collect all formula groups.
    std::vector<sc::FormulaGroupEntry> aGroups = GetFormulaGroupEntries();

    PostRangeNameUpdateHandler aFunc(pDocument);
    PostRangeNameUpdateHandler aFunc(pDocument, rCompileCxt);
    std::for_each(aGroups.begin(), aGroups.end(), aFunc);
}

diff --git a/sc/source/core/data/document10.cxx b/sc/source/core/data/document10.cxx
index 84410ab..e4a2ff1 100644
--- a/sc/source/core/data/document10.cxx
+++ b/sc/source/core/data/document10.cxx
@@ -14,6 +14,8 @@
#include <table.hxx>
#include <tokenarray.hxx>
#include <editutil.hxx>
#include <listenercontext.hxx>
#include <tokenstringcontext.hxx>

// Add totally brand-new methods to this source file.

@@ -239,21 +241,25 @@ const ScCalcConfig& ScDocument::GetCalcConfig() const

void ScDocument::PreprocessRangeNameUpdate()
{
    sc::EndListeningContext aEndListenCxt(*this);
    sc::CompileFormulaContext aCompileCxt(this);

    TableContainer::iterator it = maTabs.begin(), itEnd = maTabs.end();
    for (; it != itEnd; ++it)
    {
        ScTable* p = *it;
        p->PreprocessRangeNameUpdate();
        p->PreprocessRangeNameUpdate(aEndListenCxt, aCompileCxt);
    }
}

void ScDocument::PostprocessRangeNameUpdate()
{
    sc::CompileFormulaContext aCompileCxt(this);
    TableContainer::iterator it = maTabs.begin(), itEnd = maTabs.end();
    for (; it != itEnd; ++it)
    {
        ScTable* p = *it;
        p->PostprocessRangeNameUpdate();
        p->PostprocessRangeNameUpdate(aCompileCxt);
    }
}

diff --git a/sc/source/core/data/table7.cxx b/sc/source/core/data/table7.cxx
index 9406326..48aa0a4 100644
--- a/sc/source/core/data/table7.cxx
+++ b/sc/source/core/data/table7.cxx
@@ -80,16 +80,17 @@ void ScTable::CopyCellValuesFrom( SCCOL nCol, SCROW nRow, const sc::CellValues& 
    aCol[nCol].CopyCellValuesFrom(nRow, rSrc);
}

void ScTable::PreprocessRangeNameUpdate()
void ScTable::PreprocessRangeNameUpdate(
    sc::EndListeningContext& rEndListenCxt, sc::CompileFormulaContext& rCompileCxt )
{
    for (SCCOL i = 0; i <= MAXCOL; ++i)
        aCol[i].PreprocessRangeNameUpdate();
        aCol[i].PreprocessRangeNameUpdate(rEndListenCxt, rCompileCxt);
}

void ScTable::PostprocessRangeNameUpdate()
void ScTable::PostprocessRangeNameUpdate( sc::CompileFormulaContext& rCompileCxt )
{
    for (SCCOL i = 0; i <= MAXCOL; ++i)
        aCol[i].PostprocessRangeNameUpdate();
        aCol[i].PostprocessRangeNameUpdate(rCompileCxt);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */