From 0b1a36885914bce024aec63a236058f83809e1db Mon Sep 17 00:00:00 2001 From: Subhransu Mohanty Date: Mon, 30 Nov 2020 18:04:20 +0900 Subject: [PATCH] Avoid Heap allocation in UniformMap. 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 | 3 +- .../data-providers/uniform-map-data-provider.h | 5 +- dali/internal/render/renderers/render-renderer.cpp | 18 +++--- .../update/common/property-owner-messages.h | 4 +- dali/internal/update/common/property-owner.cpp | 4 +- dali/internal/update/common/property-owner.h | 2 +- dali/internal/update/common/uniform-map.cpp | 67 ++++++++-------------- dali/internal/update/common/uniform-map.h | 4 +- dali/internal/update/nodes/node.cpp | 4 +- dali/internal/update/nodes/node.h | 2 +- .../update/rendering/scene-graph-renderer.cpp | 21 +++++-- 11 files changed, 61 insertions(+), 73 deletions(-) mode change 100755 => 100644 dali/internal/update/nodes/node.cpp diff --git a/dali/internal/event/common/object-impl.cpp b/dali/internal/event/common/object-impl.cpp index b117b09..5e16c11 100644 --- a/dali/internal/event/common/object-impl.cpp +++ b/dali/internal/event/common/object-impl.cpp @@ -803,8 +803,7 @@ void Object::AddUniformMapping(Property::Index propertyIndex, std::string unifor { const SceneGraph::PropertyOwner& sceneObject = GetSceneObject(); - OwnerPointer 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(GetEventThreadServices()), sceneObject, map ); } diff --git a/dali/internal/render/data-providers/uniform-map-data-provider.h b/dali/internal/render/data-providers/uniform-map-data-provider.h index 9160908..68ae5e7 100644 --- a/dali/internal/render/data-providers/uniform-map-data-provider.h +++ b/dali/internal/render/data-providers/uniform-map-data-provider.h @@ -16,8 +16,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include #include +#include +#include namespace Dali { @@ -28,7 +29,7 @@ namespace SceneGraph class UniformMap; class UniformPropertyMapping; -using CollectedUniformMap = Dali::Vector; +using CollectedUniformMap = Dali::Vector; /** * This class maps uniform names to property value pointers. diff --git a/dali/internal/render/renderers/render-renderer.cpp b/dali/internal/render/renderers/render-renderer.cpp index cb316e8..518c1de 100644 --- a/dali/internal/render/renderers/render-renderer.cpp +++ b/dali/internal/render/renderers/render-renderer.cpp @@ -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; ipropertyPtr; + 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) diff --git a/dali/internal/update/common/property-owner-messages.h b/dali/internal/update/common/property-owner-messages.h index ebfb567..60d9531 100644 --- a/dali/internal/update/common/property-owner-messages.h +++ b/dali/internal/update/common/property-owner-messages.h @@ -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 >; + using LocalType = MessageValue1; // Reserve some memory inside the message queue uint32_t* slot = eventThreadServices.ReserveMessageSlot( sizeof( LocalType ) ); diff --git a/dali/internal/update/common/property-owner.cpp b/dali/internal/update/common/property-owner.cpp index 7398de3..bd23fe2 100644 --- a/dali/internal/update/common/property-owner.cpp +++ b/dali/internal/update/common/property-owner.cpp @@ -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 ) diff --git a/dali/internal/update/common/property-owner.h b/dali/internal/update/common/property-owner.h index 677b81c..2e6c7da 100644 --- a/dali/internal/update/common/property-owner.h +++ b/dali/internal/update/common/property-owner.h @@ -201,7 +201,7 @@ public: /** * @copydoc UniformMap::Add */ - virtual void AddUniformMapping( OwnerPointer< UniformPropertyMapping >& map ); + virtual void AddUniformMapping(const UniformPropertyMapping& map); /** * @copydoc UniformMap::Remove diff --git a/dali/internal/update/common/uniform-map.cpp b/dali/internal/update/common/uniform-map.cpp index 3f72540..6f60e38 100644 --- a/dali/internal/update/common/uniform-map.cpp +++ b/dali/internal/update/common/uniform-map.cpp @@ -14,6 +14,8 @@ * limitations under the License. */ +#include + // CLASS HEADER #include @@ -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 diff --git a/dali/internal/update/common/uniform-map.h b/dali/internal/update/common/uniform-map.h index c243a51..4978e7c 100644 --- a/dali/internal/update/common/uniform-map.h +++ b/dali/internal/update/common/uniform-map.h @@ -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; + using UniformMapContainer = Dali::Vector; using UniformMapIter = UniformMapContainer::Iterator; using Observers = Dali::Vector; using ObserversIter = Observers::Iterator; diff --git a/dali/internal/update/nodes/node.cpp b/dali/internal/update/nodes/node.cpp old mode 100755 new mode 100644 index 6cbf519..86b4864 --- a/dali/internal/update/nodes/node.cpp +++ b/dali/internal/update/nodes/node.cpp @@ -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& map ) override; + void AddUniformMapping(const UniformPropertyMapping& map) override; /** * @copydoc UniformMap::Remove diff --git a/dali/internal/update/rendering/scene-graph-renderer.cpp b/dali/internal/update/rendering/scene-graph-renderer.cpp index e8d9cd9..b9680f5 100644 --- a/dali/internal/update/rendering/scene-graph-renderer.cpp +++ b/dali/internal/update/rendering/scene-graph-renderer.cpp @@ -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 ) -- 2.7.4