Avoid Heap allocation in UniformMap. 90/248590/3
authorSubhransu Mohanty <sub.mohanty@samsung.com>
Mon, 30 Nov 2020 09:04:20 +0000 (18:04 +0900)
committerSubhransu Mohanty <sub.mohanty@samsung.com>
Tue, 1 Dec 2020 05:24:32 +0000 (14:24 +0900)
With the use of ConstString we no need to heap allocate UniformPropertyMapping.
So just keep a Dali::Vector instead of OwnerContainer.

Change-Id: I2dff9828a121f7b6b245c306e0f689f5d2073021

dali/internal/event/common/object-impl.cpp
dali/internal/render/data-providers/uniform-map-data-provider.h
dali/internal/render/renderers/render-renderer.cpp
dali/internal/update/common/property-owner-messages.h
dali/internal/update/common/property-owner.cpp
dali/internal/update/common/property-owner.h
dali/internal/update/common/uniform-map.cpp
dali/internal/update/common/uniform-map.h
dali/internal/update/nodes/node.cpp [changed mode: 0755->0644]
dali/internal/update/nodes/node.h
dali/internal/update/rendering/scene-graph-renderer.cpp

index b117b09..5e16c11 100644 (file)
@@ -803,8 +803,7 @@ void Object::AddUniformMapping(Property::Index propertyIndex, std::string unifor
   {
     const SceneGraph::PropertyOwner& sceneObject = GetSceneObject();
 
-    OwnerPointer<SceneGraph::UniformPropertyMapping> map =
-      new SceneGraph::UniformPropertyMapping(ConstString(uniformName), propertyPtr);
+    SceneGraph::UniformPropertyMapping map(ConstString(uniformName), propertyPtr);
     // Message takes ownership of Uniform map (and will delete it after copy)
     AddUniformMapMessage( const_cast<EventThreadServices&>(GetEventThreadServices()), sceneObject, map );
   }
index 9160908..68ae5e7 100644 (file)
@@ -16,8 +16,9 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include <dali/public-api/common/dali-vector.h>
 #include <dali/internal/common/buffer-index.h>
+#include <dali/internal/update/common/uniform-map.h>
+#include <dali/public-api/common/dali-vector.h>
 
 namespace Dali
 {
@@ -28,7 +29,7 @@ namespace SceneGraph
 class UniformMap;
 class UniformPropertyMapping;
 
-using CollectedUniformMap = Dali::Vector<const UniformPropertyMapping*>;
+using CollectedUniformMap = Dali::Vector<UniformPropertyMapping>;
 
 /**
  * This class maps uniform names to property value pointers.
index cb316e8..518c1de 100644 (file)
@@ -255,19 +255,19 @@ void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataP
     uint32_t mapIndex = 0;
     for(; mapIndex < uniformMap.Count() ; ++mapIndex )
     {
-      mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
-      mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
+      mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex].propertyPtr;
+      mUniformIndexMap[mapIndex].uniformIndex  = program.RegisterUniform(uniformMap[mapIndex].uniformName);
     }
 
     for( uint32_t nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
     {
-      uint32_t uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
+      uint32_t uniformIndex = program.RegisterUniform(uniformMapNode[nodeMapIndex].uniformName);
       bool found(false);
       for( uint32_t i = 0; i<uniformMap.Count(); ++i )
       {
         if( mUniformIndexMap[i].uniformIndex == uniformIndex )
         {
-          mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
+          mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
           found = true;
           break;
         }
@@ -275,7 +275,7 @@ void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataP
 
       if( !found )
       {
-        mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
+        mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex].propertyPtr;
         mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
         ++mapIndex;
       }
@@ -729,16 +729,16 @@ bool Renderer::Updated(BufferIndex bufferIndex, const SceneGraph::NodeDataProvid
 
   uint64_t hash = 0xc70f6907UL;
   const SceneGraph::CollectedUniformMap& uniformMapNode = node->GetUniformMap( bufferIndex );
-  for (const auto* uniformProperty : uniformMapNode)
+  for(const auto& uniformProperty : uniformMapNode)
   {
-    hash = uniformProperty->propertyPtr->Hash(bufferIndex, hash);
+    hash = uniformProperty.propertyPtr->Hash(bufferIndex, hash);
   }
 
   const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
   const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
-  for (const auto* uniformProperty : uniformMap)
+  for(const auto& uniformProperty : uniformMap)
   {
-    hash = uniformProperty->propertyPtr->Hash(bufferIndex, hash);
+    hash = uniformProperty.propertyPtr->Hash(bufferIndex, hash);
   }
 
   if (mUniformsHash != hash)
index ebfb567..60d9531 100644 (file)
@@ -254,9 +254,9 @@ inline void RemoveConstraintMessage( EventThreadServices& eventThreadServices, c
   new (slot) LocalType( &owner, &PropertyOwner::RemoveConstraint, &constraint );
 }
 
-inline void AddUniformMapMessage( EventThreadServices& eventThreadServices, const PropertyOwner& owner, OwnerPointer< UniformPropertyMapping >& map )
+inline void AddUniformMapMessage(EventThreadServices& eventThreadServices, const PropertyOwner& owner, UniformPropertyMapping map)
 {
-  using LocalType = MessageValue1<PropertyOwner, OwnerPointer<UniformPropertyMapping> >;
+  using LocalType = MessageValue1<PropertyOwner, UniformPropertyMapping>;
 
   // Reserve some memory inside the message queue
   uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) );
index 7398de3..bd23fe2 100644 (file)
@@ -153,9 +153,9 @@ PropertyOwner::PropertyOwner()
 {
 }
 
-void PropertyOwner::AddUniformMapping( OwnerPointer< UniformPropertyMapping >& map )
+void PropertyOwner::AddUniformMapping(const UniformPropertyMapping& map)
 {
-  mUniformMaps.Add( map.Release() );
+  mUniformMaps.Add(map);
 }
 
 void PropertyOwner::RemoveUniformMapping( const ConstString& uniformName )
index 677b81c..2e6c7da 100644 (file)
@@ -201,7 +201,7 @@ public:
   /**
    * @copydoc UniformMap::Add
    */
-  virtual void AddUniformMapping( OwnerPointer< UniformPropertyMapping >& map );
+  virtual void AddUniformMapping(const UniformPropertyMapping& map);
 
   /**
    * @copydoc UniformMap::Remove
index 3f72540..6f60e38 100644 (file)
@@ -14,6 +14,8 @@
  * limitations under the License.
  */
 
+#include <algorithm>
+
 // CLASS HEADER
 #include <dali/internal/update/common/uniform-map.h>
 
@@ -62,27 +64,20 @@ void UniformMap::MappingChanged()
   }
 }
 
-void UniformMap::Add( UniformPropertyMapping* newMap )
+void UniformMap::Add(UniformPropertyMapping newMap)
 {
-  bool found = false;
+  auto iter = std::find_if(mUniformMaps.Begin(),
+                           mUniformMaps.End(),
+                           [&](auto& element) { return element.uniformName == newMap.uniformName; });
 
-  for( UniformMapIter iter = mUniformMaps.Begin() ;
-       iter != mUniformMaps.End() ;
-       ++iter )
+  if(iter != mUniformMaps.End())
   {
-    UniformPropertyMapping* map = *iter;
-    if( map->uniformName == newMap->uniformName )
-    {
-      found = true;
-      // Mapping already exists - update it.
-      map->propertyPtr = newMap->propertyPtr;
-      break;
-    }
+    // Mapping already exists - update it.
+    (*iter).propertyPtr = newMap.propertyPtr;
   }
-
-  if( found == false )
+  else
   {
-    // Take ownership of the new map
+    // add the new map.
     mUniformMaps.PushBack(newMap);
   }
 
@@ -91,40 +86,24 @@ void UniformMap::Add( UniformPropertyMapping* newMap )
 
 void UniformMap::Remove( ConstString uniformName )
 {
-  bool found=false;
-
-  for( UniformMapIter iter = mUniformMaps.Begin() ;
-       iter != mUniformMaps.End() ;
-       ++iter )
-  {
-    UniformPropertyMapping* map = *iter;
-    if( map->uniformName == uniformName )
-    {
-      mUniformMaps.Erase( iter );
-      found = true;
-      break;
-    }
-  }
+  auto iter = std::find_if(mUniformMaps.Begin(),
+                           mUniformMaps.End(),
+                           [&](auto& element) { return element.uniformName == uniformName; });
 
-  if( found )
+  if(iter != mUniformMaps.End())
   {
+    mUniformMaps.Erase(iter);
     MappingChanged();
   }
 }
 
-const PropertyInputImpl* UniformMap::Find( ConstString uniformName )
+const PropertyInputImpl* UniformMap::Find(ConstString uniformName)
 {
-  for( UniformMapIter iter = mUniformMaps.Begin() ;
-       iter != mUniformMaps.End() ;
-       ++iter )
-  {
-    UniformPropertyMapping* map = *iter;
-    if( map->uniformName == uniformName )
-    {
-      return map->propertyPtr;
-    }
-  }
-  return nullptr;
+  auto iter = std::find_if(mUniformMaps.Begin(),
+                           mUniformMaps.End(),
+                           [&](auto& element) { return element.uniformName == uniformName; });
+
+  return (iter != mUniformMaps.End()) ? (*iter).propertyPtr : nullptr;
 }
 
 UniformMap::SizeType UniformMap::Count() const
@@ -134,7 +113,7 @@ UniformMap::SizeType UniformMap::Count() const
 
 const UniformPropertyMapping& UniformMap::operator[]( UniformMap::SizeType index ) const
 {
-  return *mUniformMaps[index];
+  return mUniformMaps[index];
 }
 
 } // SceneGraph
index c243a51..4978e7c 100644 (file)
@@ -99,7 +99,7 @@ public:
   /**
    * Add a map to the mappings table.
    */
-  void Add( UniformPropertyMapping* map );
+  void Add(UniformPropertyMapping map);
 
   /**
    * Remove a map from the mappings table
@@ -132,7 +132,7 @@ private:
   void MappingChanged();
 
 private:
-  using UniformMapContainer = OwnerContainer<UniformPropertyMapping*>;
+  using UniformMapContainer = Dali::Vector<UniformPropertyMapping>;
   using UniformMapIter      = UniformMapContainer::Iterator;
   using Observers           = Dali::Vector<Observer*>;
   using ObserversIter       = Observers::Iterator;
old mode 100755 (executable)
new mode 100644 (file)
index 6cbf519..86b4864
@@ -176,7 +176,7 @@ void Node::SetRoot(bool isRoot)
   mIsRoot = isRoot;
 }
 
-void Node::AddUniformMapping( OwnerPointer< UniformPropertyMapping >& map )
+void Node::AddUniformMapping(const UniformPropertyMapping& map)
 {
   PropertyOwner::AddUniformMapping( map );
   mRegenerateUniformMap = 2;
@@ -204,7 +204,7 @@ void Node::PrepareRender( BufferIndex bufferIndex )
 
       for( UniformMap::SizeType i = 0, count=mUniformMaps.Count(); i<count; ++i )
       {
-        localMap.PushBack( &mUniformMaps[i] );
+        localMap.PushBack(mUniformMaps[i]);
       }
     }
     else if( mRegenerateUniformMap == 1 )
index 6377a5f..77bd5ac 100644 (file)
@@ -798,7 +798,7 @@ public:
   /**
    * @copydoc UniformMap::Add
    */
-  void AddUniformMapping( OwnerPointer< UniformPropertyMapping >& map ) override;
+  void AddUniformMapping(const UniformPropertyMapping& map) override;
 
   /**
    * @copydoc UniformMap::Remove
index e8d9cd9..b9680f5 100644 (file)
@@ -64,8 +64,8 @@ void AddMappings( CollectedUniformMap& localMap, const UniformMap& uniformMap )
 
     for( CollectedUniformMap::Iterator iter = localMap.Begin() ; iter != localMap.End() ; ++iter )
     {
-      const UniformPropertyMapping* map = (*iter);
-      if( map->uniformName == uniformMap[i].uniformName )
+      const UniformPropertyMapping& map = (*iter);
+      if(map.uniformName == uniformMap[i].uniformName)
       {
         found = true;
         break;
@@ -73,8 +73,7 @@ void AddMappings( CollectedUniformMap& localMap, const UniformMap& uniformMap )
     }
     if( !found )
     {
-      // it's a new mapping. Add raw ptr to temporary list
-      newUniformMappings.PushBack( &uniformMap[i] );
+      newUniformMappings.PushBack(uniformMap[i]);
     }
   }
 
@@ -87,7 +86,7 @@ void AddMappings( CollectedUniformMap& localMap, const UniformMap& uniformMap )
          iter != end ;
          ++iter )
     {
-      const UniformPropertyMapping* map = (*iter);
+      const UniformPropertyMapping& map = (*iter);
       localMap.PushBack( map );
     }
   }
@@ -188,9 +187,19 @@ bool Renderer::PrepareRender( BufferIndex updateBufferIndex )
     if( mRegenerateUniformMap == REGENERATE_UNIFORM_MAP)
     {
       CollectedUniformMap& localMap = mCollectedUniformMap[ updateBufferIndex ];
-      localMap.Resize(0);
+      localMap.Clear();
 
       const UniformMap& rendererUniformMap = PropertyOwner::GetUniformMap();
+
+      auto size = rendererUniformMap.Count();
+
+      if(mShader)
+      {
+        size += mShader->GetUniformMap().Count();
+      }
+
+      localMap.Reserve(size);
+
       AddMappings( localMap, rendererUniformMap );
 
       if( mShader )