forked from opentibiabr/canary
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve: dispatcher (opentibiabr#1732)
This PR consists of: 1- removing redundant code 2- task expiration Log 3- priority_queue to btree_multiset 4- flat_hash_set to unordered_set on hasTraceableContext 5- scheduled tasks with batch erase and insert 6- some other adjustments. **Benchmark:** ![image](https://github.com/opentibiabr/canary/assets/2267386/0f3dd190-2d19-40f6-b61e-5f8bd34dd2d1) Note; Unfortunately priority_queue does not have batch insertion. <details> ```c++ static constexpr auto max_stress = 999999; const auto time_now = OTSYS_TIME(); std::multiset<std::shared_ptr<Task>, Task::Compare> multiset; phmap::btree_multiset<std::shared_ptr<Task>, Task::Compare> btreemultiset; std::priority_queue<std::shared_ptr<Task>, std::deque<std::shared_ptr<Task>>, Task::Compare> priority; std::vector<std::shared_ptr<Task>> tasks; for (int i = 0; ++i < max_stress;) { tasks.emplace_back(std::make_shared<Task>([] {}, "priority", time_now + (100 + (rand() % 2000)))); } const auto &benchmark = [&](std::string_view type, auto set, uint64_t stress) { stress = std::min<uint64_t>(stress, max_stress); Benchmark bm; for (int i = 0; ++i <= 3;) { set.insert(tasks.begin(), tasks.begin() + stress); while (!set.empty()) { auto it = set.begin(); // top: auto s = *it; // pop: set.erase(it); } } g_logger().info("{}({}) - {}ms", type, stress, bm.duration()); }; const auto &benchmarkPriority = [&](auto priority, uint64_t stress) { stress = std::min<uint64_t>(stress, max_stress); Benchmark bm; for (int i = 0; ++i <= 3;) { for (int d = 0; ++d <= stress;) { priority.emplace(tasks[d]); } while (!priority.empty()) { auto t = priority.top(); priority.pop(); } } g_logger().info("{}({}) - {}ms", "priority", stress, bm.duration()); }; benchmark("multiset", multiset, 999); benchmark("multiset", multiset, 99999); benchmark("multiset", multiset, 999999); benchmark("btreemultiset", btreemultiset, 999); benchmark("btreemultiset", btreemultiset, 99999); benchmark("btreemultiset", btreemultiset, 999999); benchmarkPriority(priority, 999); benchmarkPriority(priority, 99999); benchmarkPriority(priority, 999999); ``` </details> ![image](https://github.com/opentibiabr/canary/assets/2267386/39fd3f5e-629b-4151-8315-9e07e5cd41b1) <details> ```c++ static constexpr auto max_stress = 999999; const std::vector < std::string_view> tasks ({ "Creature::checkCreatureWalk", "Decay::checkDecay", "Dispatcher::asyncEvent", "Game::checkCreatureAttack", "Game::checkCreatures", "Game::checkImbuements", "Game::checkLight", "Game::createFiendishMonsters", "Game::createInfluencedMonsters", "Game::updateCreatureWalk", "Game::updateForgeableMonsters", "GlobalEvents::think", "LuaEnvironment::executeTimerEvent", "Modules::executeOnRecvbyte", "OutputMessagePool::sendAll", "ProtocolGame::addGameTask", "ProtocolGame::parsePacketFromDispatcher", "Raids::checkRaids", "SpawnMonster::checkSpawnMonster", "SpawnMonster::scheduleSpawn", "SpawnNpc::checkSpawnNpc", "Webhook::run", "sendRecvMessageCallback", }); const std::set<std::string_view> set(tasks.begin(), tasks.end()); const std::unordered_set<std::string_view> unordered_set(tasks.begin(), tasks.end()); const phmap::btree_set<std::string_view> btree_set(tasks.begin(), tasks.end()); const phmap::flat_hash_set<std::string_view> flat_hash_set(tasks.begin(), tasks.end()); // stress for (int i = 0; ++i <= 9999999;) { } const auto &benchmark = [&](std::string_view type, auto set, uint64_t stress) { stress = std::min<uint64_t>(stress, max_stress); Benchmark bm; for (int i = 0; ++i <= stress;) { set.contains(tasks[rand() % (tasks.size() - 1)]); } g_logger().info("{}({}) - {}ms", type, stress, bm.duration()); }; benchmark("set", set, 999); benchmark("set", set, 99999); benchmark("set", set, 999999); benchmark("unordered_set", unordered_set, 999); benchmark("unordered_set", unordered_set, 99999); benchmark("unordered_set", unordered_set, 999999); benchmark("btree_set", btree_set, 999); benchmark("btree_set", btree_set, 99999); benchmark("btree_set", btree_set, 999999); benchmark("flat_hash_set", flat_hash_set, 999); benchmark("flat_hash_set", flat_hash_set, 99999); benchmark("flat_hash_set", flat_hash_set, 999999); ``` </details>
- Loading branch information
Showing
6 changed files
with
86 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters