LYR-SEM0221
a large struct passed by value
A parameter's type is a struct of more than 64 bytes, counted without padding. Not said unless the lint is set to warn.
Explanation
A struct is a value: each call copies it whole into the parameter. Past 64 bytes the copy costs more than a reference would. The size the lint counts is a floor — no padding, a reference as one word — so a struct it names is at least as large as it says. The lint is allow by default.
Make the type a class, or pass the part of it the function needs.
Wrong
struct Frame {
a: int,
b: int,
c: int,
d: int,
e: int,
f: int,
g: int,
h: int,
i: int,
}
@Warn("large-value-copy")
fn total(frame: Frame): int {
return frame.a + frame.i;
}
fn main(): void {
let _ = total(Frame { a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8, i = 9 });
}
Right
struct Frame {
a: int,
b: int,
c: int,
d: int,
e: int,
f: int,
g: int,
h: int,
}
@Warn("large-value-copy")
fn total(frame: Frame): int {
return frame.a + frame.h;
}
fn main(): void {
let _ = total(Frame { a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7, h = 8 });
}