summaryrefslogtreecommitdiff
path: root/sw/source
diff options
context:
space:
mode:
authorLuboš Luňák <l.lunak@collabora.com>2021-09-15 13:49:11 +0200
committerLuboš Luňák <l.lunak@collabora.com>2021-09-19 09:33:11 +0200
commitf90fac8e0c7cf4b4ffcca8ad9cdc076460360cdb (patch)
treec0d75fc030b65284cd4d1f4c130095ff395c97a2 /sw/source
parent328f91e0d373006f0039c16cde28f44116f45f29 (diff)
optimize SwRegionRects::Compress()
This function gets called on modifying documents, and it shows up noticeably e.g. when profiling editing in Online. The code repeatedly restarts the whole process on most changes, but in practice this seems to only lead to repeated checks with the same outcome. So finish the whole compression and try again only at the end, if deemed needed. And even there I'm not sure if it's actually ever needed, since I couldn't trigger such a case in practice. Change-Id: I15b0a83c6865bad21fe60a7e9ca0b87ceea5b489 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122156 Tested-by: Luboš Luňák <l.lunak@collabora.com> Reviewed-by: Luboš Luňák <l.lunak@collabora.com> (cherry picked from commit d13b63a9859d653d1aba688a56590aa0ef24b89c) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122121 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Diffstat (limited to 'sw/source')
-rw-r--r--sw/source/core/bastyp/swregion.cxx92
1 files changed, 54 insertions, 38 deletions
diff --git a/sw/source/core/bastyp/swregion.cxx b/sw/source/core/bastyp/swregion.cxx
index 09e07757b236..defc4c07e95c 100644
--- a/sw/source/core/bastyp/swregion.cxx
+++ b/sw/source/core/bastyp/swregion.cxx
@@ -136,7 +136,7 @@ void SwRegionRects::Invert()
swap( aInvRegion );
}
-static SwTwips CalcArea( const SwRect &rRect )
+static inline SwTwips CalcArea( const SwRect &rRect )
{
return rRect.Width() * rRect.Height();
}
@@ -144,51 +144,67 @@ static SwTwips CalcArea( const SwRect &rRect )
// combine all adjacent rectangles
void SwRegionRects::Compress()
{
- for (size_type i = 0; i < size(); )
+ bool bAgain;
+ do
{
- bool bRestart(false);
- for ( size_type j = i+1; j < size(); ++j )
+ bAgain = false;
+ for (size_type i = 0; i < size(); ++i )
{
- // If one rectangle contains a second completely than the latter
- // does not need to be stored and can be deleted
- if ( (*this)[i].IsInside( (*this)[j] ) )
+ for ( size_type j = i+1; j < size(); ++j )
{
- erase( begin() + j );
- --j;
- }
- else if ( (*this)[j].IsInside( (*this)[i] ) )
- {
- (*this)[i] = (*this)[j];
- erase( begin() + j );
- bRestart = true;
- break;
- }
- else
- {
- // If two rectangles have the same area of their union minus the
- // intersection then one of them can be deleted.
- // For combining as much as possible (and for having less single
- // paints), the area of the union can be a little bit larger:
- // ( 9622 * 141.5 = 1361513 ~= a quarter (1/4) centimeter wider
- // than the width of an A4 page
- const tools::Long nFuzzy = 1361513;
- SwRect aUnion( (*this)[i] );
- aUnion.Union( (*this)[j] );
- SwRect aInter( (*this)[i] );
- aInter.Intersection( (*this)[j] );
- if ( (::CalcArea( (*this)[i] ) +
- ::CalcArea( (*this)[j] ) + nFuzzy) >=
- (::CalcArea( aUnion ) - CalcArea( aInter )) )
+ // If one rectangle contains a second completely than the latter
+ // does not need to be stored and can be deleted
+ if ( (*this)[i].IsInside( (*this)[j] ) )
+ {
+ erase( begin() + j );
+ --j;
+ }
+ else if ( (*this)[j].IsInside( (*this)[i] ) )
{
- (*this)[i] = aUnion;
+ (*this)[i] = (*this)[j];
erase( begin() + j );
- bRestart = true;
- break;
+ --j;
+ bAgain = true;
+ }
+ else
+ {
+ // TODO: I think the comment below and the code are partially incorrect.
+ // An obvious mistake is the comment saying that one rectangle can be deleted,
+ // while it's the union that gets used instead of the two rectangles.
+ // I think this code is supposed to merge adjacent rectangles (possibly
+ // overlapping), and such rectangles can be detected by their merged areas
+ // being equal to the area of the union (which is obviously the case if they
+ // share one side, and using the nFuzzy extra allow merging also rectangles
+ // that do not quite cover the entire union but it's close enough).
+ // So another mistake seems to be using '- CalcArea( aInter )',
+ // it should be on the other side of the comparison to subtract shared area
+ // counted twice. In practice it seems rectangles here do not share areas,
+ // so the error is irrelevant.
+
+ // If two rectangles have the same area of their union minus the
+ // intersection then one of them can be deleted.
+ // For combining as much as possible (and for having less single
+ // paints), the area of the union can be a little bit larger:
+ // ( 9622 * 141.5 = 1361513 ~= a quarter (1/4) centimeter wider
+ // than the width of an A4 page
+ const tools::Long nFuzzy = 1361513;
+ SwRect aUnion( (*this)[i] );
+ aUnion.Union( (*this)[j] );
+ SwRect aInter( (*this)[i] );
+ aInter.Intersection( (*this)[j] );
+ if ( (::CalcArea( (*this)[i] ) +
+ ::CalcArea( (*this)[j] ) + nFuzzy) >=
+ (::CalcArea( aUnion ) - CalcArea( aInter )) )
+ {
+ (*this)[i] = aUnion;
+ erase( begin() + j );
+ --j;
+ bAgain = true;
+ }
}
}
}
- i = bRestart ? 0 : i+1;
- }
+ } while(bAgain);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */