Improve performance when hosting canvas with many objects

This commit is contained in:
Yousef Khadadeh 2025-07-30 11:36:55 +03:00
parent 207e173c1a
commit b2f2070f95
5 changed files with 28 additions and 30 deletions

View file

@ -74,27 +74,22 @@ void DrawComponent::server_send_erase_set(MainServer& server, const std::unorder
void DrawComponent::server_send_update_temp(MainServer& server, ServerClientID id) { void DrawComponent::server_send_update_temp(MainServer& server, ServerClientID id) {
server.netServer->send_items_to_all_clients(UNRELIABLE_COMMAND_CHANNEL, CLIENT_UPDATE_COMPONENT, true, id, *this); server.netServer->send_items_to_all_clients(UNRELIABLE_COMMAND_CHANNEL, CLIENT_UPDATE_COMPONENT, true, id, *this);
tempServerUpdateTimer = std::chrono::steady_clock::now();
serverIsTempUpdate = true;
} }
void DrawComponent::server_send_update_final(MainServer& server, ServerClientID id) { void DrawComponent::server_send_update_final(MainServer& server, ServerClientID id) {
server.netServer->send_items_to_all_clients(RELIABLE_COMMAND_CHANNEL, CLIENT_UPDATE_COMPONENT, false, id, *this); server.netServer->send_items_to_all_clients(RELIABLE_COMMAND_CHANNEL, CLIENT_UPDATE_COMPONENT, false, id, *this);
serverIsTempUpdate = false;
} }
void DrawComponent::server_send_transform_many(MainServer& server, const std::vector<std::pair<ServerClientID, CoordSpaceHelper>>& transforms) { void DrawComponent::server_send_transform_many(MainServer& server, const std::vector<std::pair<ServerClientID, CoordSpaceHelper>>& transforms) {
server.netServer->send_items_to_all_clients(RELIABLE_COMMAND_CHANNEL, CLIENT_TRANSFORM_MANY_COMPONENTS, transforms); server.netServer->send_items_to_all_clients(RELIABLE_COMMAND_CHANNEL, CLIENT_TRANSFORM_MANY_COMPONENTS, transforms);
} }
void DrawComponent::server_update(MainServer& server, ServerClientID id) { bool DrawComponent::server_update(MainServer& server, ServerClientID id, const std::chrono::time_point<std::chrono::steady_clock>& tempServerUpdateTimer) {
if(serverIsTempUpdate) { if((std::chrono::steady_clock::now() - tempServerUpdateTimer) >= CLIENT_DRAWCOMP_DELAY_TIMER_DURATION) {
float timeSince = std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::steady_clock::now() - tempServerUpdateTimer).count(); server_send_update_final(server, id);
if(timeSince >= 1.0f) { return true;
server_send_update_final(server, id);
serverIsTempUpdate = false;
}
} }
return false;
} }
void DrawComponent::get_used_resources(std::unordered_set<ServerClientID>& v) const { void DrawComponent::get_used_resources(std::unordered_set<ServerClientID>& v) const {
@ -112,8 +107,7 @@ bool DrawComponent::bounds_draw_check(const DrawData& drawData) const {
void DrawComponent::check_timers(DrawingProgram& drawP) { void DrawComponent::check_timers(DrawingProgram& drawP) {
if(delayedUpdatePtr) { if(delayedUpdatePtr) {
float updateDur = std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::steady_clock::now() - lastUpdateTime).count(); if(std::chrono::steady_clock::now() - lastUpdateTime > CLIENT_DRAWCOMP_DELAY_TIMER_DURATION) {
if(updateDur > CLIENT_DRAWCOMP_DELAY_TIMER_DURATION) {
update_from_delayed_ptr(); update_from_delayed_ptr();
delayedUpdatePtr = nullptr; delayedUpdatePtr = nullptr;
final_update(drawP); final_update(drawP);

View file

@ -14,7 +14,7 @@
#include <include/core/SkVertices.h> #include <include/core/SkVertices.h>
#endif #endif
#define CLIENT_DRAWCOMP_DELAY_TIMER_DURATION 5.0f #define CLIENT_DRAWCOMP_DELAY_TIMER_DURATION std::chrono::seconds(6)
#define DRAWCOMP_MAX_SHIFT_BEFORE_DISAPPEAR 20 #define DRAWCOMP_MAX_SHIFT_BEFORE_DISAPPEAR 20
#define DRAWCOMP_MIN_SHIFT_BEFORE_DISAPPEAR 11 #define DRAWCOMP_MIN_SHIFT_BEFORE_DISAPPEAR 11
@ -71,18 +71,13 @@ class DrawComponent {
void server_send_update_final(MainServer& server, ServerClientID id); void server_send_update_final(MainServer& server, ServerClientID id);
static void server_send_transform_many(MainServer& server, const std::vector<std::pair<ServerClientID, CoordSpaceHelper>>& transforms); static void server_send_transform_many(MainServer& server, const std::vector<std::pair<ServerClientID, CoordSpaceHelper>>& transforms);
std::chrono::time_point<std::chrono::steady_clock> tempServerUpdateTimer;
bool serverIsTempUpdate = false;
std::weak_ptr<CollabListType::ObjectInfo> collabListInfo; std::weak_ptr<CollabListType::ObjectInfo> collabListInfo;
std::weak_ptr<DrawingProgramCacheBVHNode> parentBvhNode; std::weak_ptr<DrawingProgramCacheBVHNode> parentBvhNode;
void server_update(MainServer& server, ServerClientID id); bool server_update(MainServer& server, ServerClientID id, const std::chrono::time_point<std::chrono::steady_clock>& tempServerUpdateTimer);
#ifndef IS_SERVER #ifndef IS_SERVER
std::chrono::time_point<std::chrono::steady_clock> lastTransformTime;
std::chrono::time_point<std::chrono::steady_clock> lastUpdateTime; std::chrono::time_point<std::chrono::steady_clock> lastUpdateTime;
std::shared_ptr<DrawComponent> delayedUpdatePtr = nullptr; std::shared_ptr<DrawComponent> delayedUpdatePtr = nullptr;
bool bounds_draw_check(const DrawData& drawData) const; bool bounds_draw_check(const DrawData& drawData) const;

View file

@ -76,8 +76,7 @@ void DrawingProgram::init_client_callbacks() {
if(!objPtr) if(!objPtr)
return; return;
std::shared_ptr<DrawComponent>& comp = objPtr->obj; std::shared_ptr<DrawComponent>& comp = objPtr->obj;
float dur = std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::steady_clock::now() - comp->lastUpdateTime).count(); if((std::chrono::steady_clock::now() - comp->lastUpdateTime) > CLIENT_DRAWCOMP_DELAY_TIMER_DURATION){
if(dur > CLIENT_DRAWCOMP_DELAY_TIMER_DURATION){
comp->delayedUpdatePtr = nullptr; comp->delayedUpdatePtr = nullptr;
message(*comp); message(*comp);
if(isTemp) if(isTemp)

View file

@ -197,16 +197,20 @@ MainServer::MainServer(World& initWorld, const std::string& serverLocalID):
}); });
netServer->add_recv_callback(SERVER_UPDATE_COMPONENT, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) { netServer->add_recv_callback(SERVER_UPDATE_COMPONENT, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
bool isTemp; bool isTemp;
ServerClientID idToTransform; ServerClientID idToUpdate;
message(isTemp, idToTransform); message(isTemp, idToUpdate);
auto it = data.idToComponentMap.find(idToTransform); auto it = data.idToComponentMap.find(idToUpdate);
if(it != data.idToComponentMap.end()) { if(it != data.idToComponentMap.end()) {
std::shared_ptr<DrawComponent>& comp = it->second; std::shared_ptr<DrawComponent>& comp = it->second;
message(*comp); message(*comp);
if(isTemp) if(isTemp) {
comp->server_send_update_temp(*this, idToTransform); comp->server_send_update_temp(*this, idToUpdate);
else data.updateComponentTimers[idToUpdate] = std::chrono::steady_clock::now();
comp->server_send_update_final(*this, idToTransform); }
else {
comp->server_send_update_final(*this, idToUpdate);
data.updateComponentTimers.erase(idToUpdate);
}
} }
}); });
netServer->add_recv_callback(SERVER_RESOURCE_INIT, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) { netServer->add_recv_callback(SERVER_RESOURCE_INIT, [&](std::shared_ptr<NetServer::ClientData> client, cereal::PortableBinaryInputArchive& message) {
@ -305,8 +309,13 @@ void MainServer::ensure_display_name_unique(std::string& displayName) {
void MainServer::update() { void MainServer::update() {
netServer->update(); netServer->update();
for(auto& c : data.components) std::erase_if(data.updateComponentTimers, [&](auto& c) {
c.second->server_update(*this, c.first); auto& [id, timer] = c;
auto it = data.idToComponentMap.find(id);
if(it == data.idToComponentMap.end())
return true;
return it->second->server_update(*this, id, timer);
});
if(std::chrono::steady_clock::now() - lastKeepAliveSent > std::chrono::seconds(2)) { if(std::chrono::steady_clock::now() - lastKeepAliveSent > std::chrono::seconds(2)) {
netServer->send_items_to_all_clients(UNRELIABLE_COMMAND_CHANNEL, CLIENT_KEEP_ALIVE); netServer->send_items_to_all_clients(UNRELIABLE_COMMAND_CHANNEL, CLIENT_KEEP_ALIVE);
lastKeepAliveSent = std::chrono::steady_clock::now(); lastKeepAliveSent = std::chrono::steady_clock::now();

View file

@ -11,6 +11,7 @@ class ServerData {
std::vector<std::pair<ServerClientID, std::shared_ptr<DrawComponent>>> components; std::vector<std::pair<ServerClientID, std::shared_ptr<DrawComponent>>> components;
std::unordered_map<ServerClientID, std::shared_ptr<DrawComponent>> idToComponentMap; std::unordered_map<ServerClientID, std::shared_ptr<DrawComponent>> idToComponentMap;
std::unordered_map<ServerClientID, ResourceData> resources; std::unordered_map<ServerClientID, ResourceData> resources;
std::unordered_map<ServerClientID, std::chrono::time_point<std::chrono::steady_clock>> updateComponentTimers;
std::unordered_map<std::string, Bookmark> bookmarks; std::unordered_map<std::string, Bookmark> bookmarks;
void save(cereal::PortableBinaryOutputArchive& a) const; void save(cereal::PortableBinaryOutputArchive& a) const;
void write_to_file(cereal::PortableBinaryOutputArchive& a) const; void write_to_file(cereal::PortableBinaryOutputArchive& a) const;