From c6b2ef46271fac04fc8ae0ab9cf734f39bb8a0fd Mon Sep 17 00:00:00 2001 From: lena Date: Wed, 5 Jul 2023 13:41:55 +0200 Subject: [PATCH 1/4] remove duplicate code in getAllSprings() function we were previously doing almost the same work in with both spring types but were splitting those into different arms of an if statement --- shared/src/SimuloPhysicsServer.ts | 61 +++++++++---------------------- 1 file changed, 18 insertions(+), 43 deletions(-) diff --git a/shared/src/SimuloPhysicsServer.ts b/shared/src/SimuloPhysicsServer.ts index f5c385bdb..9da45bf1a 100644 --- a/shared/src/SimuloPhysicsServer.ts +++ b/shared/src/SimuloPhysicsServer.ts @@ -2168,57 +2168,32 @@ class SimuloPhysicsServer { var springs: { p1: number[], p2: number[], image: string | null, line: { color: string, scale_with_zoom: boolean } | null, width: number }[] = []; // distance joints are considered springs var mouseSprings: { p1: number[], p2: number[], image: string | null, line: { color: string, scale_with_zoom: boolean } | null, width: number }[] = []; while (box2D.getPointer(joint)) { - var j = joint; + const j = joint; joint = joint.GetNext(); - if (j.GetType() == box2D.e_distanceJoint) { - var d = box2D.castObject(j, box2D.b2DistanceJoint); - var dData = d.GetUserData() as SimuloJointData; - var image: string | null; - if (dData.image != null) { - image = dData.image; - } - else { - image = null; - } - var line: { color: string, scale_with_zoom: boolean } | null; - if (dData.line != null) { - line = dData.line; - } - else { - line = null; - } - springs.push({ + const d = box2D.castObject(j, box2D.b2DistanceJoint); + const dData = d.GetUserData() as SimuloJointData; + var image: string | null; + if (dData.image != null) { + image = dData.image; + } + else { + image = null; + } + var line: { color: string, scale_with_zoom: boolean } | null; + line = dData.line; + + const spring = { p1: [d.GetAnchorA().get_x(), d.GetAnchorA().get_y()], p2: [d.GetAnchorB().get_x(), d.GetAnchorB().get_y()], image: image, line: line, width: dData.width, - }); + }; + if (j.GetType() == box2D.e_distanceJoint) { + springs.push(spring); } else if (j.GetType() == box2D.e_mouseJoint) { - var m = box2D.castObject(j, box2D.b2MouseJoint); - var mData = m.GetUserData() as SimuloJointData; - var image: string | null; - if (mData.image != null) { - image = mData.image; - } - else { - image = null; - } - var line: { color: string, scale_with_zoom: boolean } | null; - if (mData.line != null) { - line = mData.line; - } - else { - line = null; - } - mouseSprings.push({ - p1: [m.GetAnchorA().get_x(), m.GetAnchorA().get_y()], - p2: [m.GetAnchorB().get_x(), m.GetAnchorB().get_y()], - image: image, - line: line, - width: mData.width, - }); + mouseSprings.push(spring); } } return { springs: springs, mouseSprings: mouseSprings }; From 88892ae6ba2a794d73c488dcf4c97409dfd7db79 Mon Sep 17 00:00:00 2001 From: lena Date: Wed, 5 Jul 2023 13:50:05 +0200 Subject: [PATCH 2/4] stop doing unneeded work in the previous commit removed the early if checks, causing lot's of code to be executed where it isn't strictly needed --- shared/src/SimuloPhysicsServer.ts | 49 ++++++++++++++++--------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/shared/src/SimuloPhysicsServer.ts b/shared/src/SimuloPhysicsServer.ts index 9da45bf1a..40d0373ce 100644 --- a/shared/src/SimuloPhysicsServer.ts +++ b/shared/src/SimuloPhysicsServer.ts @@ -2170,30 +2170,33 @@ class SimuloPhysicsServer { while (box2D.getPointer(joint)) { const j = joint; joint = joint.GetNext(); - const d = box2D.castObject(j, box2D.b2DistanceJoint); - const dData = d.GetUserData() as SimuloJointData; - var image: string | null; - if (dData.image != null) { - image = dData.image; - } - else { - image = null; - } - var line: { color: string, scale_with_zoom: boolean } | null; - line = dData.line; + const joint_type = j.GetType(); + if (joint_type == box2D.e_distanceJoint || joint_type == box2D.e_mouseJoint) { + const d = box2D.castObject(j, box2D.b2DistanceJoint); + const dData = d.GetUserData() as SimuloJointData; + var image: string | null; + if (dData.image != null) { + image = dData.image; + } + else { + image = null; + } + var line: { color: string, scale_with_zoom: boolean } | null; + line = dData.line; - const spring = { - p1: [d.GetAnchorA().get_x(), d.GetAnchorA().get_y()], - p2: [d.GetAnchorB().get_x(), d.GetAnchorB().get_y()], - image: image, - line: line, - width: dData.width, - }; - if (j.GetType() == box2D.e_distanceJoint) { - springs.push(spring); - } - else if (j.GetType() == box2D.e_mouseJoint) { - mouseSprings.push(spring); + const spring = { + p1: [d.GetAnchorA().get_x(), d.GetAnchorA().get_y()], + p2: [d.GetAnchorB().get_x(), d.GetAnchorB().get_y()], + image: image, + line: line, + width: dData.width, + }; + if (joint_type == box2D.e_distanceJoint) { + springs.push(spring); + } + else if (joint_type == box2D.e_mouseJoint) { + mouseSprings.push(spring); + } } } return { springs: springs, mouseSprings: mouseSprings }; From 1860669a6915886746c71156b42e12831e461566 Mon Sep 17 00:00:00 2001 From: lena Date: Wed, 5 Jul 2023 14:24:14 +0200 Subject: [PATCH 3/4] remove redundant if introduced in one of the prior commits --- shared/src/SimuloPhysicsServer.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/shared/src/SimuloPhysicsServer.ts b/shared/src/SimuloPhysicsServer.ts index 40d0373ce..ae0921920 100644 --- a/shared/src/SimuloPhysicsServer.ts +++ b/shared/src/SimuloPhysicsServer.ts @@ -2181,8 +2181,7 @@ class SimuloPhysicsServer { else { image = null; } - var line: { color: string, scale_with_zoom: boolean } | null; - line = dData.line; + var line: { color: string, scale_with_zoom: boolean } | null = dData.line; const spring = { p1: [d.GetAnchorA().get_x(), d.GetAnchorA().get_y()], @@ -2194,7 +2193,7 @@ class SimuloPhysicsServer { if (joint_type == box2D.e_distanceJoint) { springs.push(spring); } - else if (joint_type == box2D.e_mouseJoint) { + else { mouseSprings.push(spring); } } From 17601e7f5d303230296dc1bc23d9c187fd882b5d Mon Sep 17 00:00:00 2001 From: lena Date: Thu, 6 Jul 2023 18:32:09 +0200 Subject: [PATCH 4/4] save only save the world once every 40 iterations the saveWorld function was using like 9% cpu time before --- shared/src/SimuloServerController.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/shared/src/SimuloServerController.ts b/shared/src/SimuloServerController.ts index 01693f5d6..724fe9f53 100644 --- a/shared/src/SimuloServerController.ts +++ b/shared/src/SimuloServerController.ts @@ -74,6 +74,8 @@ class SimuloServerController { theme: SimuloTheme; localClients: SimuloLocalClient[] = []; selectedObjects: { [key: string]: (SimuloJoint | SimuloObject)[] } = {}; + // counts the number of iterations done in nonpaused mode + itCount: number = 0; sendAll(type: string, data: any) { if (this.networkServer) { @@ -125,9 +127,10 @@ class SimuloServerController { this.physicsServer.loadWorld(this.savedWorld); console.log("reverted to last step"); } - else { + else if (this.itCount % 40 == 0) { this.savedWorld = this.physicsServer.saveWorld(); } + this.itCount++; } var render = this.physicsServer.render() as SimuloStep;