mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-07 04:53:44 +02:00
Optimizations, make Locator deserialize the same regardless of serialized field order.
This commit is contained in:
parent
fc39894541
commit
ea2f95ed70
3 changed files with 36 additions and 10 deletions
|
@ -46,20 +46,12 @@ bool Locator::add(const Endpoint &ep, const SharedPtr< const EndpointAttributes
|
|||
return false;
|
||||
}
|
||||
|
||||
struct p_SortByEndpoint
|
||||
{
|
||||
// There can't be more than one of the same endpoint, so only need to sort
|
||||
// by endpoint.
|
||||
ZT_INLINE bool operator()(const std::pair< Endpoint, SharedPtr< const Locator::EndpointAttributes > > &a,const std::pair< Endpoint, SharedPtr< const Locator::EndpointAttributes > > &b) const noexcept
|
||||
{ return a.first < b.first; }
|
||||
};
|
||||
|
||||
bool Locator::sign(const int64_t ts, const Identity &id) noexcept
|
||||
{
|
||||
m_ts = ts;
|
||||
m_signer = id.fingerprint();
|
||||
|
||||
std::sort(m_endpoints.begin(), m_endpoints.end(), p_SortByEndpoint());
|
||||
m_sortEndpoints();
|
||||
|
||||
uint8_t signdata[ZT_LOCATOR_MARSHAL_SIZE_MAX];
|
||||
const unsigned int signlen = marshal(signdata, true);
|
||||
|
@ -195,9 +187,22 @@ int Locator::unmarshal(const uint8_t *data, const int len) noexcept
|
|||
if (unlikely(p > len))
|
||||
return -1;
|
||||
|
||||
m_sortEndpoints();
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
struct p_SortByEndpoint
|
||||
{
|
||||
// There can't be more than one of the same endpoint, so only need to sort
|
||||
// by endpoint.
|
||||
ZT_INLINE bool operator()(const std::pair< Endpoint, SharedPtr< const Locator::EndpointAttributes > > &a,const std::pair< Endpoint, SharedPtr< const Locator::EndpointAttributes > > &b) const noexcept
|
||||
{ return a.first < b.first; }
|
||||
};
|
||||
|
||||
void Locator::m_sortEndpoints() noexcept
|
||||
{ std::sort(m_endpoints.begin(), m_endpoints.end(), p_SortByEndpoint()); }
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
extern "C" {
|
||||
|
|
|
@ -227,6 +227,8 @@ public:
|
|||
{ return !(*this == l); }
|
||||
|
||||
private:
|
||||
void m_sortEndpoints() noexcept;
|
||||
|
||||
int64_t m_ts;
|
||||
Fingerprint m_signer;
|
||||
Vector< std::pair< Endpoint, SharedPtr< const EndpointAttributes > > > m_endpoints;
|
||||
|
|
|
@ -213,10 +213,29 @@ private:
|
|||
|
||||
} // namespace ZeroTier
|
||||
|
||||
// Augment std::swap to speed up some operations with SharedPtr.
|
||||
namespace std {
|
||||
|
||||
template< typename T >
|
||||
ZT_INLINE void swap(ZeroTier::SharedPtr< T > &a, ZeroTier::SharedPtr< T > &b) noexcept
|
||||
{ a.swap(b); }
|
||||
}
|
||||
|
||||
template< typename T >
|
||||
constexpr bool is_swappable(ZeroTier::SharedPtr< T > &a) noexcept
|
||||
{ return true; }
|
||||
|
||||
template< typename T >
|
||||
constexpr bool is_swappable_with(ZeroTier::SharedPtr< T > &a, ZeroTier::SharedPtr< T > &b) noexcept
|
||||
{ return true; }
|
||||
|
||||
template< typename T >
|
||||
constexpr bool is_nothrow_swappable(ZeroTier::SharedPtr< T > &a) noexcept
|
||||
{ return true; }
|
||||
|
||||
template< typename T >
|
||||
constexpr bool is_nothrow_swappable_with(ZeroTier::SharedPtr< T > &a, ZeroTier::SharedPtr< T > &b) noexcept
|
||||
{ return true; }
|
||||
|
||||
} // namespace std
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue