mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-05 03:53:44 +02:00
Integrate Hashtable into Multicaster, where @mwarning found heaviest std::map() overhead.
This commit is contained in:
parent
3947807b1f
commit
b11ffc9635
5 changed files with 119 additions and 59 deletions
|
@ -19,7 +19,6 @@
|
||||||
*
|
*
|
||||||
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||||
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef ZT_HASHTABLE_HPP
|
#ifndef ZT_HASHTABLE_HPP
|
||||||
|
@ -39,9 +38,6 @@ namespace ZeroTier {
|
||||||
* This is not a drop-in replacement for STL containers, and has several
|
* This is not a drop-in replacement for STL containers, and has several
|
||||||
* limitations. It's designed to be small and fast for use in the
|
* limitations. It's designed to be small and fast for use in the
|
||||||
* ZeroTier core.
|
* ZeroTier core.
|
||||||
*
|
|
||||||
* Pairs of values can also be used as a key. In this case the first and
|
|
||||||
* second element of the pair's hash codes are XORed.
|
|
||||||
*/
|
*/
|
||||||
template<typename K,typename V>
|
template<typename K,typename V>
|
||||||
class Hashtable
|
class Hashtable
|
||||||
|
@ -49,12 +45,11 @@ class Hashtable
|
||||||
private:
|
private:
|
||||||
struct _Bucket
|
struct _Bucket
|
||||||
{
|
{
|
||||||
_Bucket(const K &k,const V &v) :
|
_Bucket(const K &k,const V &v) : k(k),v(v) {}
|
||||||
k(k),
|
_Bucket(const K &k) : k(k),v() {}
|
||||||
v(v) {}
|
|
||||||
_Bucket *next;
|
|
||||||
K k;
|
K k;
|
||||||
V v;
|
V v;
|
||||||
|
_Bucket *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -188,35 +183,56 @@ public:
|
||||||
/**
|
/**
|
||||||
* @param k Key
|
* @param k Key
|
||||||
* @param v Value
|
* @param v Value
|
||||||
|
* @return Reference to value in table
|
||||||
*/
|
*/
|
||||||
inline void set(const K &k,const V &v)
|
inline V &set(const K &k,const V &v)
|
||||||
{
|
{
|
||||||
if (_s >= _bc) {
|
|
||||||
const unsigned long nc = _bc * 2;
|
|
||||||
_Bucket **nt = reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * nc));
|
|
||||||
if (nt) {
|
|
||||||
for(unsigned long i=0;i<nc;++i)
|
|
||||||
nt[i] = (_Bucket *)0;
|
|
||||||
for(unsigned long i=0;i<_bc;++i) {
|
|
||||||
_Bucket *b = _t[i];
|
|
||||||
while (b) {
|
|
||||||
_Bucket *const nb = b->next;
|
|
||||||
const unsigned long nidx = _hc(b->k) % nc;
|
|
||||||
b->next = nt[nidx];
|
|
||||||
nt[nidx] = b;
|
|
||||||
b = nb;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
::free(_t);
|
|
||||||
_t = nt;
|
|
||||||
_bc = nc;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const unsigned long bidx = _hc(k) % _bc;
|
const unsigned long bidx = _hc(k) % _bc;
|
||||||
_Bucket *const b = new _Bucket(k,v);
|
|
||||||
|
_Bucket *b = _t[bidx];
|
||||||
|
while (b) {
|
||||||
|
if (b->k == k) {
|
||||||
|
b->v = v;
|
||||||
|
return b->v;
|
||||||
|
}
|
||||||
|
b = b->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_s >= _bc)
|
||||||
|
_grow();
|
||||||
|
|
||||||
|
b = new _Bucket(k,v);
|
||||||
b->next = _t[bidx];
|
b->next = _t[bidx];
|
||||||
_t[bidx] = b;
|
_t[bidx] = b;
|
||||||
++_s;
|
++_s;
|
||||||
|
|
||||||
|
return b->v;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param k Key
|
||||||
|
* @return Value, possibly newly created
|
||||||
|
*/
|
||||||
|
inline V &operator[](const K &k)
|
||||||
|
{
|
||||||
|
const unsigned long bidx = _hc(k) % _bc;
|
||||||
|
|
||||||
|
_Bucket *b = _t[bidx];
|
||||||
|
while (b) {
|
||||||
|
if (b->k == k)
|
||||||
|
return b->v;
|
||||||
|
b = b->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_s >= _bc)
|
||||||
|
_grow();
|
||||||
|
|
||||||
|
b = new _Bucket(k);
|
||||||
|
b->next = _t[bidx];
|
||||||
|
_t[bidx] = b;
|
||||||
|
++_s;
|
||||||
|
|
||||||
|
return b->v;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -242,6 +258,29 @@ private:
|
||||||
return (unsigned long)((i ^ (i >> 32)) * 2654435761ULL);
|
return (unsigned long)((i ^ (i >> 32)) * 2654435761ULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void _grow()
|
||||||
|
{
|
||||||
|
const unsigned long nc = _bc * 2;
|
||||||
|
_Bucket **nt = reinterpret_cast<_Bucket **>(::malloc(sizeof(_Bucket *) * nc));
|
||||||
|
if (nt) {
|
||||||
|
for(unsigned long i=0;i<nc;++i)
|
||||||
|
nt[i] = (_Bucket *)0;
|
||||||
|
for(unsigned long i=0;i<_bc;++i) {
|
||||||
|
_Bucket *b = _t[i];
|
||||||
|
while (b) {
|
||||||
|
_Bucket *const nb = b->next;
|
||||||
|
const unsigned long nidx = _hc(b->k) % nc;
|
||||||
|
b->next = nt[nidx];
|
||||||
|
nt[nidx] = b;
|
||||||
|
b = nb;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
::free(_t);
|
||||||
|
_t = nt;
|
||||||
|
_bc = nc;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_Bucket **_t;
|
_Bucket **_t;
|
||||||
unsigned long _bc;
|
unsigned long _bc;
|
||||||
unsigned long _s;
|
unsigned long _s;
|
||||||
|
|
|
@ -242,6 +242,8 @@ public:
|
||||||
*/
|
*/
|
||||||
inline unsigned int size() const throw() { return 6; }
|
inline unsigned int size() const throw() { return 6; }
|
||||||
|
|
||||||
|
inline unsigned long hashCode() const throw() { return (unsigned long)_m; }
|
||||||
|
|
||||||
inline MAC &operator=(const MAC &m)
|
inline MAC &operator=(const MAC &m)
|
||||||
throw()
|
throw()
|
||||||
{
|
{
|
||||||
|
|
|
@ -141,6 +141,8 @@ public:
|
||||||
*/
|
*/
|
||||||
inline uint32_t adi() const throw() { return _adi; }
|
inline uint32_t adi() const throw() { return _adi; }
|
||||||
|
|
||||||
|
inline unsigned long hashCode() const throw() { return (_mac.hashCode() ^ (unsigned long)_adi); }
|
||||||
|
|
||||||
inline bool operator==(const MulticastGroup &g) const throw() { return ((_mac == g._mac)&&(_adi == g._adi)); }
|
inline bool operator==(const MulticastGroup &g) const throw() { return ((_mac == g._mac)&&(_adi == g._adi)); }
|
||||||
inline bool operator!=(const MulticastGroup &g) const throw() { return ((_mac != g._mac)||(_adi != g._adi)); }
|
inline bool operator!=(const MulticastGroup &g) const throw() { return ((_mac != g._mac)||(_adi != g._adi)); }
|
||||||
inline bool operator<(const MulticastGroup &g) const throw()
|
inline bool operator<(const MulticastGroup &g) const throw()
|
||||||
|
|
|
@ -41,7 +41,9 @@
|
||||||
namespace ZeroTier {
|
namespace ZeroTier {
|
||||||
|
|
||||||
Multicaster::Multicaster(const RuntimeEnvironment *renv) :
|
Multicaster::Multicaster(const RuntimeEnvironment *renv) :
|
||||||
RR(renv)
|
RR(renv),
|
||||||
|
_groups(1024),
|
||||||
|
_groups_m()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +56,7 @@ void Multicaster::addMultiple(uint64_t now,uint64_t nwid,const MulticastGroup &m
|
||||||
const unsigned char *p = (const unsigned char *)addresses;
|
const unsigned char *p = (const unsigned char *)addresses;
|
||||||
const unsigned char *e = p + (5 * count);
|
const unsigned char *e = p + (5 * count);
|
||||||
Mutex::Lock _l(_groups_m);
|
Mutex::Lock _l(_groups_m);
|
||||||
MulticastGroupStatus &gs = _groups[std::pair<uint64_t,MulticastGroup>(nwid,mg)];
|
MulticastGroupStatus &gs = _groups[Multicaster::Key(nwid,mg)];
|
||||||
while (p != e) {
|
while (p != e) {
|
||||||
_add(now,nwid,mg,gs,Address(p,5));
|
_add(now,nwid,mg,gs,Address(p,5));
|
||||||
p += 5;
|
p += 5;
|
||||||
|
@ -64,11 +66,11 @@ void Multicaster::addMultiple(uint64_t now,uint64_t nwid,const MulticastGroup &m
|
||||||
void Multicaster::remove(uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
void Multicaster::remove(uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
||||||
{
|
{
|
||||||
Mutex::Lock _l(_groups_m);
|
Mutex::Lock _l(_groups_m);
|
||||||
std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus >::iterator g(_groups.find(std::pair<uint64_t,MulticastGroup>(nwid,mg)));
|
MulticastGroupStatus *s = _groups.get(Multicaster::Key(nwid,mg));
|
||||||
if (g != _groups.end()) {
|
if (s) {
|
||||||
for(std::vector<MulticastGroupMember>::iterator m(g->second.members.begin());m!=g->second.members.end();++m) {
|
for(std::vector<MulticastGroupMember>::iterator m(s->members.begin());m!=s->members.end();++m) {
|
||||||
if (m->address == member) {
|
if (m->address == member) {
|
||||||
g->second.members.erase(m);
|
s->members.erase(m);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,18 +104,18 @@ unsigned int Multicaster::gather(const Address &queryingPeer,uint64_t nwid,const
|
||||||
|
|
||||||
Mutex::Lock _l(_groups_m);
|
Mutex::Lock _l(_groups_m);
|
||||||
|
|
||||||
std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus >::const_iterator gs(_groups.find(std::pair<uint64_t,MulticastGroup>(nwid,mg)));
|
const MulticastGroupStatus *s = _groups.get(Multicaster::Key(nwid,mg));
|
||||||
if ((gs != _groups.end())&&(!gs->second.members.empty())) {
|
if ((s)&&(!s->members.empty())) {
|
||||||
totalKnown += (unsigned int)gs->second.members.size();
|
totalKnown += (unsigned int)s->members.size();
|
||||||
|
|
||||||
// Members are returned in random order so that repeated gather queries
|
// Members are returned in random order so that repeated gather queries
|
||||||
// will return different subsets of a large multicast group.
|
// will return different subsets of a large multicast group.
|
||||||
k = 0;
|
k = 0;
|
||||||
while ((added < limit)&&(k < gs->second.members.size())&&((appendTo.size() + ZT_ADDRESS_LENGTH) <= ZT_UDP_DEFAULT_PAYLOAD_MTU)) {
|
while ((added < limit)&&(k < s->members.size())&&((appendTo.size() + ZT_ADDRESS_LENGTH) <= ZT_UDP_DEFAULT_PAYLOAD_MTU)) {
|
||||||
rptr = (unsigned int)RR->node->prng();
|
rptr = (unsigned int)RR->node->prng();
|
||||||
|
|
||||||
restart_member_scan:
|
restart_member_scan:
|
||||||
a = gs->second.members[rptr % (unsigned int)gs->second.members.size()].address.toInt();
|
a = s->members[rptr % (unsigned int)s->members.size()].address.toInt();
|
||||||
for(i=0;i<k;++i) {
|
for(i=0;i<k;++i) {
|
||||||
if (picked[i] == a) {
|
if (picked[i] == a) {
|
||||||
++rptr;
|
++rptr;
|
||||||
|
@ -146,10 +148,10 @@ std::vector<Address> Multicaster::getMembers(uint64_t nwid,const MulticastGroup
|
||||||
{
|
{
|
||||||
std::vector<Address> ls;
|
std::vector<Address> ls;
|
||||||
Mutex::Lock _l(_groups_m);
|
Mutex::Lock _l(_groups_m);
|
||||||
std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus >::const_iterator gs(_groups.find(std::pair<uint64_t,MulticastGroup>(nwid,mg)));
|
const MulticastGroupStatus *s = _groups.get(Multicaster::Key(nwid,mg));
|
||||||
if (gs == _groups.end())
|
if (!s)
|
||||||
return ls;
|
return ls;
|
||||||
for(std::vector<MulticastGroupMember>::const_reverse_iterator m(gs->second.members.rbegin());m!=gs->second.members.rend();++m) {
|
for(std::vector<MulticastGroupMember>::const_reverse_iterator m(s->members.rbegin());m!=s->members.rend();++m) {
|
||||||
ls.push_back(m->address);
|
ls.push_back(m->address);
|
||||||
if (ls.size() >= limit)
|
if (ls.size() >= limit)
|
||||||
break;
|
break;
|
||||||
|
@ -173,7 +175,7 @@ void Multicaster::send(
|
||||||
unsigned long *indexes = idxbuf;
|
unsigned long *indexes = idxbuf;
|
||||||
|
|
||||||
Mutex::Lock _l(_groups_m);
|
Mutex::Lock _l(_groups_m);
|
||||||
MulticastGroupStatus &gs = _groups[std::pair<uint64_t,MulticastGroup>(nwid,mg)];
|
MulticastGroupStatus &gs = _groups[Multicaster::Key(nwid,mg)];
|
||||||
|
|
||||||
if (!gs.members.empty()) {
|
if (!gs.members.empty()) {
|
||||||
// Allocate a memory buffer if group is monstrous
|
// Allocate a memory buffer if group is monstrous
|
||||||
|
@ -291,18 +293,22 @@ void Multicaster::send(
|
||||||
void Multicaster::clean(uint64_t now)
|
void Multicaster::clean(uint64_t now)
|
||||||
{
|
{
|
||||||
Mutex::Lock _l(_groups_m);
|
Mutex::Lock _l(_groups_m);
|
||||||
for(std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus >::iterator mm(_groups.begin());mm!=_groups.end();) {
|
|
||||||
for(std::list<OutboundMulticast>::iterator tx(mm->second.txQueue.begin());tx!=mm->second.txQueue.end();) {
|
Multicaster::Key *k = (Multicaster::Key *)0;
|
||||||
|
MulticastGroupStatus *s = (MulticastGroupStatus *)0;
|
||||||
|
Hashtable<Multicaster::Key,MulticastGroupStatus>::Iterator mm(_groups);
|
||||||
|
while (mm.next(k,s)) {
|
||||||
|
for(std::list<OutboundMulticast>::iterator tx(s->txQueue.begin());tx!=s->txQueue.end();) {
|
||||||
if ((tx->expired(now))||(tx->atLimit()))
|
if ((tx->expired(now))||(tx->atLimit()))
|
||||||
mm->second.txQueue.erase(tx++);
|
s->txQueue.erase(tx++);
|
||||||
else ++tx;
|
else ++tx;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long count = 0;
|
unsigned long count = 0;
|
||||||
{
|
{
|
||||||
std::vector<MulticastGroupMember>::iterator reader(mm->second.members.begin());
|
std::vector<MulticastGroupMember>::iterator reader(s->members.begin());
|
||||||
std::vector<MulticastGroupMember>::iterator writer(reader);
|
std::vector<MulticastGroupMember>::iterator writer(reader);
|
||||||
while (reader != mm->second.members.end()) {
|
while (reader != s->members.end()) {
|
||||||
if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
|
if ((now - reader->timestamp) < ZT_MULTICAST_LIKE_EXPIRE) {
|
||||||
*writer = *reader;
|
*writer = *reader;
|
||||||
++writer;
|
++writer;
|
||||||
|
@ -313,13 +319,11 @@ void Multicaster::clean(uint64_t now)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count) {
|
if (count) {
|
||||||
mm->second.members.resize(count);
|
s->members.resize(count);
|
||||||
++mm;
|
} else if (s->txQueue.empty()) {
|
||||||
} else if (mm->second.txQueue.empty()) {
|
_groups.erase(*k);
|
||||||
_groups.erase(mm++);
|
|
||||||
} else {
|
} else {
|
||||||
mm->second.members.clear();
|
s->members.clear();
|
||||||
++mm;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
#include <list>
|
#include <list>
|
||||||
|
|
||||||
#include "Constants.hpp"
|
#include "Constants.hpp"
|
||||||
|
#include "Hashtable.hpp"
|
||||||
#include "Address.hpp"
|
#include "Address.hpp"
|
||||||
#include "MAC.hpp"
|
#include "MAC.hpp"
|
||||||
#include "MulticastGroup.hpp"
|
#include "MulticastGroup.hpp"
|
||||||
|
@ -56,6 +57,18 @@ class Packet;
|
||||||
class Multicaster : NonCopyable
|
class Multicaster : NonCopyable
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
struct Key
|
||||||
|
{
|
||||||
|
Key() : nwid(0),mg() {}
|
||||||
|
Key(uint64_t n,const MulticastGroup &g) : nwid(n),mg(g) {}
|
||||||
|
|
||||||
|
uint64_t nwid;
|
||||||
|
MulticastGroup mg;
|
||||||
|
|
||||||
|
inline bool operator==(const Key &k) const throw() { return ((nwid == k.nwid)&&(mg == k.mg)); }
|
||||||
|
inline unsigned long hashCode() const throw() { return (mg.hashCode() ^ (unsigned long)(nwid ^ (nwid >> 32))); }
|
||||||
|
};
|
||||||
|
|
||||||
struct MulticastGroupMember
|
struct MulticastGroupMember
|
||||||
{
|
{
|
||||||
MulticastGroupMember() {}
|
MulticastGroupMember() {}
|
||||||
|
@ -89,7 +102,7 @@ public:
|
||||||
inline void add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
inline void add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,const Address &member)
|
||||||
{
|
{
|
||||||
Mutex::Lock _l(_groups_m);
|
Mutex::Lock _l(_groups_m);
|
||||||
_add(now,nwid,mg,_groups[std::pair<uint64_t,MulticastGroup>(nwid,mg)],member);
|
_add(now,nwid,mg,_groups[Multicaster::Key(nwid,mg)],member);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -181,7 +194,7 @@ private:
|
||||||
void _add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member);
|
void _add(uint64_t now,uint64_t nwid,const MulticastGroup &mg,MulticastGroupStatus &gs,const Address &member);
|
||||||
|
|
||||||
const RuntimeEnvironment *RR;
|
const RuntimeEnvironment *RR;
|
||||||
std::map< std::pair<uint64_t,MulticastGroup>,MulticastGroupStatus > _groups;
|
Hashtable<Multicaster::Key,MulticastGroupStatus> _groups;
|
||||||
Mutex _groups_m;
|
Mutex _groups_m;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue