diff options
author | Sven Gothel <[email protected]> | 2020-12-13 21:55:40 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-12-13 21:55:40 +0100 |
commit | cafd8d1f3689135eae36854a9cca4def690045fd (patch) | |
tree | 31c97ca30139f870b97e20bbcf148982b41bbcd7 /api/direct_bt/GATTHandler.hpp | |
parent | 93d956905bcddcc226164296051a9ec20daca722 (diff) |
smart_ptr-1: Handle AttPDUMsg instances via std::unique_ptr instead of std::shared_ptr for more efficancy; Use std::make_unique where possible
smart_ptr change series, commit #1
For high frequent objects, it is desired to reduce resources and overhead as much as possible.
unique_ptr has zero overhead, while shared_ptr own a little storage for the reference counter.
Using unique_ptr forces us to move the instance from the receiving reading thread to the ringbuffer
and moving it out later. Hence the changes in jau::ringbuffer earlier.
Using std::make_unique is not really required, as unique_ptr has no resource overhead,
hence 'unique_ptr( new Something() )' is only one allocation.
However, this smart_ptr change series will review all smart pointer use cases
and in case a shared_ptr is required like DBTDevice of GATTCharacteristic elements of GATTServices etc,
we shall use std::make_shared to fold two allocations into one.
Note: 'shared_ptr( new Something() )' implies two allocations.
Current implementation state allows these optimizations,
as the use cases for these data types are well understood.
Earlier it was not feasible to undertake constraining efforts,
as it was not clear whether other actors like to share a resource, like a uuid_t for example.
Diffstat (limited to 'api/direct_bt/GATTHandler.hpp')
-rw-r--r-- | api/direct_bt/GATTHandler.hpp | 4 |
1 files changed, 2 insertions, 2 deletions
diff --git a/api/direct_bt/GATTHandler.hpp b/api/direct_bt/GATTHandler.hpp index 447143e0..d477b649 100644 --- a/api/direct_bt/GATTHandler.hpp +++ b/api/direct_bt/GATTHandler.hpp @@ -163,7 +163,7 @@ namespace direct_bt { jau::sc_atomic_bool is_connected; // reflects state jau::relaxed_atomic_bool has_ioerror; // reflects state - jau::ringbuffer<std::shared_ptr<const AttPDUMsg>, nullptr, jau::nsize_t> attPDURing; + jau::ringbuffer<std::unique_ptr<const AttPDUMsg>, nullptr, jau::nsize_t> attPDURing; jau::sc_atomic_bool l2capReaderShallStop; std::mutex mtx_l2capReaderLifecycle; @@ -221,7 +221,7 @@ namespace direct_bt { * @param timeout milliseconds to wait for a reply * @return a valid reply, never nullptrs */ - std::shared_ptr<const AttPDUMsg> sendWithReply(const AttPDUMsg & msg, const int timeout); + std::unique_ptr<const AttPDUMsg> sendWithReply(const AttPDUMsg & msg, const int timeout); /** * BT Core Spec v5.2: Vol 3, Part G GATT: 3.4.2 MTU Exchange |