提交 455090bb 编写于 作者: S storypku 提交者: Liu Jiaming

cyber: code optimization for transport

1) simplified identity implementation
2) remove WIN32 related preprocessing
上级 edf5b87f
......@@ -15,6 +15,7 @@
*****************************************************************************/
#include "cyber/transport/common/endpoint.h"
#include "cyber/common/global_data.h"
namespace apollo {
......
......@@ -21,6 +21,7 @@
#include <string>
#include "cyber/proto/role_attributes.pb.h"
#include "cyber/transport/common/identity.h"
namespace apollo {
......
......@@ -44,6 +44,9 @@ TEST(EndpointTest, construction) {
EXPECT_EQ(erole.host_name(), "123");
EXPECT_EQ(erole.process_id(), 54321);
EXPECT_NE(erole.id(), e0.id().HashValue());
auto id = std::string(e0.id().data(), ID_SIZE);
EXPECT_NE(std::string("endpoint"), id);
}
}
......
......@@ -24,42 +24,40 @@ namespace apollo {
namespace cyber {
namespace transport {
Identity::Identity(bool need_generate) : hash_value_(0), hash_value_str_("") {
memset(data_, 0, ID_SIZE);
Identity::Identity(bool need_generate) : hash_value_(0) {
std::memset(data_, 0, ID_SIZE);
if (need_generate) {
uuid_t uuid;
uuid_generate(uuid);
memcpy(data_, uuid, ID_SIZE);
std::memcpy(data_, uuid, ID_SIZE);
Update();
}
}
Identity::Identity(const Identity& another) {
memcpy(data_, another.data(), ID_SIZE);
hash_value_ = another.hash_value_;
hash_value_str_ = another.hash_value_str_;
Identity::Identity(const Identity& rhs) {
std::memcpy(data_, rhs.data_, ID_SIZE);
hash_value_ = rhs.hash_value_;
}
Identity::~Identity() {}
Identity& Identity::operator=(const Identity& another) {
if (this != &another) {
memcpy(data_, another.data(), ID_SIZE);
hash_value_ = another.hash_value_;
hash_value_str_ = another.hash_value_str_;
Identity& Identity::operator=(const Identity& rhs) {
if (this != &rhs) {
std::memcpy(data_, rhs.data_, ID_SIZE);
hash_value_ = rhs.hash_value_;
}
return *this;
}
bool Identity::operator==(const Identity& another) const {
return memcmp(data_, another.data(), ID_SIZE) == 0;
bool Identity::operator==(const Identity& rhs) const {
return std::memcmp(data_, rhs.data_, ID_SIZE) == 0;
}
bool Identity::operator!=(const Identity& another) const {
return memcmp(data_, another.data(), ID_SIZE) != 0;
bool Identity::operator!=(const Identity& rhs) const {
return std::memcmp(data_, rhs.data_, ID_SIZE) != 0;
}
const std::string& Identity::ToString() const { return hash_value_str_; }
std::string Identity::ToString() const { return std::to_string(hash_value_); }
size_t Identity::Length() const { return ID_SIZE; }
......@@ -67,7 +65,6 @@ uint64_t Identity::HashValue() const { return hash_value_; }
void Identity::Update() {
hash_value_ = common::Hash(std::string(data_, ID_SIZE));
hash_value_str_ = std::to_string(hash_value_);
}
} // namespace transport
......
......@@ -25,7 +25,7 @@ namespace apollo {
namespace cyber {
namespace transport {
const uint8_t ID_SIZE = 8;
constexpr uint8_t ID_SIZE = 8;
class Identity {
public:
......@@ -37,18 +37,16 @@ class Identity {
bool operator==(const Identity& another) const;
bool operator!=(const Identity& another) const;
const std::string& ToString() const;
std::string ToString() const;
size_t Length() const;
uint64_t HashValue() const;
// getter and setter
const char* data() const { return data_; }
void set_data(const char* data) {
if (data == nullptr) {
return;
}
memset(data_, 0, sizeof(data_));
memcpy(data_, data, sizeof(data_));
std::memcpy(data_, data, sizeof(data_));
Update();
}
......@@ -57,7 +55,6 @@ class Identity {
char data_[ID_SIZE];
uint64_t hash_value_;
std::string hash_value_str_;
};
} // namespace transport
......
......@@ -26,14 +26,34 @@ TEST(IdentityTest, testConstructFalse) {
Identity it(false);
EXPECT_EQ(it.HashValue(), static_cast<uint64_t>(0));
EXPECT_EQ(it.ToString(), "");
EXPECT_EQ(it.ToString(), "0");
}
TEST(IdentityTest, testConstructTrue) {
Identity it(true);
EXPECT_NE(it.HashValue(), static_cast<uint64_t>(0));
EXPECT_NE(it.ToString(), "");
EXPECT_NE(it.ToString(), "0");
}
TEST(IdentityTest, testIdentityEqual) {
Identity id1;
Identity id2;
Identity id3;
EXPECT_NE(id1, id2);
EXPECT_NE(id2, id3);
EXPECT_NE(id1, id3);
EXPECT_NE(id1.HashValue(), id3.HashValue());
id2 = id2;
EXPECT_NE(id2, id3);
id2 = id3;
EXPECT_EQ(id2, id3);
Identity id4(id1);
EXPECT_EQ(id1, id4);
EXPECT_EQ(id1.ToString(), id4.ToString());
EXPECT_EQ(id1.HashValue(), id4.HashValue());
}
TEST(IdentityTest, testOperatorEqual) {
......
......@@ -16,7 +16,7 @@
#include "cyber/transport/message/message_info.h"
#include <arpa/inet.h>
#include <cstring>
#include "cyber/common/log.h"
......@@ -67,26 +67,23 @@ bool MessageInfo::SerializeTo(std::string* dst) const {
RETURN_VAL_IF_NULL(dst, false);
dst->assign(sender_id_.data(), ID_SIZE);
dst->append(reinterpret_cast<char*>(const_cast<uint64_t*>(&seq_num_)),
sizeof(seq_num_));
dst->append(reinterpret_cast<const char*>(&seq_num_), sizeof(seq_num_));
dst->append(spare_id_.data(), ID_SIZE);
return true;
}
bool MessageInfo::SerializeTo(char* dst, std::size_t len) const {
RETURN_VAL_IF_NULL(dst, false);
if (len < kSize) {
if (dst == nullptr || len < kSize) {
return false;
}
char* ptr = dst;
memcpy(ptr, sender_id_.data(), ID_SIZE);
std::memcpy(ptr, sender_id_.data(), ID_SIZE);
ptr += ID_SIZE;
memcpy(ptr, reinterpret_cast<char*>(const_cast<uint64_t*>(&seq_num_)),
sizeof(seq_num_));
std::memcpy(ptr, reinterpret_cast<const char*>(&seq_num_), sizeof(seq_num_));
ptr += sizeof(seq_num_);
memcpy(ptr, spare_id_.data(), ID_SIZE);
std::memcpy(ptr, spare_id_.data(), ID_SIZE);
return true;
}
......@@ -105,7 +102,7 @@ bool MessageInfo::DeserializeFrom(const char* src, std::size_t len) {
char* ptr = const_cast<char*>(src);
sender_id_.set_data(ptr);
ptr += ID_SIZE;
memcpy(reinterpret_cast<char*>(&seq_num_), ptr, sizeof(seq_num_));
std::memcpy(reinterpret_cast<char*>(&seq_num_), ptr, sizeof(seq_num_));
ptr += sizeof(seq_num_);
spare_id_.set_data(ptr);
......
......@@ -22,16 +22,8 @@
#include "cyber/transport/rtps/underlay_message.h"
#include "fastcdr/Cdr.h"
#include "fastcdr/exceptions/BadParamException.h"
#ifdef _WIN32
// Remove linker warning LNK4221 on Visual Studio
namespace {
char dummy;
}
#endif
namespace apollo {
namespace cyber {
namespace transport {
......
......@@ -24,35 +24,7 @@
#include <utility>
#include <vector>
#if defined(_WIN32)
#if defined(EPROSIMA_USER_DLL_EXPORT)
#define ePcyberima_user_DllExport __declspec(dllexport)
#else
#define ePcyberima_user_DllExport
#endif
#else
#define ePcyberima_user_DllExport
#endif
#if defined(_WIN32)
#if defined(EPROSIMA_USER_DLL_EXPORT)
#if defined(UnderlayMessage_SOURCE)
#define UnderlayMessage_DllAPI __declspec(dllexport)
#else
#define UnderlayMessage_DllAPI __declspec(dllimport)
#endif // UnderlayMessage_SOURCE
#else
#define UnderlayMessage_DllAPI
#endif
#else
#define UnderlayMessage_DllAPI
#endif // _WIN32
namespace eprosima {
namespace fastcdr {
class Cdr;
}
} // namespace eprosima
#include "fastcdr/Cdr.h"
namespace apollo {
namespace cyber {
......@@ -68,118 +40,105 @@ class UnderlayMessage {
/*!
* @brief Default constructor.
*/
ePcyberima_user_DllExport UnderlayMessage();
UnderlayMessage();
/*!
* @brief Default destructor.
*/
ePcyberima_user_DllExport ~UnderlayMessage();
~UnderlayMessage();
/*!
* @brief Copy constructor.
* @param x Reference to the object UnderlayMessage that will be copied.
*/
ePcyberima_user_DllExport UnderlayMessage(const UnderlayMessage& x);
UnderlayMessage(const UnderlayMessage& x);
/*!
* @brief Move constructor.
* @param x Reference to the object UnderlayMessage that will be copied.
*/
ePcyberima_user_DllExport UnderlayMessage(UnderlayMessage&& x);
UnderlayMessage(UnderlayMessage&& x);
/*!
* @brief Copy assignment.
* @param x Reference to the object UnderlayMessage that will be copied.
*/
ePcyberima_user_DllExport UnderlayMessage& operator=(
const UnderlayMessage& x);
UnderlayMessage& operator=(const UnderlayMessage& x);
/*!
* @brief Move assignment.
* @param x Reference to the object UnderlayMessage that will be copied.
*/
ePcyberima_user_DllExport UnderlayMessage& operator=(UnderlayMessage&& x);
UnderlayMessage& operator=(UnderlayMessage&& x);
/*!
* @brief This function sets a value in member timestamp
* @param _timestamp New value for member timestamp
*/
inline ePcyberima_user_DllExport void timestamp(int32_t _timestamp) {
m_timestamp = _timestamp;
}
inline void timestamp(int32_t _timestamp) { m_timestamp = _timestamp; }
/*!
* @brief This function returns the value of member timestamp
* @return Value of member timestamp
*/
inline ePcyberima_user_DllExport int32_t timestamp() const {
return m_timestamp;
}
inline int32_t timestamp() const { return m_timestamp; }
/*!
* @brief This function returns a reference to member timestamp
* @return Reference to member timestamp
*/
inline ePcyberima_user_DllExport int32_t& timestamp() { return m_timestamp; }
inline int32_t& timestamp() { return m_timestamp; }
/*!
* @brief This function sets a value in member seq
* @param _seq New value for member seq
*/
inline ePcyberima_user_DllExport void seq(int32_t _seq) { m_seq = _seq; }
inline void seq(int32_t _seq) { m_seq = _seq; }
/*!
* @brief This function returns the value of member seq
* @return Value of member seq
*/
inline ePcyberima_user_DllExport int32_t seq() const { return m_seq; }
inline int32_t seq() const { return m_seq; }
/*!
* @brief This function returns a reference to member seq
* @return Reference to member seq
*/
inline ePcyberima_user_DllExport int32_t& seq() { return m_seq; }
inline int32_t& seq() { return m_seq; }
/*!
* @brief This function copies the value in member data
* @param _data New value to be copied in member data
*/
inline ePcyberima_user_DllExport void data(const std::string& _data) {
m_data = _data;
}
inline void data(const std::string& _data) { m_data = _data; }
/*!
* @brief This function moves the value in member data
* @param _data New value to be moved in member data
*/
inline ePcyberima_user_DllExport void data(std::string&& _data) {
m_data = std::move(_data);
}
inline void data(std::string&& _data) { m_data = std::move(_data); }
/*!
* @brief This function returns a constant reference to member data
* @return Constant reference to member data
*/
inline ePcyberima_user_DllExport const std::string& data() const {
return m_data;
}
inline const std::string& data() const { return m_data; }
/*!
* @brief This function returns a reference to member data
* @return Reference to member data
*/
inline ePcyberima_user_DllExport std::string& data() { return m_data; }
inline std::string& data() { return m_data; }
/*!
* @brief This function copies the value in member datatype
* @param _datatype New value to be copied in member datatype
*/
inline ePcyberima_user_DllExport void datatype(const std::string& _datatype) {
m_datatype = _datatype;
}
inline void datatype(const std::string& _datatype) { m_datatype = _datatype; }
/*!
* @brief This function moves the value in member datatype
* @param _datatype New value to be moved in member datatype
*/
inline ePcyberima_user_DllExport void datatype(std::string&& _datatype) {
inline void datatype(std::string&& _datatype) {
m_datatype = std::move(_datatype);
}
......@@ -187,17 +146,13 @@ class UnderlayMessage {
* @brief This function returns a constant reference to member datatype
* @return Constant reference to member datatype
*/
inline ePcyberima_user_DllExport const std::string& datatype() const {
return m_datatype;
}
inline const std::string& datatype() const { return m_datatype; }
/*!
* @brief This function returns a reference to member datatype
* @return Reference to member datatype
*/
inline ePcyberima_user_DllExport std::string& datatype() {
return m_datatype;
}
inline std::string& datatype() { return m_datatype; }
/*!
* @brief This function returns the maximum serialized size of an object
......@@ -205,8 +160,7 @@ class UnderlayMessage {
* @param current_alignment Buffer alignment.
* @return Maximum serialized size.
*/
ePcyberima_user_DllExport static size_t getMaxCdrSerializedSize(
size_t current_alignment = 0);
static size_t getMaxCdrSerializedSize(size_t current_alignment = 0);
/*!
* @brief This function returns the serialized size of a data depending on the
......@@ -215,22 +169,20 @@ class UnderlayMessage {
* @param current_alignment Buffer alignment.
* @return Serialized size.
*/
ePcyberima_user_DllExport static size_t getCdrSerializedSize(
const UnderlayMessage& data, size_t current_alignment = 0);
static size_t getCdrSerializedSize(const UnderlayMessage& data,
size_t current_alignment = 0);
/*!
* @brief This function serializes an object using CDR serialization.
* @param cdr CDR serialization object.
*/
ePcyberima_user_DllExport void serialize(
eprosima::fastcdr::Cdr& cdr) const; // NOLINT
void serialize(eprosima::fastcdr::Cdr& cdr) const; // NOLINT
/*!
* @brief This function deserializes an object using CDR serialization.
* @param cdr CDR serialization object.
*/
ePcyberima_user_DllExport void deserialize(
eprosima::fastcdr::Cdr& cdr); // NOLINT
void deserialize(eprosima::fastcdr::Cdr& cdr); // NOLINT
/*!
* @brief This function returns the maximum serialized size of the Key of an
......@@ -239,21 +191,19 @@ class UnderlayMessage {
* @param current_alignment Buffer alignment.
* @return Maximum serialized size.
*/
ePcyberima_user_DllExport static size_t getKeyMaxCdrSerializedSize(
size_t current_alignment = 0);
static size_t getKeyMaxCdrSerializedSize(size_t current_alignment = 0);
/*!
* @brief This function tells you if the Key has been defined for this type
*/
ePcyberima_user_DllExport static bool isKeyDefined();
static bool isKeyDefined();
/*!
* @brief This function serializes the key members of an object using CDR
* serialization.
* @param cdr CDR serialization object.
*/
ePcyberima_user_DllExport void serializeKey(
eprosima::fastcdr::Cdr& cdr) const; // NOLINT
void serializeKey(eprosima::fastcdr::Cdr& cdr) const; // NOLINT
private:
int32_t m_timestamp;
......
......@@ -49,7 +49,7 @@ class Block {
void ReleaseWriteLock();
void ReleaseReadLock();
volatile std::atomic<int32_t> lock_num_ = {0};
std::atomic<int32_t> lock_num_ = {0};
uint64_t msg_size_;
uint64_t msg_info_size_;
......
"""Loads the fastrtps library"""
"""Load the fastrtps library"""
# Sanitize a dependency so that it works correctly from code that includes
# Apollo as a submodule.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册