feat(server-world): use priority thread pool for chunk generation

Introduce PriorityThreadPool to process chunk generation tasks with priorities based on their distance from the player. Closer chunks receive higher priority, improving responsiveness.
This commit is contained in:
2026-07-01 15:13:33 +08:00
parent ad1d3b3ac9
commit 647f980c66
2 changed files with 29 additions and 6 deletions

View File

@@ -6,6 +6,7 @@
#include "Cubed/gameplay/river_worm.hpp" #include "Cubed/gameplay/river_worm.hpp"
#include "Cubed/gameplay/server_chunk.hpp" #include "Cubed/gameplay/server_chunk.hpp"
#include "Cubed/gameplay/server_player.hpp" #include "Cubed/gameplay/server_player.hpp"
#include "Cubed/tools/priority_thread_pool.hpp"
#include "Cubed/tools/recent_queue.hpp" #include "Cubed/tools/recent_queue.hpp"
#include "Cubed/tools/thread_pool.hpp" #include "Cubed/tools/thread_pool.hpp"
#include "world/block_change.pb.h" #include "world/block_change.pb.h"
@@ -147,7 +148,7 @@ private:
RecentQueue<std::string> m_need_gen_queue; RecentQueue<std::string> m_need_gen_queue;
std::atomic<std::shared_ptr<ThreadPool>> m_gen_thread_pool; std::atomic<std::shared_ptr<PriorityThreadPool>> m_gen_thread_pool;
std::atomic<std::shared_ptr<ThreadPool>> m_net_thread_pool; std::atomic<std::shared_ptr<ThreadPool>> m_net_thread_pool;
std::atomic<ChunkLoadStyle> m_chunk_load_style{ChunkLoadStyle::CENTER}; std::atomic<ChunkLoadStyle> m_chunk_load_style{ChunkLoadStyle::CENTER};
@@ -178,6 +179,9 @@ private:
int int
change_pool_threads(std::atomic<std::shared_ptr<ThreadPool>>& thread_pool, change_pool_threads(std::atomic<std::shared_ptr<ThreadPool>>& thread_pool,
int threads); int threads);
int change_pool_threads(
std::atomic<std::shared_ptr<PriorityThreadPool>>& thread_pool,
int threads);
void send_server_stop(); void send_server_stop();
}; };
} // namespace Cubed } // namespace Cubed

View File

@@ -330,12 +330,17 @@ void ServerWorld::submit_new_chunks(const std::string& uuid,
[&dist2](const auto& a, const auto& b) { [&dist2](const auto& a, const auto& b) {
return dist2(a.first) < dist2(b.first); return dist2(a.first) < dist2(b.first);
}); });
for (auto& [pos, task] : tasks) {
pool_ptr->enqueue([this, chunk = std::move(task->chunk)]() mutable { const int CHUNKS_PER_PRIORITY = m_gen_pool_threads;
chunk->gen_chunk();
m_finished_queue.push(std::move(chunk)); for (size_t i = 0; i < tasks.size(); ++i) {
}); int priority = 10 + static_cast<int>(i / CHUNKS_PER_PRIORITY);
auto* task = tasks[i].second;
pool_ptr->enqueue(priority,
[this, chunk = std::move(task->chunk)]() mutable {
chunk->gen_chunk();
m_finished_queue.push(std::move(chunk));
});
} }
} break; } break;
} }
@@ -777,6 +782,20 @@ int ServerWorld::change_pool_threads(
return used_thread; return used_thread;
} }
int ServerWorld::change_pool_threads(
std::atomic<std::shared_ptr<PriorityThreadPool>>& thread_pool,
int threads) {
m_max_threads = std::thread::hardware_concurrency();
if (m_max_threads < 1) {
Logger::warn("Can't Get Max Support Threads, Set Max Threads to 4");
m_max_threads = 1;
}
int used_thread = std::clamp(threads, 1, m_max_threads.load());
Logger::info("Create New Thread Pool Use {} Threads", used_thread);
thread_pool.store(std::make_shared<PriorityThreadPool>(used_thread));
return used_thread;
}
void ServerWorld::send_server_stop() { void ServerWorld::send_server_stop() {
Arena arena; Arena arena;
auto* rsp = Arena::Create<LogoutRsp>(&arena); auto* rsp = Arena::Create<LogoutRsp>(&arena);