luauperf 0.1.6

A static performance linter for Luau
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use crate::lint::{Hit, Rule, Severity};
use crate::visit;

pub struct TwoArgInstanceNew;
pub struct PropertyChangeSignalWrong;
pub struct ClearAllChildrenLoop;
pub struct SetParentInLoop;
pub struct PropertyBeforeParent;
pub struct RepeatedFindFirstChild;
pub struct ChangedOnMovingPart;
pub struct BulkPropertySet;
pub struct CollectionServiceInLoop;
pub struct NameIndexingInLoop;
pub struct DestroyInLoop;
pub struct GetChildrenInLoop;

impl Rule for TwoArgInstanceNew {
    fn id(&self) -> &'static str { "instance::two_arg_instance_new" }
    fn severity(&self) -> Severity { Severity::Error }

    fn check(&self, _source: &str, ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let mut hits = Vec::new();
        visit::each_call(ast, |call, _ctx| {
            if visit::is_dot_call(call, "Instance", "new") && visit::call_arg_count(call) == 2 {
                hits.push(Hit {
                    pos: visit::call_pos(call),
                    msg: "Instance.new(class, parent) is 40x slower - set Parent after all properties".into(),
                });
            }
        });
        hits
    }
}

impl Rule for PropertyChangeSignalWrong {
    fn id(&self) -> &'static str { "instance::property_change_signal_wrong" }
    fn severity(&self) -> Severity { Severity::Warn }

    fn check(&self, source: &str, _ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let mut hits = Vec::new();
        for pos in visit::find_pattern_positions(source, ".Changed:Connect") {
            let before = &source[..pos];
            if !before.ends_with("GetPropertyChangedSignal") && !before.ends_with("Humanoid") {
                hits.push(Hit {
                    pos,
                    msg: ".Changed fires for ANY property - use GetPropertyChangedSignal(\"Prop\") for specific properties".into(),
                });
            }
        }
        hits
    }
}

impl Rule for ClearAllChildrenLoop {
    fn id(&self) -> &'static str { "instance::clear_all_children_loop" }
    fn severity(&self) -> Severity { Severity::Warn }

    fn check(&self, _source: &str, ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let mut hits = Vec::new();
        visit::each_call(ast, |call, ctx| {
            if ctx.in_loop && visit::is_method_call(call, "Destroy") {
                let src = format!("{call}");
                if src.contains("child") || src.contains("obj") || src.contains("item") || src.contains("v:") {
                    hits.push(Hit {
                        pos: visit::call_pos(call),
                        msg: ":Destroy() in loop over children - use :ClearAllChildren() instead".into(),
                    });
                }
            }
        });
        hits
    }
}

impl Rule for SetParentInLoop {
    fn id(&self) -> &'static str { "instance::set_parent_in_loop" }
    fn severity(&self) -> Severity { Severity::Warn }

    fn check(&self, source: &str, _ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let parent_positions = visit::find_pattern_positions(source, ".Parent =");
        if parent_positions.is_empty() {
            return vec![];
        }

        let loop_depth = build_loop_depth_map(source);
        let line_starts = line_start_offsets(source);

        parent_positions
            .into_iter()
            .filter(|&pos| {
                let line = line_starts.partition_point(|&s| s <= pos).saturating_sub(1);
                line < loop_depth.len() && loop_depth[line] > 0
            })
            .map(|pos| Hit {
                pos,
                msg: ".Parent set in loop - triggers replication + ancestry events per iteration, set Parent last".into(),
            })
            .collect()
    }
}

impl Rule for PropertyBeforeParent {
    fn id(&self) -> &'static str { "instance::property_before_parent" }
    fn severity(&self) -> Severity { Severity::Warn }

    fn check(&self, source: &str, _ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let parent_positions = visit::find_pattern_positions(source, ".Parent =");
        if parent_positions.is_empty() {
            return vec![];
        }

        let instance_new_positions = visit::find_pattern_positions(source, "Instance.new(");
        if instance_new_positions.is_empty() {
            return vec![];
        }

        let mut hits = Vec::new();
        for &parent_pos in &parent_positions {
            let search_start = visit::floor_char(source, parent_pos.saturating_sub(300));
            let before = &source[search_start..parent_pos];
            if !before.contains("Instance.new(") {
                continue;
            }

            let after_parent = parent_pos + ".Parent =".len();
            let after_end = visit::ceil_char(source, (after_parent + 300).min(source.len()));
            let after = &source[after_parent..after_end];

            let mut found_prop_after = false;
            for line in after.lines().skip(1) {
                let trimmed = line.trim();
                if trimmed.is_empty() || trimmed.starts_with("--") {
                    continue;
                }
                if trimmed == "end" || trimmed.starts_with("end") || trimmed.starts_with("local ") || trimmed.starts_with("return") {
                    break;
                }
                if trimmed.contains('.') && trimmed.contains(" = ") && !trimmed.contains(".Parent") {
                    let dot_part = trimmed.split('.').nth(1).unwrap_or("");
                    if dot_part.starts_with(|c: char| c.is_uppercase()) {
                        found_prop_after = true;
                        break;
                    }
                }
            }

            if found_prop_after {
                hits.push(Hit {
                    pos: parent_pos,
                    msg: ".Parent set before other properties - set properties FIRST, parent LAST to batch replication".into(),
                });
            }
        }
        hits
    }
}

impl Rule for RepeatedFindFirstChild {
    fn id(&self) -> &'static str { "instance::repeated_find_first_child" }
    fn severity(&self) -> Severity { Severity::Warn }

    fn check(&self, source: &str, _ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let positions = visit::find_pattern_positions(source, ":FindFirstChild(");
        if positions.len() < 2 {
            return vec![];
        }

        let mut calls: Vec<(usize, String)> = Vec::new();
        for &pos in &positions {
            let after = &source[pos + ":FindFirstChild(".len()..];
            if let Some(close) = after.find(')') {
                let arg = after[..close].trim().to_string();
                if !arg.is_empty() {
                    calls.push((pos, arg));
                }
            }
        }

        let mut hits = Vec::new();
        let mut seen: std::collections::HashMap<&str, usize> = std::collections::HashMap::new();
        for (pos, arg) in &calls {
            if let Some(&first_pos) = seen.get(arg.as_str()) {
                if pos - first_pos < 1000 {
                    hits.push(Hit {
                        pos: *pos,
                        msg: format!("duplicate FindFirstChild({arg}) - cache the result in a local variable"),
                    });
                }
            } else {
                seen.insert(arg, *pos);
            }
        }
        hits
    }
}

impl Rule for ChangedOnMovingPart {
    fn id(&self) -> &'static str { "instance::changed_on_moving_part" }
    fn severity(&self) -> Severity { Severity::Warn }

    fn check(&self, source: &str, _ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let mut hits = Vec::new();
        for pos in visit::find_pattern_positions(source, ".Changed:Connect") {
            let search_start = visit::floor_char(source, pos.saturating_sub(200));
            let before = &source[search_start..pos];
            let part_indicators = ["BasePart", "Part", "MeshPart", "UnionOperation", "Model",
                                   "PrimaryPart", ".Position", ".CFrame"];
            let is_likely_part = part_indicators.iter().any(|ind| before.contains(ind));
            if is_likely_part {
                hits.push(Hit {
                    pos,
                    msg: ".Changed on Part/Model fires for every physics update - use GetPropertyChangedSignal(\"Prop\")".into(),
                });
            }
        }
        hits
    }
}

impl Rule for BulkPropertySet {
    fn id(&self) -> &'static str { "instance::bulk_property_set" }
    fn severity(&self) -> Severity { Severity::Allow }

    fn check(&self, source: &str, _ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let mut hits = Vec::new();
        let lines: Vec<&str> = source.lines().collect();
        let mut i = 0;

        while i < lines.len() {
            let trimmed = lines[i].trim();
            if let Some((var, _prop)) = parse_property_assignment(trimmed) {
                let start_line = i;
                let mut count = 1u32;
                let mut j = i + 1;
                while j < lines.len() {
                    let next = lines[j].trim();
                    if next.is_empty() || next.starts_with("--") {
                        j += 1;
                        continue;
                    }
                    if let Some((next_var, _)) = parse_property_assignment(next) {
                        if next_var == var {
                            count += 1;
                            j += 1;
                            continue;
                        }
                    }
                    break;
                }
                if count >= 5 {
                    let byte_pos = lines[..start_line].iter().map(|l| l.len() + 1).sum::<usize>();
                    hits.push(Hit {
                        pos: byte_pos,
                        msg: format!("{count} consecutive property sets on '{var}' - if parented, each triggers replication; consider BulkMoveTo or batching"),
                    });
                }
                i = j;
            } else {
                i += 1;
            }
        }
        hits
    }
}

impl Rule for CollectionServiceInLoop {
    fn id(&self) -> &'static str { "instance::collection_service_in_loop" }
    fn severity(&self) -> Severity { Severity::Warn }

    fn check(&self, _source: &str, ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let mut hits = Vec::new();
        visit::each_call(ast, |call, ctx| {
            if !ctx.in_loop {
                return;
            }
            let pos = visit::call_pos(call);
            if visit::is_method_call(call, "AddTag") || visit::is_method_call(call, "RemoveTag") {
                hits.push(Hit {
                    pos,
                    msg: "AddTag/RemoveTag in loop - triggers CollectionService event per call, batch outside loop".into(),
                });
            } else if visit::is_method_call(call, "HasTag") {
                hits.push(Hit {
                    pos,
                    msg: "HasTag() in loop - consider caching tag state outside loop".into(),
                });
            }
        });
        hits
    }
}

impl Rule for NameIndexingInLoop {
    fn id(&self) -> &'static str { "instance::name_indexing_in_loop" }
    fn severity(&self) -> Severity { Severity::Allow }

    fn check(&self, source: &str, _ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let loop_depth = build_loop_depth_map(source);
        let line_starts = line_start_offsets(source);
        let mut hits = Vec::new();

        for pos in visit::find_pattern_positions(source, "workspace.") {
            let after = &source[pos + "workspace.".len()..];
            let name_end = after.find(|c: char| !c.is_alphanumeric() && c != '_').unwrap_or(after.len());
            let name = &after[..name_end];
            if name.is_empty() || !name.starts_with(|c: char| c.is_uppercase()) {
                continue;
            }
            let line = line_starts.partition_point(|&s| s <= pos).saturating_sub(1);
            if line < loop_depth.len() && loop_depth[line] > 0 {
                hits.push(Hit {
                    pos,
                    msg: format!("workspace.{name} accessed in loop - property lookup each iteration, cache in a local"),
                });
            }
        }
        hits
    }
}

fn parse_property_assignment(line: &str) -> Option<(&str, &str)> {
    let eq_pos = line.find(" = ")?;
    let lhs = line[..eq_pos].trim();
    let dot_pos = lhs.rfind('.')?;
    let var = &lhs[..dot_pos];
    let prop = &lhs[dot_pos + 1..];
    if var.is_empty() || prop.is_empty() {
        return None;
    }
    if !prop.starts_with(|c: char| c.is_uppercase()) {
        return None;
    }
    if var.contains(' ') || var.contains('(') {
        return None;
    }
    Some((var, prop))
}

fn line_start_offsets(source: &str) -> Vec<usize> {
    let mut starts = vec![0];
    for (i, b) in source.bytes().enumerate() {
        if b == b'\n' {
            starts.push(i + 1);
        }
    }
    starts
}

fn build_loop_depth_map(source: &str) -> Vec<u32> {
    let mut depth: u32 = 0;
    let mut depths = Vec::new();
    for line in source.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("for ") || trimmed.starts_with("while ") || trimmed.starts_with("repeat") {
            depth += 1;
        }
        depths.push(depth);
        if trimmed == "end" || trimmed.starts_with("end ") || trimmed.starts_with("until ") || trimmed == "until" {
            depth = depth.saturating_sub(1);
        }
    }
    depths
}

impl Rule for DestroyInLoop {
    fn id(&self) -> &'static str { "instance::destroy_in_loop" }
    fn severity(&self) -> Severity { Severity::Warn }

    fn check(&self, _source: &str, ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let mut hits = Vec::new();
        visit::each_call(ast, |call, ctx| {
            if ctx.in_loop && visit::is_method_call(call, "Destroy") {
                hits.push(Hit {
                    pos: visit::call_pos(call),
                    msg: ":Destroy() in loop triggers ancestry-changed events per call - consider :ClearAllChildren() on the parent or batch with Debris".into(),
                });
            }
        });
        hits
    }
}

impl Rule for GetChildrenInLoop {
    fn id(&self) -> &'static str { "instance::get_children_in_loop" }
    fn severity(&self) -> Severity { Severity::Warn }

    fn check(&self, _source: &str, ast: &full_moon::ast::Ast) -> Vec<Hit> {
        let mut hits = Vec::new();
        visit::each_call(ast, |call, ctx| {
            if ctx.in_loop && (visit::is_method_call(call, "GetChildren") || visit::is_method_call(call, "GetDescendants")) {
                hits.push(Hit {
                    pos: visit::call_pos(call),
                    msg: ":GetChildren/:GetDescendants in loop allocates a new table each call - cache outside the loop".into(),
                });
            }
        });
        hits
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lint::Rule;

    fn parse(src: &str) -> full_moon::ast::Ast {
        full_moon::parse(src).unwrap()
    }

    #[test]
    fn property_before_parent_detected() {
        let src = "local p = Instance.new(\"Part\")\np.Parent = workspace\np.Size = Vector3.new(1,1,1)";
        let ast = parse(src);
        let hits = PropertyBeforeParent.check(src, &ast);
        assert_eq!(hits.len(), 1);
        assert!(hits[0].msg.contains("parent LAST"));
    }

    #[test]
    fn property_before_parent_correct_order() {
        let src = "local p = Instance.new(\"Part\")\np.Size = Vector3.new(1,1,1)\np.Parent = workspace";
        let ast = parse(src);
        let hits = PropertyBeforeParent.check(src, &ast);
        assert_eq!(hits.len(), 0);
    }

    #[test]
    fn repeated_find_first_child_detected() {
        let src = "local a = obj:FindFirstChild(\"Gun\")\nlocal b = obj:FindFirstChild(\"Gun\")";
        let ast = parse(src);
        let hits = RepeatedFindFirstChild.check(src, &ast);
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn different_find_first_child_not_flagged() {
        let src = "local a = obj:FindFirstChild(\"Gun\")\nlocal b = obj:FindFirstChild(\"Sword\")";
        let ast = parse(src);
        let hits = RepeatedFindFirstChild.check(src, &ast);
        assert_eq!(hits.len(), 0);
    }

    #[test]
    fn changed_on_part_detected() {
        let src = "local part = workspace.Part\npart.Changed:Connect(function() end)";
        let ast = parse(src);
        let hits = ChangedOnMovingPart.check(src, &ast);
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn bulk_property_set_detected() {
        let src = "part.Size = v\npart.Position = v\npart.Color = c\npart.Material = m\npart.Transparency = t\npart.Anchored = true";
        let ast = parse(src);
        let hits = BulkPropertySet.check(src, &ast);
        assert_eq!(hits.len(), 1);
        assert!(hits[0].msg.contains("6 consecutive"));
    }

    #[test]
    fn few_property_sets_not_flagged() {
        let src = "part.Size = v\npart.Position = v\npart.Color = c";
        let ast = parse(src);
        let hits = BulkPropertySet.check(src, &ast);
        assert_eq!(hits.len(), 0);
    }

    #[test]
    fn collection_service_in_loop_detected() {
        let src = "for _, part in parts do\n  part:AddTag(\"Tagged\")\nend";
        let ast = parse(src);
        let hits = CollectionServiceInLoop.check(src, &ast);
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn collection_service_outside_loop_ok() {
        let src = "part:AddTag(\"Tagged\")";
        let ast = parse(src);
        let hits = CollectionServiceInLoop.check(src, &ast);
        assert_eq!(hits.len(), 0);
    }

    #[test]
    fn name_indexing_in_loop_detected() {
        let src = "for i = 1, 10 do\n  local p = workspace.SpawnLocation\nend";
        let ast = parse(src);
        let hits = NameIndexingInLoop.check(src, &ast);
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn name_indexing_outside_loop_ok() {
        let src = "local p = workspace.SpawnLocation";
        let ast = parse(src);
        let hits = NameIndexingInLoop.check(src, &ast);
        assert_eq!(hits.len(), 0);
    }

    #[test]
    fn destroy_in_loop_detected() {
        let src = "for _, child in children do\n  child:Destroy()\nend";
        let ast = parse(src);
        let hits = DestroyInLoop.check(src, &ast);
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn destroy_outside_loop_ok() {
        let src = "part:Destroy()";
        let ast = parse(src);
        let hits = DestroyInLoop.check(src, &ast);
        assert_eq!(hits.len(), 0);
    }

    #[test]
    fn get_children_in_loop_detected() {
        let src = "for i = 1, 10 do\n  local c = folder:GetChildren()\nend";
        let ast = parse(src);
        let hits = GetChildrenInLoop.check(src, &ast);
        assert_eq!(hits.len(), 1);
    }

    #[test]
    fn get_children_outside_loop_ok() {
        let src = "local c = folder:GetChildren()";
        let ast = parse(src);
        let hits = GetChildrenInLoop.check(src, &ast);
        assert_eq!(hits.len(), 0);
    }
}