Skip to content

Commit 10e6a73

Browse files
committed
Optimized the way the process function works.
1 parent 639d10c commit 10e6a73

4 files changed

Lines changed: 35 additions & 30 deletions

File tree

core/net_utilities.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ int NS::PeerData::get_latency() const {
1616
return int(compressed_latency) * 4;
1717
}
1818

19-
void NS::PeerData::make_controller(NS::SceneSynchronizerBase &p_scene_sync, int p_peer) {
19+
void NS::PeerData::make_controller() {
2020
controller = std::make_unique<NS::PeerNetworkedController>();
21-
controller->setup_synchronizer(p_scene_sync, p_peer);
2221
}
2322

2423
bool NS::SyncGroup::is_realtime_node_list_changed() const {

core/net_utilities.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ struct PeerData {
449449
void set_compressed_latency(std::uint8_t p_compressed_latency) { compressed_latency = p_compressed_latency; }
450450
std::uint8_t get_compressed_latency() const { return compressed_latency; }
451451

452-
void make_controller(class SceneSynchronizerBase &p_scene_sync, int p_peer);
452+
void make_controller();
453453
PeerNetworkedController *get_controller() {
454454
return controller.get();
455455
}
@@ -523,7 +523,7 @@ struct SyncGroup {
523523

524524
public:
525525
SyncGroupId group_id = SyncGroupId::NONE;
526-
SceneSynchronizerBase *scene_sync = nullptr;
526+
class SceneSynchronizerBase *scene_sync = nullptr;
527527

528528
private:
529529
bool simulated_sync_objects_list_changed = false;

core/peer_networked_controller.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,11 @@ bool PeerNetworkedController::has_another_instant_to_process_after(int p_i) cons
172172
}
173173

174174
void PeerNetworkedController::process(double p_delta) {
175-
// This function is registered as processed function, so it's called by the
176-
// `SceneSync` in sync with the scene processing.
177-
controller->process(p_delta);
175+
if make_likely (controller && can_simulate()) {
176+
// This function is registered as processed function, so it's called by the
177+
// `SceneSync` in sync with the scene processing.
178+
controller->process(p_delta);
179+
}
178180
}
179181

180182
ServerController *PeerNetworkedController::get_server_controller() {

scene_synchronizer.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -919,7 +919,10 @@ void SceneSynchronizerBase::on_peer_connected(int p_peer) {
919919
return;
920920
}
921921

922-
pd_it->second.make_controller(*this, p_peer);
922+
pd_it->second.make_controller();
923+
pd_it->second.get_controller()->setup_synchronizer(*this, p_peer);
924+
// Clear the process function because they need to be rebuild to include the new peer.
925+
process_functions__clear();
923926
reset_controller(*pd_it->second.get_controller());
924927

925928
event_peer_status_updated.broadcast(p_peer, true, true);
@@ -940,6 +943,9 @@ void SceneSynchronizerBase::on_peer_disconnected(int p_peer) {
940943

941944
peer_data.erase(p_peer);
942945

946+
// Clear the process function to make sure the peer process functions are removed.
947+
process_functions__clear();
948+
943949
#ifdef DEBUG_ENABLED
944950
ASSERT_COND_MSG(peer_data.count(p_peer) <= 0, "The peer was just removed. This can't be triggered.");
945951
#endif
@@ -1386,11 +1392,29 @@ void SceneSynchronizerBase::process_functions__execute() {
13861392
NS_PROFILE_WITH_INFO(info);
13871393

13881394
if (cached_process_functions_valid == false) {
1389-
// Clear the process_functions.
1395+
// Clear all the process_functions.
13901396
for (int process_phase = PROCESS_PHASE_EARLY; process_phase < PROCESS_PHASE_COUNT; ++process_phase) {
13911397
cached_process_functions[process_phase].clear();
13921398
}
13931399

1400+
// Add a new process function for each peer
1401+
{
1402+
// Fetch the connected peers and sort them
1403+
std::vector<int> peers;
1404+
for (const auto &[peer, _] : peer_data) {
1405+
peers.push_back(peer);
1406+
}
1407+
std::sort(peers.begin(), peers.end());
1408+
1409+
// For each peer, add the process function.
1410+
for (int peer : peers) {
1411+
PeerNetworkedController *peer_controller = get_controller_for_peer(peer, false);
1412+
if (peer_controller) {
1413+
cached_process_functions[PROCESS_PHASE_PROCESS].bind(std::bind(&PeerNetworkedController::process, peer_controller, std::placeholders::_1));
1414+
}
1415+
}
1416+
}
1417+
13941418
// Build the cached_process_functions, making sure the node data order is kept.
13951419
for (auto od : objects_data_storage.get_sorted_objects_data()) {
13961420
if (od == nullptr || (is_client() && od->realtime_sync_enabled_on_client == false)) {
@@ -1411,27 +1435,7 @@ void SceneSynchronizerBase::process_functions__execute() {
14111435
SceneSynchronizerDebugger::singleton()->print(INFO, "Process functions START");
14121436

14131437
// Pre process phase
1414-
for (int process_phase = PROCESS_PHASE_EARLY; process_phase < PROCESS_PHASE_PROCESS; ++process_phase) {
1415-
const std::string info = "process phase: " + std::to_string(process_phase);
1416-
NS_PROFILE_WITH_INFO(info);
1417-
cached_process_functions[process_phase].broadcast(get_fixed_frame_delta());
1418-
}
1419-
1420-
// Controller process
1421-
{
1422-
const std::string info = "process phase -- CONTROLLER --";
1423-
NS_PROFILE_WITH_INFO(info);
1424-
1425-
// TODO optimize this, as `can_simulate` is expensive.
1426-
for (auto &pd : peer_data) {
1427-
if (pd.second.get_controller() && pd.second.get_controller()->can_simulate()) {
1428-
pd.second.get_controller()->process(get_fixed_frame_delta());
1429-
}
1430-
}
1431-
}
1432-
1433-
// Post process
1434-
for (int process_phase = PROCESS_PHASE_PROCESS; process_phase < PROCESS_PHASE_COUNT; ++process_phase) {
1438+
for (int process_phase = PROCESS_PHASE_EARLY; process_phase < PROCESS_PHASE_COUNT; ++process_phase) {
14351439
const std::string info = "process phase: " + std::to_string(process_phase);
14361440
NS_PROFILE_WITH_INFO(info);
14371441
cached_process_functions[process_phase].broadcast(get_fixed_frame_delta());

0 commit comments

Comments
 (0)