From bf096870c73898963558bef5c9d75760f9f76290 Mon Sep 17 00:00:00 2001 From: Sven Göthel Date: Mon, 22 Jan 2024 06:02:30 +0100 Subject: Bug 1490 - GraphUI Performance: Group/Scene: Use temp sorted arrays Group/Scene's uses temp arrays for Z oder sorting, which should be maintained locally to avoid too many temp object creations. --- src/graphui/classes/com/jogamp/graph/ui/Group.java | 21 ++++++++++++++------- src/graphui/classes/com/jogamp/graph/ui/Scene.java | 20 +++++++++++--------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/graphui/classes/com/jogamp/graph/ui/Group.java b/src/graphui/classes/com/jogamp/graph/ui/Group.java index e3431224c..af07d4053 100644 --- a/src/graphui/classes/com/jogamp/graph/ui/Group.java +++ b/src/graphui/classes/com/jogamp/graph/ui/Group.java @@ -27,6 +27,7 @@ */ package com.jogamp.graph.ui; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Comparator; @@ -38,6 +39,7 @@ import com.jogamp.graph.curve.opengl.RegionRenderer; import com.jogamp.graph.ui.layout.Padding; import com.jogamp.graph.ui.shapes.Rectangle; import com.jogamp.math.FloatUtil; +import com.jogamp.math.Matrix4f; import com.jogamp.math.Vec2f; import com.jogamp.math.Vec3f; import com.jogamp.math.Vec4f; @@ -79,6 +81,7 @@ public class Group extends Shape implements Container { } private final List shapes = new CopyOnWriteArrayList(); + private Shape[] drawShapeArray = new Shape[0]; // reduce memory re-alloc @ display /** Enforced fixed size. In case z-axis is NaN, its 3D z-axis will be adjusted. */ private final Vec3f fixedSize = new Vec3f(); private Layout layouter; @@ -300,6 +303,8 @@ public class Group extends Shape implements Container { // s.clearImpl0(gl, renderer); s.clear(gl, renderer); } + shapes.clear(); + drawShapeArray = new Shape[0]; } @Override @@ -308,6 +313,8 @@ public class Group extends Shape implements Container { // s.destroyImpl0(gl, renderer); s.destroy(gl, renderer); } + shapes.clear(); + drawShapeArray = new Shape[0]; if( null != border ) { border.destroy(gl, renderer); border = null; @@ -322,12 +329,14 @@ public class Group extends Shape implements Container { @Override public final boolean isFrustumCullingEnabled() { return doFrustumCulling; } - @SuppressWarnings({ "unchecked", "rawtypes" }) @Override protected void drawImpl0(final GL2ES2 gl, final RegionRenderer renderer, final Vec4f rgba) { final PMVMatrix4f pmv = renderer.getMatrix(); - final Object[] shapesS = shapes.toArray(); - Arrays.sort(shapesS, (Comparator)Shape.ZAscendingComparator); + final int shapeCount = shapes.size(); + Arrays.fill(drawShapeArray, null); // flush old refs + final Shape[] shapeArray = shapes.toArray(drawShapeArray); // local-backup + drawShapeArray = shapeArray; // keep backup + Arrays.sort(shapeArray, 0, shapeCount, Shape.ZAscendingComparator); final boolean useClipFrustum = null != clipFrustum; if( useClipFrustum || clipOnBounds ) { @@ -336,9 +345,8 @@ public class Group extends Shape implements Container { final Frustum frustumMv = useClipFrustum ? clipFrustum : tempC00.set( box ).transform( pmv.getMv() ).updateFrustumPlanes(tempF00); renderer.setClipFrustum( frustumMv ); - final int shapeCount = shapesS.length; for(int i=0; i shapes = new CopyOnWriteArrayList(); + private Shape[] displayShapeArray = new Shape[0]; // reduce memory re-alloc @ display private final AtomicReference toolTipActive = new AtomicReference(); private final AtomicReference toolTipHUD = new AtomicReference(); @@ -449,13 +451,13 @@ public final class Scene implements Container, GLEventListener { @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public void display(final GLAutoDrawable drawable) { - final Object[] shapesS = shapes.toArray(); - Arrays.sort(shapesS, (Comparator)Shape.ZAscendingComparator); + final int shapeCount = shapes.size(); + Arrays.fill(displayShapeArray, null); // flush old refs + final Shape[] shapeArray = shapes.toArray(displayShapeArray); // local-backup + displayShapeArray = shapeArray; // keep backup + Arrays.sort(shapeArray, 0, shapeCount, Shape.ZAscendingComparator); - display(drawable, shapesS); - } - private void display(final GLAutoDrawable drawable, final Object[] shapes) { final GL2ES2 gl = drawable.getGL().getGL2ES2(); if( null != clearColor ) { @@ -466,9 +468,8 @@ public final class Scene implements Container, GLEventListener { renderer.enable(gl, true); - final int shapeCount = shapes.length; for(int i=0; i * @param pmv a new {@link PMVMatrix4f} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix4f, Recti) be setup}, - * {@link Shape#setTransformMv(PMVMatrix4f) shape-transformed} and can be reused by the caller and runnable. + * {@link Shape#applyMatToMv(PMVMatrix4f) shape-transformed} and can be reused by the caller and runnable. * @param glWinX window X coordinate, bottom-left origin * @param glWinY window Y coordinate, bottom-left origin * @param objPos storage for found object position in model-space of found {@link Shape} @@ -736,7 +738,7 @@ public final class Scene implements Container, GLEventListener { * @param glWinX in GL window coordinates, origin bottom-left * @param glWinY in GL window coordinates, origin bottom-left * @param pmv a new {@link PMVMatrix4f} which will {@link Scene.PMVMatrixSetup#set(PMVMatrix4f, Recti) be setup}, - * {@link Shape#setTransformMv(PMVMatrix4f) shape-transformed} and can be reused by the caller and runnable. + * {@link Shape#applyMatToMv(PMVMatrix4f) shape-transformed} and can be reused by the caller and runnable. * @param objPos resulting object position * @param runnable action */ -- cgit v1.2.3