Skip to content

Commit dcae0a6

Browse files
Mitigate JavaScript Object Allocation for Performance Gain (Phase 1) (#337)
1 parent 1d760d2 commit dcae0a6

31 files changed

Lines changed: 2089 additions & 578 deletions

Cargo.lock

Lines changed: 59 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

builds/prepare_builds/templates/package.json.tera

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"//": "Better keep rimraf version in sync with wasm-pack, see https://github.com/rustwasm/wasm-pack/issues/1444",
1515
"devDependencies": {
1616
"rimraf": "^3.0.2",
17-
"typedoc": "^0.25.13"
17+
"typedoc": "^0.25.13",
18+
"typescript": "~5.4.5"
1819
},
1920
"dependencies": {
2021
"wasm-pack": "^0.12.1"

builds/prepare_builds/templates/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"outDir": "./pkg",
44
"module": "ES6",
55
"target": "es6",
6-
"lib": ["es6"],
6+
"lib": ["es6", "ESNext.Disposable"],
77
"moduleResolution": "node",
88
"sourceMap": true,
99
"declaration": true,

builds/prepare_builds/templates/tsconfig_typedoc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"moduleResolution": "node",
55
"sourceMap": true,
66
"declaration": true,
7-
"lib": ["es6"]
7+
"lib": ["es6", "ESNext.Disposable"]
88
},
99
"files": ["./pkg/rapier.d.ts"]
1010
}

src.ts/control/character_controller.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {RawKinematicCharacterController, RawCharacterCollision} from "../raw";
2-
import {Rotation, Vector, VectorOps} from "../math";
2+
import {Rotation, Vector, VectorOps, scratchBuffer} from "../math";
33
import {
44
BroadPhase,
55
Collider,
@@ -333,8 +333,9 @@ export class KinematicCharacterController {
333333
/**
334334
* The movement computed by the last call to `this.computeColliderMovement`.
335335
*/
336-
public computedMovement(): Vector {
337-
return VectorOps.fromRaw(this.raw.computedMovement());
336+
public computedMovement(target?: Vector): Vector {
337+
this.raw.computedMovement(scratchBuffer);
338+
return VectorOps.fromBuffer(scratchBuffer, target);
338339
}
339340

340341
/**
@@ -368,17 +369,25 @@ export class KinematicCharacterController {
368369
} else {
369370
let c = this.rawCharacterCollision;
370371
out = out ?? new CharacterCollision();
371-
out.translationDeltaApplied = VectorOps.fromRaw(
372-
c.translationDeltaApplied(),
372+
c.translationDeltaApplied(scratchBuffer);
373+
out.translationDeltaApplied = VectorOps.fromBuffer(
374+
scratchBuffer,
375+
out.translationDeltaApplied,
373376
);
374-
out.translationDeltaRemaining = VectorOps.fromRaw(
375-
c.translationDeltaRemaining(),
377+
c.translationDeltaRemaining(scratchBuffer);
378+
out.translationDeltaRemaining = VectorOps.fromBuffer(
379+
scratchBuffer,
380+
out.translationDeltaRemaining,
376381
);
377382
out.toi = c.toi();
378-
out.witness1 = VectorOps.fromRaw(c.worldWitness1());
379-
out.witness2 = VectorOps.fromRaw(c.worldWitness2());
380-
out.normal1 = VectorOps.fromRaw(c.worldNormal1());
381-
out.normal2 = VectorOps.fromRaw(c.worldNormal2());
383+
c.worldWitness1(scratchBuffer);
384+
out.witness1 = VectorOps.fromBuffer(scratchBuffer, out.witness1);
385+
c.worldWitness2(scratchBuffer);
386+
out.witness2 = VectorOps.fromBuffer(scratchBuffer, out.witness2);
387+
c.worldNormal1(scratchBuffer);
388+
out.normal1 = VectorOps.fromBuffer(scratchBuffer, out.normal1);
389+
c.worldNormal2(scratchBuffer);
390+
out.normal2 = VectorOps.fromBuffer(scratchBuffer, out.normal2);
382391
out.collider = this.colliders.get(c.handle());
383392
return out;
384393
}

src.ts/control/pid_controller.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {RawPidController} from "../raw";
2-
import {Rotation, RotationOps, Vector, VectorOps} from "../math";
2+
import {Rotation, RotationOps, scratchBuffer, Vector, VectorOps} from "../math";
33
import {Collider, ColliderSet, InteractionGroups, Shape} from "../geometry";
44
import {QueryFilterFlags, World} from "../pipeline";
55
import {IntegrationParameters, RigidBody, RigidBodySet} from "../dynamics";
@@ -151,20 +151,22 @@ export class PidController {
151151
body: RigidBody,
152152
targetPosition: Vector,
153153
targetLinvel: Vector,
154+
target?: Vector,
154155
): Vector {
155156
let rawPos = VectorOps.intoRaw(targetPosition);
156157
let rawVel = VectorOps.intoRaw(targetLinvel);
157-
let correction = this.raw.linear_correction(
158+
this.raw.linear_correction(
158159
this.params.dt,
159160
this.bodies.raw,
160161
body.handle,
161162
rawPos,
162163
rawVel,
164+
scratchBuffer,
163165
);
164166
rawPos.free();
165167
rawVel.free();
166168

167-
return VectorOps.fromRaw(correction);
169+
return VectorOps.fromBuffer(scratchBuffer, target);
168170
}
169171

170172
// #if DIM2
@@ -188,20 +190,22 @@ export class PidController {
188190
body: RigidBody,
189191
targetRotation: Rotation,
190192
targetAngVel: Vector,
193+
target?: Vector,
191194
): Vector {
192195
let rawPos = RotationOps.intoRaw(targetRotation);
193196
let rawVel = VectorOps.intoRaw(targetAngVel);
194-
let correction = this.raw.angular_correction(
197+
this.raw.angular_correction(
195198
this.params.dt,
196199
this.bodies.raw,
197200
body.handle,
198201
rawPos,
199202
rawVel,
203+
scratchBuffer,
200204
);
201205
rawPos.free();
202206
rawVel.free();
203207

204-
return VectorOps.fromRaw(correction);
208+
return VectorOps.fromBuffer(scratchBuffer, target);
205209
}
206210
// #endif
207211
}

0 commit comments

Comments
 (0)