Skip to content

Commit 5d1a30f

Browse files
authored
Reduce copying & memory allocation by writing directly into function argument instead of allocating & copying (#38846)
1 parent 7481f60 commit 5d1a30f

12 files changed

+117
-105
lines changed

src/app/SafeAttributePersistenceProvider.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class SafeAttributePersistenceProvider
7171
* Read an attribute of type intX, uintX or bool from non-volatile memory.
7272
*
7373
* @param [in] aPath the attribute path for the data being persisted.
74-
* @param [in,out] aValue where to place the data.
74+
* @param [out] aValue where to place the data.
7575
*
7676
* @retval CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if no stored value exists for the attribute
7777
*/
@@ -112,7 +112,7 @@ class SafeAttributePersistenceProvider
112112
* Read an attribute of type nullable intX, uintX from non-volatile memory.
113113
*
114114
* @param [in] aPath the attribute path for the data being persisted.
115-
* @param [in,out] aValue where to place the data.
115+
* @param [out] aValue where to place the data.
116116
*
117117
* @retval CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND if no stored value exists for the attribute
118118
*/

src/app/clusters/scenes-server/SceneTable.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include <app/clusters/scenes-server/ExtensionFieldSets.h>
2121
#include <app/storage/TableEntry.h>
2222
#include <lib/support/CHIPMemString.h>
23-
#include <lib/support/CommonIterator.h>
2423
#include <lib/support/IntrusiveList.h>
2524
#include <lib/support/PersistentData.h>
2625
#include <lib/support/Span.h>
@@ -233,7 +232,7 @@ class SceneTable
233232

234233
SceneTable & operator=(const SceneTable &) = delete;
235234

236-
virtual CHIP_ERROR Init(PersistentStorageDelegate * storage) = 0;
235+
virtual CHIP_ERROR Init(PersistentStorageDelegate & storage) = 0;
237236
virtual void Finish() = 0;
238237

239238
// Global scene count

src/app/clusters/scenes-server/SceneTableImpl.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ StorageKeyName Serializer::FabricEntryDataKey(FabricIndex fabric, EndpointId end
4848

4949
// Worst case tested: Add Scene Command with EFS using the default SerializeAdd Method. This yielded a serialized scene of 175 bytes
5050
// when using the OnOff, Level Control and Color Control as well as the maximal name length of 16 bytes. Putting 256 gives some
51-
// slack in case different clusters are used. Value obtained by using writer.GetLengthWritten at the end of the SceneTableData
51+
// slack in case different clusters are used. Value obtained by using writer.GetLengthWritten at the end of the Serializer
5252
// Serialize method.
5353
template <>
5454
constexpr size_t Serializer::kEntryMaxBytes()
@@ -83,7 +83,7 @@ namespace {
8383
/// kGroupId: Tag for GroupID if the Scene is a Group Scene
8484
/// kSceneId: Tag for the scene ID. Together with kGroupId, forms the SceneStorageId
8585
/// kName: Tag for the name of the scene
86-
/// kTransitionTime: Tag for the transition time of the scene in miliseconds
86+
/// kTransitionTime: Tag for the transition time of the scene in milliseconds
8787
enum class TagScene : uint8_t
8888
{
8989
kGroupId = static_cast<uint8_t>(TagEntry::kFabricTableFirstSpecializationReservedTag),
@@ -93,13 +93,12 @@ enum class TagScene : uint8_t
9393
};
9494
} // namespace
9595

96-
using SceneTableData = TableEntryData<SceneStorageId, SceneData, Serializer::kEntryMaxBytes()>;
9796
using FabricSceneData =
9897
FabricEntryData<SceneStorageId, SceneData, Serializer::kEntryMaxBytes(), Serializer::kFabricMaxBytes(), kMaxScenesPerFabric>;
9998

10099
template class chip::app::Storage::FabricTableImpl<SceneTableBase::SceneStorageId, SceneTableBase::SceneData, kIteratorsMax>;
101100

102-
CHIP_ERROR DefaultSceneTableImpl::Init(PersistentStorageDelegate * storage)
101+
CHIP_ERROR DefaultSceneTableImpl::Init(PersistentStorageDelegate & storage)
103102
{
104103
return FabricTableImpl::Init(storage);
105104
}
@@ -156,7 +155,6 @@ CHIP_ERROR DefaultSceneTableImpl::GetAllSceneIdsInGroup(FabricIndex fabric_index
156155
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
157156

158157
FabricSceneData fabric(mEndpointId, fabric_index, mMaxPerFabric, mMaxPerEndpoint);
159-
SceneTableData scene(mEndpointId, fabric_index);
160158

161159
uint8_t scene_count = 0;
162160
CHIP_ERROR err = fabric.Load(this->mStorage);
@@ -190,7 +188,6 @@ CHIP_ERROR DefaultSceneTableImpl::DeleteAllScenesInGroup(FabricIndex fabric_inde
190188
VerifyOrReturnError(IsInitialized(), CHIP_ERROR_INTERNAL);
191189

192190
FabricSceneData fabric(mEndpointId, fabric_index, mMaxPerFabric, mMaxPerEndpoint);
193-
SceneTableData scene(mEndpointId, fabric_index);
194191

195192
CHIP_ERROR err = fabric.Load(this->mStorage);
196193
VerifyOrReturnValue(CHIP_ERROR_NOT_FOUND != err, CHIP_NO_ERROR);

src/app/clusters/scenes-server/SceneTableImpl.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <app/util/attribute-storage.h>
2424
#include <app/util/config.h>
2525
#include <lib/core/DataModelTypes.h>
26-
#include <lib/support/CommonIterator.h>
2726
#include <lib/support/PersistentData.h>
2827
#include <lib/support/Pool.h>
2928

@@ -61,7 +60,7 @@ class DefaultSceneTableImpl
6160
DefaultSceneTableImpl() : Super(kMaxScenesPerFabric, kMaxScenesPerEndpoint) {}
6261
~DefaultSceneTableImpl() { Finish(); };
6362

64-
CHIP_ERROR Init(PersistentStorageDelegate * storage) override;
63+
CHIP_ERROR Init(PersistentStorageDelegate & storage) override;
6564
void Finish() override;
6665

6766
// Scene count

src/app/clusters/scenes-server/scenes-server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ CHIP_ERROR ScenesServer::Init()
333333
mGroupProvider = Credentials::GetGroupDataProvider();
334334

335335
SceneTable * sceneTable = scenes::GetSceneTableImpl();
336-
ReturnErrorOnFailure(sceneTable->Init(&Server::GetInstance().GetPersistentStorage()));
336+
ReturnErrorOnFailure(sceneTable->Init(Server::GetInstance().GetPersistentStorage()));
337337
ReturnErrorOnFailure(Server::GetInstance().GetFabricTable().AddFabricDelegate(&gFabricDelegate));
338338

339339
mIsInitialized = true;

src/app/icd/server/ICDMonitoringTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ enum class Fields : uint8_t
3030
kClientType = 5,
3131
};
3232

33-
CHIP_ERROR ICDMonitoringEntry::UpdateKey(StorageKeyName & skey)
33+
CHIP_ERROR ICDMonitoringEntry::UpdateKey(StorageKeyName & skey) const
3434
{
3535
VerifyOrReturnError(kUndefinedFabricIndex != this->fabricIndex, CHIP_ERROR_INVALID_FABRIC_INDEX);
3636
skey = DefaultStorageKeyAllocator::ICDManagementTableEntry(this->fabricIndex, index);

src/app/icd/server/ICDMonitoringTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ struct ICDMonitoringEntry : public PersistentData<kICDMonitoringBufferSize>
6868
this->symmetricKeystore = keyStore;
6969
}
7070

71-
CHIP_ERROR UpdateKey(StorageKeyName & key) override;
71+
CHIP_ERROR UpdateKey(StorageKeyName & key) const override;
7272
CHIP_ERROR Serialize(TLV::TLVWriter & writer) const override;
7373
CHIP_ERROR Deserialize(TLV::TLVReader & reader) override;
7474
void Clear() override;

src/app/storage/FabricTableImpl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
#pragma once
1919

20+
#include <app/storage/TableEntry.h>
21+
#include <lib/support/CommonIterator.h>
22+
#include <lib/support/PersistentData.h>
2023
#include <lib/support/TypeTraits.h>
2124

2225
namespace chip {
@@ -41,7 +44,7 @@ class FabricTableImpl
4144
public:
4245
virtual ~FabricTableImpl() { Finish(); };
4346

44-
CHIP_ERROR Init(PersistentStorageDelegate * storage);
47+
CHIP_ERROR Init(PersistentStorageDelegate & storage);
4548
void Finish();
4649

4750
// Entry count

0 commit comments

Comments
 (0)