From f002c92553b0cd1ed3b0641494a29a4db69d49bf Mon Sep 17 00:00:00 2001 From: Laurent Redor Date: Wed, 31 Jul 2024 14:23:39 +0200 Subject: [PATCH] [426] Add cache for bounds The bounds for a node can be computed several times for a parent or a child bounds calculation. This commit adds a cache to avoid computing it again. Bug: https://github.com/eclipse-sirius/sirius-desktop/issues/426 --- .../ui/internal/refresh/GMFHelper.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/refresh/GMFHelper.java b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/refresh/GMFHelper.java index 7ff0908f58..04929f6260 100644 --- a/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/refresh/GMFHelper.java +++ b/plugins/org.eclipse.sirius.diagram.ui/src-diag/org/eclipse/sirius/diagram/ui/internal/refresh/GMFHelper.java @@ -16,6 +16,7 @@ import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -132,6 +133,8 @@ public final class GMFHelper { */ private static final int ICON_TEXT_GAP = 3; + private static Map boundsCache = new HashMap<>(); + private GMFHelper() { // Helper to not instantiate } @@ -617,9 +620,21 @@ public static Rectangle getAbsoluteBounds(Node node, boolean insetsAware, boolea * @return the absolute bounds of the node relative to the origin (Diagram) */ public static Rectangle getAbsoluteBounds(Node node, boolean insetsAware, boolean boxForConnection, boolean recursiveGetBounds) { - Point location = getAbsoluteLocation(node, insetsAware); - Rectangle bounds = getBounds(node, false, false, boxForConnection, recursiveGetBounds, location); - return new PrecisionRectangle(location.preciseX(), location.preciseY(), bounds.preciseWidth(), bounds.preciseHeight()); + if (!recursiveGetBounds) { + boundsCache.clear(); + } + Rectangle result; + if (boundsCache.containsKey(node)) { + result = boundsCache.get(node); + } else { + Point location = getAbsoluteLocation(node, insetsAware); + Rectangle bounds = getBounds(node, false, false, boxForConnection, recursiveGetBounds, location); + result = new PrecisionRectangle(location.preciseX(), location.preciseY(), bounds.preciseWidth(), bounds.preciseHeight()); + if (recursiveGetBounds) { + boundsCache.put(node, result); + } + } + return result.getCopy(); } /**