rbxts-transform-boost 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,259 @@
1
+ --!optimize 2
2
+ --!native
3
+
4
+ -- Compiled with rotor v2.2.0
5
+
6
+ -- Services
7
+ local _RunService = game:GetService("RunService")
8
+ local _Workspace = game:GetService("Workspace")
9
+ local _Players = game:GetService("Players")
10
+
11
+ local function integrate(pos: Vector3, vel: Vector3, acc: Vector3, dt: number)
12
+ local _vel = vel
13
+ local _acc = acc
14
+ local _dt = dt
15
+ const newVel = _vel + (_acc * _dt)
16
+
17
+ local _pos = pos
18
+ local _dt_1 = dt
19
+ local _arg0 = newVel * _dt_1
20
+ const newPos = _pos + _arg0
21
+
22
+ return { newPos, newVel }
23
+ end
24
+
25
+ local function dot(a: Vector3, b: Vector3): number
26
+ return a.X * b.X + a.Y * b.Y + a.Z * b.Z
27
+ end
28
+
29
+ local function cross(a: Vector3, b: Vector3): Vector3
30
+ local _cache0 = a.Y
31
+ local _cache1 = b.Z
32
+ local _cache2 = a.Z
33
+ local _cache3 = b.Y
34
+ local _cache4 = b.X
35
+ local _cache5 = a.X
36
+
37
+ return Vector3.new(_cache0 * _cache1 - _cache2 * _cache3, _cache2 * _cache4 - _cache5 * _cache1, _cache5 * _cache3 - _cache0 * _cache4)
38
+ end
39
+
40
+ local function lerpVec3(a: Vector3, b: Vector3, t: number): Vector3
41
+ local _cache0 = a.X
42
+ local _cache1 = a.Y
43
+ local _cache2 = a.Z
44
+
45
+ return Vector3.new(_cache0 + (b.X - _cache0) * t, _cache1 + (b.Y - _cache1) * t, _cache2 + (b.Z - _cache2) * t)
46
+ end
47
+
48
+ -- ── Buffer / encoding ────────────────────────────────────────────────────────
49
+
50
+ local function encodeFixed(buf: buffer, offset: number, value: number, scale: number): number
51
+ const fixed = math.floor(value * scale)
52
+ const clamped = math.clamp(fixed, -32768, 32767)
53
+ buffer.writei16(buf, offset, clamped)
54
+
55
+ return offset + 2
56
+ end
57
+
58
+ local function encodePacket(buf: buffer, x: number, y: number, z: number, scale: number)
59
+ local off = 0
60
+ off = encodeFixed(buf, off, x, scale)
61
+ off = encodeFixed(buf, off, y, scale)
62
+ encodeFixed(buf, off, z, scale)
63
+ end
64
+
65
+ -- ── Math / arithmetic ────────────────────────────────────────────────────────
66
+
67
+ local function sumWeighted(values: {number}, weights: {number}): number
68
+ local total = 0
69
+
70
+ do
71
+ local _len_values = #values
72
+
73
+ do
74
+ local i = 0
75
+ local _shouldIncrement = false
76
+
77
+ while true do
78
+ if _shouldIncrement then
79
+ i += 1
80
+ else
81
+ _shouldIncrement = true
82
+ end
83
+
84
+ if not (i < _len_values) then
85
+ break
86
+ end
87
+
88
+ total += values[i + 1] * weights[i + 1]
89
+ end
90
+ end
91
+ end
92
+
93
+ return total
94
+ end
95
+
96
+ local function dotProduct(a: {number}, b: {number}): number
97
+ local sum = 0
98
+
99
+ do
100
+ local _len_a = #a
101
+
102
+ do
103
+ local i = 0
104
+ local _shouldIncrement = false
105
+
106
+ while true do
107
+ if _shouldIncrement then
108
+ i += 1
109
+ else
110
+ _shouldIncrement = true
111
+ end
112
+
113
+ if not (i < _len_a) then
114
+ break
115
+ end
116
+
117
+ sum += a[i + 1] * b[i + 1]
118
+ end
119
+ end
120
+ end
121
+
122
+ return sum
123
+ end
124
+
125
+ local function norm(values: {number}): number
126
+ local sq = 0
127
+
128
+ do
129
+ local _len_values = #values
130
+
131
+ do
132
+ local i = 0
133
+ local _shouldIncrement = false
134
+
135
+ while true do
136
+ if _shouldIncrement then
137
+ i += 1
138
+ else
139
+ _shouldIncrement = true
140
+ end
141
+
142
+ if not (i < _len_values) then
143
+ break
144
+ end
145
+
146
+ sq += values[i + 1] * values[i + 1]
147
+ end
148
+ end
149
+ end
150
+
151
+ return math.sqrt(sq)
152
+ end
153
+
154
+ local function mathHeavy(x: number, y: number): number
155
+ return math.sin(x) * math.cos(y) + math.sqrt(x * x + y * y) + math.atan2(y, x)
156
+ end
157
+
158
+ local function fib(n: number): number
159
+
160
+ if n <= 1 then
161
+ return n
162
+ end
163
+
164
+ local a = 0
165
+ local b = 1
166
+
167
+ do
168
+ local i = 2
169
+ local _shouldIncrement = false
170
+
171
+ while true do
172
+ if _shouldIncrement then
173
+ i += 1
174
+ else
175
+ _shouldIncrement = true
176
+ end
177
+
178
+ if not (i <= n) then
179
+ break
180
+ end
181
+
182
+ const tmp = a + b
183
+ a = b
184
+ b = tmp
185
+ end
186
+ end
187
+
188
+ return b
189
+ end
190
+
191
+ -- ── CFrame ───────────────────────────────────────────────────────────────────
192
+
193
+ local function cfLookAt(eye: Vector3, target: Vector3): CFrame
194
+ return CFrame.lookAt(eye, target)
195
+ end
196
+
197
+ local function cfChain(cf: CFrame, dt: number): CFrame
198
+ local _cf = cf
199
+ local _arg0 = CFrame.Angles(0, dt, 0)
200
+
201
+ return _cf * _arg0
202
+ end
203
+
204
+ -- ── Services / instance ──────────────────────────────────────────────────────
205
+
206
+ local function serviceWork(): string
207
+ const count = #_Players:GetPlayers()
208
+ const running = _RunService:IsRunning()
209
+
210
+ return `{count}-{running}`
211
+ end
212
+
213
+ local function multiService(): boolean
214
+ const players = _Players
215
+ const rs = _RunService
216
+ const ws = _Workspace
217
+
218
+ return rs:IsRunning() and players.MaxPlayers > 0 and ws.Gravity > 0
219
+ end
220
+
221
+ local function cameraWork(camera: Camera): number
222
+ local _cache0 = camera.CFrame
223
+ const pos = _cache0.Position
224
+ const look = _cache0.LookVector
225
+ const fov = camera.FieldOfView
226
+
227
+ return pos.Magnitude + look.X + fov
228
+ end
229
+
230
+ -- ── String ───────────────────────────────────────────────────────────────────
231
+
232
+ local function formatStats(label: string, value: number, unit: string): string
233
+ return `{label}: {string.format("%.4f", value)} {unit}`
234
+ end
235
+
236
+ local function buildKey(prefix: string, id: number, suffix: string): string
237
+ return `{prefix}_{id}_{suffix}`
238
+ end
239
+
240
+ return {
241
+ integrate = integrate,
242
+ dot = dot,
243
+ cross = cross,
244
+ lerpVec3 = lerpVec3,
245
+ encodeFixed = encodeFixed,
246
+ encodePacket = encodePacket,
247
+ sumWeighted = sumWeighted,
248
+ dotProduct = dotProduct,
249
+ norm = norm,
250
+ mathHeavy = mathHeavy,
251
+ fib = fib,
252
+ cfLookAt = cfLookAt,
253
+ cfChain = cfChain,
254
+ serviceWork = serviceWork,
255
+ multiService = multiService,
256
+ cameraWork = cameraWork,
257
+ formatStats = formatStats,
258
+ buildKey = buildKey,
259
+ }
@@ -1,5 +1,5 @@
1
- import * as opt from "../shared/fns";
2
1
  import * as base from "../shared/fns-bare";
2
+ import * as opt from "../shared/fns";
3
3
 
4
4
  function bench(label: string, n: number, fn: () => void): void {
5
5
  task.wait(0.05);
@@ -9,41 +9,41 @@ function bench(label: string, n: number, fn: () => void): void {
9
9
  print(` ${label}: ${string.format("%.3f", (elapsed / n) * 1e6)} us/iter`);
10
10
  }
11
11
 
12
- const N = 100000;
13
- const NS = 10000;
12
+ const N = 100000;
13
+ const NS = 10000;
14
14
 
15
- const pos = new Vector3(1, 2, 3);
16
- const vel = new Vector3(0, 1, 0);
17
- const acc = new Vector3(0, -9.8, 0);
15
+ const pos = new Vector3(1, 2, 3);
16
+ const vel = new Vector3(0, 1, 0);
17
+ const acc = new Vector3(0, -9.8, 0);
18
18
  const vecA = new Vector3(1, 0, 0);
19
19
  const vecB = new Vector3(0, 1, 0);
20
- const eye = new Vector3(0, 5, 10);
21
- const tgt = new Vector3(0, 0, 0);
22
- const cf = CFrame.lookAt(eye, tgt);
23
- const buf = buffer.create(256);
20
+ const eye = new Vector3(0, 5, 10);
21
+ const tgt = new Vector3(0, 0, 0);
22
+ const cf = CFrame.lookAt(eye, tgt);
23
+ const buf = buffer.create(256);
24
24
  const vals = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
25
- const wts = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
26
- const cam = game.GetService("Workspace").CurrentCamera!;
25
+ const wts = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
26
+ const cam = game.GetService("Workspace").CurrentCamera!;
27
27
 
28
28
  function runSuite(fns: typeof opt): void {
29
- bench("integrate (verlet)", N, () => fns.integrate(pos, vel, acc, 1 / 60));
30
- bench("dot (V3 manual)", N, () => fns.dot(vecA, vecB));
31
- bench("cross (V3 manual)", N, () => fns.cross(vecA, vecB));
32
- bench("lerpVec3 (V3 manual)", N, () => fns.lerpVec3(pos, eye, 0.5));
33
- bench("encodeFixed (buf+math)", N, () => fns.encodeFixed(buf, 0, 3.14, 100));
34
- bench("encodePacket(3x fixed)", N, () => fns.encodePacket(buf, 1.1, 2.2, 3.3, 100));
35
- bench("sumWeighted (loop)", N, () => fns.sumWeighted(vals, wts));
36
- bench("dotProduct (loop)", N, () => fns.dotProduct(vals, vals));
37
- bench("norm (loop+sqrt)", N, () => fns.norm(vals));
38
- bench("mathHeavy (trig+sqrt)", N, () => fns.mathHeavy(1.23, 4.56));
39
- bench("fib(20) (iter)", N, () => fns.fib(20));
40
- bench("cfLookAt (ctor)", NS, () => fns.cfLookAt(eye, tgt));
41
- bench("cfChain (mul+angles)", N, () => fns.cfChain(cf, 0.016));
42
- bench("serviceWork (GetService x2)", N, () => fns.serviceWork());
43
- bench("multiSvc (GetService x3)", N, () => fns.multiService());
44
- bench("cameraWork (prop chain)", N, () => fns.cameraWork(cam));
45
- bench("formatStats (template)", N, () => fns.formatStats("speed", 9.81, "m/s"));
46
- bench("buildKey (template)", N, () => fns.buildKey("player", 42, "data"));
29
+ bench("integrate (verlet)", N, () => fns.integrate(pos, vel, acc, 1 / 60));
30
+ bench("dot (V3 manual)", N, () => fns.dot(vecA, vecB));
31
+ bench("cross (V3 manual)", N, () => fns.cross(vecA, vecB));
32
+ bench("lerpVec3 (V3 manual)", N, () => fns.lerpVec3(pos, eye, 0.5));
33
+ bench("encodeFixed (buf+math)", N, () => fns.encodeFixed(buf, 0, 3.14, 100));
34
+ bench("encodePacket(3x fixed)", N, () => fns.encodePacket(buf, 1.1, 2.2, 3.3, 100));
35
+ bench("sumWeighted (loop)", N, () => fns.sumWeighted(vals, wts));
36
+ bench("dotProduct (loop)", N, () => fns.dotProduct(vals, vals));
37
+ bench("norm (loop+sqrt)", N, () => fns.norm(vals));
38
+ bench("mathHeavy (trig+sqrt)", N, () => fns.mathHeavy(1.23, 4.56));
39
+ bench("fib(20) (iter)", N, () => fns.fib(20));
40
+ bench("cfLookAt (ctor)", NS, () => fns.cfLookAt(eye, tgt));
41
+ bench("cfChain (mul+angles)", N, () => fns.cfChain(cf, 0.016));
42
+ bench("serviceWork (GetService x2)", N, () => fns.serviceWork());
43
+ bench("multiSvc (GetService x3)", N, () => fns.multiService());
44
+ bench("cameraWork (prop chain)", N, () => fns.cameraWork(cam));
45
+ bench("formatStats (template)", N, () => fns.formatStats("speed", 9.81, "m/s"));
46
+ bench("buildKey (template)", N, () => fns.buildKey("player", 42, "data"));
47
47
  }
48
48
 
49
49
  print("\n=== optimized (--!native + transformer) ===");
@@ -1,3 +1,5 @@
1
+ //!native
2
+
1
3
  // ── Vector3 / physics ────────────────────────────────────────────────────────
2
4
 
3
5
  export function integrate(pos: Vector3, vel: Vector3, acc: Vector3, dt: number): [Vector3, Vector3] {
@@ -13,7 +13,7 @@
13
13
  "outDir": "./out/src",
14
14
  "typeRoots": [
15
15
  "./node_modules/@rbxts"
16
- ],
16
+ ]
17
17
  },
18
18
  "include": [
19
19
  "./src/shared/fns-bare.ts"
package/dist/config.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export interface PluginConfig {
2
2
  optimize?: boolean;
3
+ strict?: boolean;
3
4
  hoist?: boolean;
4
5
  }
package/dist/index.js CHANGED
@@ -6,15 +6,15 @@ const cache_1 = require("./passes/cache");
6
6
  const loops_1 = require("./passes/loops");
7
7
  const annotate_1 = require("./passes/annotate");
8
8
  function default_1(program, config = {}, { ts }) {
9
- const { optimize = true, hoist = true } = config;
9
+ const { optimize = true, strict = true, hoist = true } = config;
10
10
  return (ctx) => (sourceFile) => {
11
11
  (0, annotate_1.annotatePass)(ts, program, sourceFile);
12
12
  let result = sourceFile;
13
13
  if (hoist)
14
14
  result = (0, cache_1.cachePass)(ts, program, ctx, result);
15
15
  result = (0, loops_1.loopsPass)(ts, program, ctx, result);
16
- if (optimize)
17
- result = (0, native_1.nativePass)(ts, ctx, result);
16
+ if (optimize || strict)
17
+ result = (0, native_1.nativePass)(ts, ctx, result, optimize, strict);
18
18
  return result;
19
19
  };
20
20
  }