Lyricdev
GitHub

LYR-SEM0222

a value boxed as an interface in a loop

warning · spec 05-interfaces · lint boxed-interface-in-hot-loop, group perf, allow unless said

In a loop, a number or a struct or enum value is passed where the parameter's type is an interface. Not said unless the lint is set to warn.

Explanation

An interface value holds a reference: a value becomes one by being copied into a box on the heap, each time it is passed — in a loop, each round. The lint is allow by default.

Take a type parameter constrained by the interface (fn show<T :: [Display]>(d: T)), which takes the value as it is, or box it once before the loop.

Wrong

fn show(d: Display): string {
    return d.show();
}

@Warn("boxed-interface-in-hot-loop")
fn main(): void {
    for (i in 0..3) {
        let _ = show(i);
    }
}
fn show<T :: [Display]>(d: T): string {
    return d.show();
}

@Warn("boxed-interface-in-hot-loop")
fn main(): void {
    for (i in 0..3) {
        let _ = show(i);
    }
}