Cache best paths determined by GetPathByRouter to reduce cpu usage

pull/1877/head
Jeff 2 years ago
parent 65ef0e4bfb
commit 38a157808e

@ -191,10 +191,11 @@ namespace llarp
lastBuild = 0s;
}
void Builder::Tick(llarp_time_t)
void
Builder::Tick(llarp_time_t now)
{
const auto now = llarp::time_now_ms();
PathSet::Tick(now);
now = llarp::time_now_ms();
m_router->pathBuildLimiter().Decay(now);
ExpirePaths(now, m_router);

@ -66,13 +66,31 @@ namespace llarp
PathSet::TickPaths(AbstractRouter* r)
{
const auto now = llarp::time_now_ms();
Lock_t l(m_PathsMutex);
Lock_t l{m_PathsMutex};
for (auto& item : m_Paths)
{
item.second->Tick(now, r);
}
}
void PathSet::Tick(llarp_time_t)
{
std::unordered_set<RouterID> endpoints;
for (auto& item : m_Paths)
{
endpoints.emplace(item.second->Endpoint());
}
m_PathCache.clear();
for (const auto& ep : endpoints)
{
if (auto path = GetPathByRouter(ep))
{
m_PathCache[ep] = path->weak_from_this();
}
}
}
void
PathSet::ExpirePaths(llarp_time_t now, AbstractRouter* router)
{
@ -150,6 +168,13 @@ namespace llarp
{
Lock_t l(m_PathsMutex);
Path_ptr chosen = nullptr;
if (roles == ePathRoleAny)
{
if (auto itr = m_PathCache.find(id); itr != m_PathCache.end())
{
return itr->second.lock();
}
}
auto itr = m_Paths.begin();
while (itr != m_Paths.end())
{

@ -130,7 +130,7 @@ namespace llarp
/// tick owned paths
virtual void
Tick(llarp_time_t now) = 0;
Tick(llarp_time_t now);
/// count the number of paths that will exist at this timestamp in future
size_t
@ -320,6 +320,9 @@ namespace llarp
using PathMap_t = std::unordered_map<std::pair<RouterID, PathID_t>, Path_ptr>;
mutable Mtx_t m_PathsMutex;
PathMap_t m_Paths;
private:
std::unordered_map<RouterID, std::weak_ptr<path::Path>> m_PathCache;
};
} // namespace path

Loading…
Cancel
Save