From 197088a3f7be5b665d78d9b511949d7345830da8 Mon Sep 17 00:00:00 2001
From: Matevz Tadel <matevz.tadel@cern.ch>
Date: Wed, 8 Dec 2010 13:54:23 +0000
Subject: [PATCH] Optimizations based on Druid callgrind dump sent by Manqi.

* TGLBoundingBox:
  Use TGLVertex3[8] instead of std::vector<> for member that always
  has 8 elements. Caused pointless allocations and fragmentation.

* TGLScene:
  In CalcBoundingBox() avoid creating a bounding box temporary in a
  loop over all elements -- use const& instead.

* TGLCamera:
  Fix for changes in TGLBoundingBox.


git-svn-id: http://root.cern.ch/svn/root/trunk@37397 27541ba8-7e3a-0410-8455-c3a389f83636
---
 graf3d/gl/inc/TGLBoundingBox.h   |  8 +++++---
 graf3d/gl/src/TGLBoundingBox.cxx | 15 +++++----------
 graf3d/gl/src/TGLCamera.cxx      |  6 +++---
 graf3d/gl/src/TGLScene.cxx       |  2 +-
 4 files changed, 14 insertions(+), 17 deletions(-)

diff --git a/graf3d/gl/inc/TGLBoundingBox.h b/graf3d/gl/inc/TGLBoundingBox.h
index 74e605001e7..2ccec17037e 100644
--- a/graf3d/gl/inc/TGLBoundingBox.h
+++ b/graf3d/gl/inc/TGLBoundingBox.h
@@ -55,7 +55,7 @@ private:
    // For axis aligned 2 verticies would suffice.
    // Rest could be calculated on demand - however speed more important
    // than memory considerations
-   std::vector<TGLVertex3> fVertex;     //! the 8 bounding box vertices
+   TGLVertex3              fVertex[8];  //! the 8 bounding box vertices
    Double_t                fVolume;     //! box volume - cached for speed
    Double_t                fDiagonal;   //! max box diagonal - cached for speed
    TGLVector3              fAxes[3];    //! box axes in global frame - cached for speed
@@ -107,7 +107,9 @@ public:
    TGLVertex3 MaxAAVertex() const;
 
    // Multiple vertices accessors
-   const std::vector<TGLVertex3> & Vertices() const;           // All 8 box vertices
+   const TGLVertex3* Vertices() const;           // All 8 box vertices
+   Int_t             NumVertices() const { return 8; }
+
    enum EFace { kFaceLowX, kFaceHighX, kFaceLowY, kFaceHighY, kFaceLowZ, kFaceHighZ, kFaceCount };
    const std::vector<UInt_t> & FaceVertices(EFace face) const; // 4 box face vertices
 
@@ -154,7 +156,7 @@ inline const TGLVertex3 & TGLBoundingBox::Vertex(UInt_t index) const
 }
 
 //______________________________________________________________________________
-inline const std::vector<TGLVertex3> & TGLBoundingBox::Vertices() const
+inline const TGLVertex3* TGLBoundingBox::Vertices() const
 {
    return fVertex;
 }
diff --git a/graf3d/gl/src/TGLBoundingBox.cxx b/graf3d/gl/src/TGLBoundingBox.cxx
index ef591c36dbe..301090604d1 100644
--- a/graf3d/gl/src/TGLBoundingBox.cxx
+++ b/graf3d/gl/src/TGLBoundingBox.cxx
@@ -28,40 +28,35 @@
 ClassImp(TGLBoundingBox)
 
 //______________________________________________________________________________
-TGLBoundingBox::TGLBoundingBox() :
-   fVertex(8)
+TGLBoundingBox::TGLBoundingBox()
 {
    // Construct an empty bounding box
    SetEmpty();
 }
 
 //______________________________________________________________________________
-TGLBoundingBox::TGLBoundingBox(const TGLVertex3 vertex[8]) :
-   fVertex(8)
+TGLBoundingBox::TGLBoundingBox(const TGLVertex3 vertex[8])
 {
    // Construct a bounding box from provided 8 vertices
    Set(vertex);
 }
 
 //______________________________________________________________________________
-TGLBoundingBox::TGLBoundingBox(const Double_t vertex[8][3]) :
-   fVertex(8)
+TGLBoundingBox::TGLBoundingBox(const Double_t vertex[8][3])
 {
    // Construct a bounding box from provided 8 vertices
    Set(vertex);
 }
 
 //______________________________________________________________________________
-TGLBoundingBox::TGLBoundingBox(const TGLVertex3 & lowVertex, const TGLVertex3 & highVertex) :
-   fVertex(8)
+TGLBoundingBox::TGLBoundingBox(const TGLVertex3 & lowVertex, const TGLVertex3 & highVertex)
 {
    // Construct an global axis ALIGNED bounding box from provided low/high vertex pair
    SetAligned(lowVertex, highVertex);
 }
 
 //______________________________________________________________________________
-TGLBoundingBox::TGLBoundingBox(const TGLBoundingBox & other) :
-   fVertex(8)
+TGLBoundingBox::TGLBoundingBox(const TGLBoundingBox & other)
 {
    // Construct a bounding box as copy of existing one
    Set(other);
diff --git a/graf3d/gl/src/TGLCamera.cxx b/graf3d/gl/src/TGLCamera.cxx
index cef0303275a..cc6cfcea510 100644
--- a/graf3d/gl/src/TGLCamera.cxx
+++ b/graf3d/gl/src/TGLCamera.cxx
@@ -359,13 +359,13 @@ TGLRect TGLCamera::ViewportRect(const TGLBoundingBox & box,
    if (face) {
       vertexCount = box.FaceVertices(*face).size();
    } else {
-      vertexCount = box.Vertices().size();
+      vertexCount = box.NumVertices();
    }
 
    for (UInt_t i = 0; i < vertexCount; i++)
    {
-      const TGLVertex3 & vertex = face ? box.Vertices().at(box.FaceVertices(*face).at(i)) :
-                                      box.Vertices().at(i);
+      const TGLVertex3 & vertex = face ? box.Vertex(box.FaceVertices(*face).at(i)) :
+                                         box.Vertex(i);
 
       gluProject(vertex.X(), vertex.Y(), vertex.Z(),
                  fModVM.CArr(), fProjM.CArr(), fViewport.CArr(),
diff --git a/graf3d/gl/src/TGLScene.cxx b/graf3d/gl/src/TGLScene.cxx
index 65ee5271807..a898ccf6576 100644
--- a/graf3d/gl/src/TGLScene.cxx
+++ b/graf3d/gl/src/TGLScene.cxx
@@ -926,7 +926,7 @@ void TGLScene::CalcBoundingBox() const
          assert(kFALSE);
          continue;
       }
-      TGLBoundingBox box = physicalShape->BoundingBox();
+      const TGLBoundingBox& box = physicalShape->BoundingBox();
       if (physicalShapeIt == fPhysicalShapes.begin()) {
          xMin = box.XMin(); xMax = box.XMax();
          yMin = box.YMin(); yMax = box.YMax();
-- 
GitLab