optimize expensive EndListener() calls (tdf#102364)

The document has a large (1M cells) column, and when replacing old cells
with new cells during undo as described in the bugreport the repeated
calls to EndListener() from ScColumn::DetachFormulaCell() get quite costly.
Optimize this by doing a single EndListeningFormulaCells() call.

Change-Id: If51dacdfbe876134de956dbc6bbd73e97d9686ff
Reviewed-on: https://gerrit.libreoffice.org/64781
Tested-by: Jenkins
Reviewed-by: Kohei Yoshida <libreoffice@kohei.us>
Reviewed-by: Eike Rathke <erack@redhat.com>
diff --git a/sc/source/core/data/column.cxx b/sc/source/core/data/column.cxx
index bfd11e0..0c151a7 100644
--- a/sc/source/core/data/column.cxx
+++ b/sc/source/core/data/column.cxx
@@ -1633,6 +1633,15 @@
                sc::formula_block::const_iterator itEnd = it;
                std::advance(itEnd, nDataSize);

                if(nDataSize > 1024 && (mnCopyFlags & InsertDeleteFlags::FORMULA) != InsertDeleteFlags::NONE)
                {
                    // If the column to be replaced contains a long formula group (tdf#102364), there can
                    // be so many listeners in a single vector that the quadratic cost of repeatedly removing
                    // the first element becomes very high. Optimize this by removing them in one go.
                    sc::EndListeningContext context(*mrDestCol.GetDoc());
                    mrDestCol.EndListeningFormulaCells( context, nRow, nRow + nDataSize - 1, nullptr, nullptr );
                }

                for (; it != itEnd; ++it, ++nRow)
                    cloneFormulaCell(nRow, **it);
            }