EntityUpdate server-side optimization

Cache net states of the same net version. Also moved readNetState after init in EntityCreate, should fix bugs like MovementController rotation not being read.
This commit is contained in:
Kae 2023-06-21 15:48:27 +10:00
parent d6fdd96076
commit acc8bc0280
5 changed files with 12 additions and 6 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 361 KiB

After

Width:  |  Height:  |  Size: 401 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 361 KiB

After

Width:  |  Height:  |  Size: 401 KiB

View File

@ -656,8 +656,8 @@ void WorldClient::handleIncomingPackets(List<PacketPtr> const& packets) {
} }
auto entity = entityFactory->netLoadEntity(entityCreate->entityType, entityCreate->storeData); auto entity = entityFactory->netLoadEntity(entityCreate->entityType, entityCreate->storeData);
entity->readNetState(entityCreate->firstNetState);
entity->init(this, entityCreate->entityId, EntityMode::Slave); entity->init(this, entityCreate->entityId, EntityMode::Slave);
entity->readNetState(entityCreate->firstNetState);
m_entityMap->addEntity(entity); m_entityMap->addEntity(entity);
if (m_interpolationTracker.interpolationEnabled()) { if (m_interpolationTracker.interpolationEnabled()) {

View File

@ -392,8 +392,8 @@ void WorldServer::handleIncomingPackets(ConnectionId clientId, List<PacketPtr> c
} }
auto entity = entityFactory->netLoadEntity(entityCreate->entityType, entityCreate->storeData); auto entity = entityFactory->netLoadEntity(entityCreate->entityType, entityCreate->storeData);
entity->readNetState(entityCreate->firstNetState);
entity->init(this, entityCreate->entityId, EntityMode::Slave); entity->init(this, entityCreate->entityId, EntityMode::Slave);
entity->readNetState(entityCreate->firstNetState);
m_entityMap->addEntity(entity); m_entityMap->addEntity(entity);
if (clientInfo->interpolationTracker.interpolationEnabled()) if (clientInfo->interpolationTracker.interpolationEnabled())
@ -605,6 +605,7 @@ void WorldServer::update() {
signalRegion(monitoredRegion.padded(jsonToVec2I(m_serverConfig.get("playerActiveRegionPad")))); signalRegion(monitoredRegion.padded(jsonToVec2I(m_serverConfig.get("playerActiveRegionPad"))));
queueUpdatePackets(pair.first); queueUpdatePackets(pair.first);
} }
m_netStateCache.clear();
for (auto& pair : m_clientInfo) for (auto& pair : m_clientInfo)
pair.second->pendingForward = false; pair.second->pendingForward = false;
@ -1743,10 +1744,14 @@ void WorldServer::queueUpdatePackets(ConnectionId clientId) {
if (connectionId != clientId) { if (connectionId != clientId) {
if (auto version = clientInfo->clientSlavesNetVersion.ptr(entityId)) { if (auto version = clientInfo->clientSlavesNetVersion.ptr(entityId)) {
if (auto updateSetPacket = updateSetPackets.value(connectionId)) { if (auto updateSetPacket = updateSetPackets.value(connectionId)) {
auto updateAndVersion = monitoredEntity->writeNetState(*version); auto pair = make_pair(entityId, *version);
if (!updateAndVersion.first.empty()) auto i = m_netStateCache.find(pair);
updateSetPacket->deltas[entityId] = move(updateAndVersion.first); if (i == m_netStateCache.end())
*version = updateAndVersion.second; i = m_netStateCache.insert(pair, move(monitoredEntity->writeNetState(*version))).first;
const auto& netState = i->second;
if (!netState.first.empty())
updateSetPacket->deltas[entityId] = netState.first;
*version = netState.second;
} }
} else if (!monitoredEntity->masterOnly()) { } else if (!monitoredEntity->masterOnly()) {
// Client was unaware of this entity until now // Client was unaware of this entity until now

View File

@ -347,6 +347,7 @@ private:
CollisionGenerator m_collisionGenerator; CollisionGenerator m_collisionGenerator;
List<CollisionBlock> m_workingCollisionBlocks; List<CollisionBlock> m_workingCollisionBlocks;
HashMap<pair<EntityId, uint64_t>, pair<ByteArray, uint64_t>> m_netStateCache;
OrderedHashMap<ConnectionId, shared_ptr<ClientInfo>> m_clientInfo; OrderedHashMap<ConnectionId, shared_ptr<ClientInfo>> m_clientInfo;
GameTimer m_tileEntityBreakCheckTimer; GameTimer m_tileEntityBreakCheckTimer;