Reduce size of _nodeMutex critical sections

This commit is contained in:
Clement 2019-03-01 11:23:46 -08:00
parent 991bcfe2b2
commit 135c7b667e
3 changed files with 43 additions and 43 deletions

View file

@ -239,6 +239,10 @@ void AvatarMixer::start() {
}, &lockWait, &nodeTransform, &functor); }, &lockWait, &nodeTransform, &functor);
auto end = usecTimestampNow(); auto end = usecTimestampNow();
_processQueuedAvatarDataPacketsElapsedTime += (end - start); _processQueuedAvatarDataPacketsElapsedTime += (end - start);
_broadcastAvatarDataLockWait += lockWait;
_broadcastAvatarDataNodeTransform += nodeTransform;
_broadcastAvatarDataNodeFunctor += functor;
} }
// process pending display names... this doesn't currently run on multiple threads, because it // process pending display names... this doesn't currently run on multiple threads, because it
@ -256,6 +260,10 @@ void AvatarMixer::start() {
}, &lockWait, &nodeTransform, &functor); }, &lockWait, &nodeTransform, &functor);
auto end = usecTimestampNow(); auto end = usecTimestampNow();
_displayNameManagementElapsedTime += (end - start); _displayNameManagementElapsedTime += (end - start);
_broadcastAvatarDataLockWait += lockWait;
_broadcastAvatarDataNodeTransform += nodeTransform;
_broadcastAvatarDataNodeFunctor += functor;
} }
// this is where we need to put the real work... // this is where we need to put the real work...

View file

@ -558,25 +558,23 @@ SharedNodePointer LimitedNodeList::nodeWithLocalID(Node::LocalID localID) const
} }
void LimitedNodeList::eraseAllNodes() { void LimitedNodeList::eraseAllNodes() {
QSet<SharedNodePointer> killedNodes; std::vector<SharedNodePointer> killedNodes;
{ {
// iterate the current nodes - grab them so we can emit that they are dying // iterate the current nodes - grab them so we can emit that they are dying
// and then remove them from the hash // and then remove them from the hash
QWriteLocker writeLocker(&_nodeMutex); QWriteLocker writeLocker(&_nodeMutex);
_localIDMap.clear();
if (_nodeHash.size() > 0) { if (_nodeHash.size() > 0) {
qCDebug(networking) << "LimitedNodeList::eraseAllNodes() removing all nodes from NodeList."; qCDebug(networking) << "LimitedNodeList::eraseAllNodes() removing all nodes from NodeList.";
auto it = _nodeHash.begin(); killedNodes.reserve(_nodeHash.size());
for (auto& pair : _nodeHash) {
while (it != _nodeHash.end()) { killedNodes.push_back(pair.second);
killedNodes.insert(it->second);
it = _nodeHash.unsafe_erase(it);
} }
} }
_localIDMap.clear();
_nodeHash.clear();
} }
foreach(const SharedNodePointer& killedNode, killedNodes) { foreach(const SharedNodePointer& killedNode, killedNodes) {
@ -593,18 +591,13 @@ void LimitedNodeList::reset() {
} }
bool LimitedNodeList::killNodeWithUUID(const QUuid& nodeUUID, ConnectionID newConnectionID) { bool LimitedNodeList::killNodeWithUUID(const QUuid& nodeUUID, ConnectionID newConnectionID) {
QReadLocker readLocker(&_nodeMutex); auto matchingNode = nodeWithUUID(nodeUUID);
NodeHash::iterator it = _nodeHash.find(nodeUUID);
if (it != _nodeHash.end()) {
SharedNodePointer matchingNode = it->second;
readLocker.unlock();
if (matchingNode) {
{ {
QWriteLocker writeLocker(&_nodeMutex); QWriteLocker writeLocker(&_nodeMutex);
_localIDMap.unsafe_erase(matchingNode->getLocalID()); _localIDMap.unsafe_erase(matchingNode->getLocalID());
_nodeHash.unsafe_erase(it); _nodeHash.unsafe_erase(matchingNode->getUUID());
} }
handleNodeKill(matchingNode, newConnectionID); handleNodeKill(matchingNode, newConnectionID);
@ -645,30 +638,26 @@ SharedNodePointer LimitedNodeList::addOrUpdateNode(const QUuid& uuid, NodeType_t
const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket, const HifiSockAddr& publicSocket, const HifiSockAddr& localSocket,
Node::LocalID localID, bool isReplicated, bool isUpstream, Node::LocalID localID, bool isReplicated, bool isUpstream,
const QUuid& connectionSecret, const NodePermissions& permissions) { const QUuid& connectionSecret, const NodePermissions& permissions) {
{ auto matchingNode = nodeWithUUID(uuid);
QReadLocker readLocker(&_nodeMutex); if (matchingNode) {
NodeHash::const_iterator it = _nodeHash.find(uuid); matchingNode->setPublicSocket(publicSocket);
matchingNode->setLocalSocket(localSocket);
matchingNode->setPermissions(permissions);
matchingNode->setConnectionSecret(connectionSecret);
matchingNode->setIsReplicated(isReplicated);
matchingNode->setIsUpstream(isUpstream || NodeType::isUpstream(nodeType));
matchingNode->setLocalID(localID);
if (it != _nodeHash.end()) { return matchingNode;
SharedNodePointer& matchingNode = it->second;
matchingNode->setPublicSocket(publicSocket);
matchingNode->setLocalSocket(localSocket);
matchingNode->setPermissions(permissions);
matchingNode->setConnectionSecret(connectionSecret);
matchingNode->setIsReplicated(isReplicated);
matchingNode->setIsUpstream(isUpstream || NodeType::isUpstream(nodeType));
matchingNode->setLocalID(localID);
return matchingNode;
}
} }
auto removeOldNode = [&](auto node) { auto removeOldNode = [&](auto node) {
if (node) { if (node) {
QWriteLocker writeLocker(&_nodeMutex); {
_localIDMap.unsafe_erase(node->getLocalID()); QWriteLocker writeLocker(&_nodeMutex);
_nodeHash.unsafe_erase(node->getUUID()); _localIDMap.unsafe_erase(node->getLocalID());
_nodeHash.unsafe_erase(node->getUUID());
}
handleNodeKill(node); handleNodeKill(node);
} }
}; };

View file

@ -205,7 +205,10 @@ public:
int* lockWaitOut = nullptr, int* lockWaitOut = nullptr,
int* nodeTransformOut = nullptr, int* nodeTransformOut = nullptr,
int* functorOut = nullptr) { int* functorOut = nullptr) {
auto start = usecTimestampNow(); quint64 start, endTransform, endFunctor;
start = usecTimestampNow();
std::vector<SharedNodePointer> nodes;
{ {
QReadLocker readLock(&_nodeMutex); QReadLocker readLock(&_nodeMutex);
auto endLock = usecTimestampNow(); auto endLock = usecTimestampNow();
@ -216,21 +219,21 @@ public:
// Size of _nodeHash could change at any time, // Size of _nodeHash could change at any time,
// so reserve enough memory for the current size // so reserve enough memory for the current size
// and then back insert all the nodes found // and then back insert all the nodes found
std::vector<SharedNodePointer> nodes;
nodes.reserve(_nodeHash.size()); nodes.reserve(_nodeHash.size());
std::transform(_nodeHash.cbegin(), _nodeHash.cend(), std::back_inserter(nodes), [&](const NodeHash::value_type& it) { std::transform(_nodeHash.cbegin(), _nodeHash.cend(), std::back_inserter(nodes), [&](const NodeHash::value_type& it) {
return it.second; return it.second;
}); });
auto endTransform = usecTimestampNow();
endTransform = usecTimestampNow();
if (nodeTransformOut) { if (nodeTransformOut) {
*nodeTransformOut = (endTransform - endLock); *nodeTransformOut = (endTransform - endLock);
} }
}
functor(nodes.cbegin(), nodes.cend()); functor(nodes.cbegin(), nodes.cend());
auto endFunctor = usecTimestampNow(); endFunctor = usecTimestampNow();
if (functorOut) { if (functorOut) {
*functorOut = (endFunctor - endTransform); *functorOut = (endFunctor - endTransform);
}
} }
} }