Optimizations, make Locator deserialize the same regardless of serialized field order.

This commit is contained in:
Adam Ierymenko 2020-07-31 14:05:54 -07:00
parent fc39894541
commit ea2f95ed70
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
3 changed files with 36 additions and 10 deletions

View file

@ -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" {

View file

@ -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;

View file

@ -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