From: Patrick Donnelly Date: Wed, 16 Jan 2019 18:07:41 +0000 (-0800) Subject: include/encoding: eliminate smart_ptr temporaries X-Git-Tag: v14.1.0~334^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=1fad3435b76880ebdd6ef5bd971886a505520b63;p=ceph.git include/encoding: eliminate smart_ptr temporaries This is a small refactor including a conversion to the range-based for loop. Signed-off-by: Patrick Donnelly --- diff --git a/src/include/encoding.h b/src/include/encoding.h index 13375c2da7db9..3b991596828ba 100644 --- a/src/include/encoding.h +++ b/src/include/encoding.h @@ -681,8 +681,9 @@ inline void encode(const std::list, Alloc>& ls, { __u32 n = (__u32)(ls.size()); // c++11 std::list::size() is O(1) encode(n, bl); - for (auto p = ls.begin(); p != ls.end(); ++p) - encode(**p, bl); + for (const auto& ref : ls) { + encode(*ref, bl); + } } template inline void encode(const std::list, Alloc>& ls, @@ -690,8 +691,9 @@ inline void encode(const std::list, Alloc>& ls, { __u32 n = (__u32)(ls.size()); // c++11 std::list::size() is O(1) encode(n, bl); - for (auto p = ls.begin(); p != ls.end(); ++p) - encode(**p, bl, features); + for (const auto& ref : ls) { + encode(*ref, bl, features); + } } template inline void decode(std::list, Alloc>& ls, @@ -701,9 +703,9 @@ inline void decode(std::list, Alloc>& ls, decode(n, p); ls.clear(); while (n--) { - std::shared_ptr v(std::make_shared()); - decode(*v, p); - ls.push_back(v); + auto ref = std::make_shared(); + decode(*ref, p); + ls.emplace_back(std::move(ref)); } } @@ -917,11 +919,12 @@ inline void encode(const std::vector,Alloc>& v, { __u32 n = (__u32)(v.size()); encode(n, bl); - for (auto p = v.begin(); p != v.end(); ++p) - if (*p) - encode(**p, bl, features); + for (const auto& ref : v) { + if (ref) + encode(*ref, bl, features); else encode(T(), bl, features); + } } template inline void encode(const std::vector,Alloc>& v, @@ -929,11 +932,12 @@ inline void encode(const std::vector,Alloc>& v, { __u32 n = (__u32)(v.size()); encode(n, bl); - for (auto p = v.begin(); p != v.end(); ++p) - if (*p) - encode(**p, bl); + for (const auto& ref : v) { + if (ref) + encode(*ref, bl); else encode(T(), bl); + } } template inline void decode(std::vector,Alloc>& v, @@ -941,10 +945,12 @@ inline void decode(std::vector,Alloc>& v, { __u32 n; decode(n, p); - v.resize(n); - for (__u32 i=0; i(); - decode(*v[i], p); + v.clear(); + v.reserve(n); + while (n--) { + auto ref = std::make_shared(); + decode(*ref, p); + v.emplace_back(std::move(ref)); } }