Ver código fonte

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

Adam Ierymenko 5 anos atrás
pai
commit
ea2f95ed70
3 arquivos alterados com 36 adições e 10 exclusões
  1. 14 9
      core/Locator.cpp
  2. 2 0
      core/Locator.hpp
  3. 20 1
      core/SharedPtr.hpp

+ 14 - 9
core/Locator.cpp

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

+ 2 - 0
core/Locator.hpp

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

+ 20 - 1
core/SharedPtr.hpp

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