refactor(server): replace chunk set with flat_hash_set and add ref count

- Replace std::unordered_set with absl::flat_hash_set for chunk position sets.
- Add ref_count field to ChunkEntity and implement update_ref_count().
- Remove clear_unused_chunks() and use ref counting for chunk lifetime.
- Add get_chunk_pos_set() accessors to ServerPlayer.
This commit is contained in:
2026-06-28 20:11:14 +08:00
parent cbe3548dcd
commit 9788f68b12
4 changed files with 67 additions and 27 deletions

View File

@@ -2,20 +2,20 @@
#include "Cubed/gameplay/chunk_pos.hpp"
#include "Cubed/gameplay/game_time.hpp"
#include <absl/container/flat_hash_set.h>
#include <atomic>
#include <glm/glm.hpp>
#include <memory>
#include <shared_mutex>
#include <string>
#include <string_view>
#include <unordered_set>
namespace Cubed {
class ServerWorld;
class Session;
class ServerPlayer {
using ChunkPosSet = std::unordered_set<ChunkPos, ChunkPos::Hash>;
public:
using ChunkPosSet = absl::flat_hash_set<ChunkPos, ChunkPos::Hash>;
ServerPlayer(const ServerPlayer&) = delete;
ServerPlayer(ServerPlayer&&) = delete;
ServerPlayer& operator=(const ServerPlayer&) = delete;
@@ -23,7 +23,6 @@ public:
ServerPlayer(std::string_view name, std::string_view uuid,
ServerWorld& m_world, std::shared_ptr<Session> session,
TickType gametick);
using PlayerChunkPosSet = std::unordered_set<ChunkPos, ChunkPos::Hash>;
const glm::vec3& get_pos() const;
const std::string& get_name() const;
@@ -36,6 +35,8 @@ public:
void task_id(int id);
bool has_player(ChunkPos pos) const;
void update_chunk_set(const ChunkPosSet& set);
const ChunkPosSet& get_chunk_pos_set() const;
ChunkPosSet& get_chunk_pos_set();
private:
static constexpr TickType TIMEOUT = 200;
@@ -48,6 +49,6 @@ private:
std::atomic<TickType> m_last_gametick{0};
std::atomic<int> m_chunk_task_id{0};
mutable std::shared_mutex m_chunk_pos_mutex;
PlayerChunkPosSet m_player_chunk_pos_set;
ChunkPosSet m_player_chunk_pos_set;
};
} // namespace Cubed

View File

@@ -10,13 +10,13 @@
#include "Cubed/tools/thread_pool.hpp"
#include "world/block_change.pb.h"
#include <absl/container/flat_hash_set.h>
#include <future>
#include <shared_mutex>
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_queue.h>
#include <tbb/concurrent_unordered_map.h>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
namespace Cubed {
@@ -91,6 +91,7 @@ private:
struct ChunkEntity {
ChunkState state;
std::shared_ptr<ServerChunk> chunk;
uint32_t ref_count = 0;
};
enum class ChunkLoadStyle { RANDOM, CENTER };
@@ -113,7 +114,7 @@ private:
using PlayerHashMap = std::unordered_map<std::string, ServerPlayer>;
using PendingChunkHashMap =
std::unordered_map<ChunkPos, PendingChunk, ChunkPos::Hash>;
using ChunkPosSet = std::unordered_set<ChunkPos, ChunkPos::Hash>;
using ChunkPosSet = absl::flat_hash_set<ChunkPos, ChunkPos::Hash>;
using PlayerUUIDMap = tbb::concurrent_hash_map<std::string, std::string>;
using uuid_acc = PlayerUUIDMap::accessor;
@@ -177,7 +178,7 @@ private:
void poll_finished_chunks();
void wait_all_chunk_tasks();
void clear_unused_chunks();
void update_ref_count(const ChunkPosSet& old, const ChunkPosSet& now);
void send_time();