Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: dispatcher #1732

Merged
merged 12 commits into from
Oct 24, 2023
Merged

improve: dispatcher #1732

merged 12 commits into from
Oct 24, 2023

Conversation

mehah
Copy link
Contributor

@mehah mehah commented Oct 21, 2023

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
Note; Unfortunately priority_queue does not have batch insertion.

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

image

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

Fix #1743
Fix #1733

@mehah mehah requested review from luan and dudantas October 23, 2023 19:50
@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

0.0% 0.0% Coverage
0.0% 0.0% Duplication

@luan luan merged commit 2a464ad into main Oct 24, 2023
36 checks passed
@luan luan deleted the improve_dispatcher branch October 24, 2023 22:04
@mehah mehah restored the improve_dispatcher branch October 25, 2023 01:52
@majestyotbr majestyotbr deleted the improve_dispatcher branch December 21, 2023 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants