NL9xxx: performance warnings
The 21 codes of band 9. Diagnostics says what a code promises and how a rule's fragment is matched.
NL9001
The message of a warning --no-warn-performance turns off contains:
…allocates a dynamically sized array on every iteration of this loop…
nl9001_pattern_loop_allocation.ts:
// NL9001: a pattern over an array allocated on every iteration names the pattern as written.
export const main = (): number => {
let total = 0;
for (let i = 1; i < 4; i++) {
const [a, b] = new Array<i32>(i + 1);
total += a + b;
}
return total;
};
`[a, b]` allocates a dynamically sized array on every iteration of this loop and nothing keeps it past the iteration, so the arena grows once per pass: hoist the allocation above the loop and reuse it, or open the loop body with `using a = arena()`
NL9002
The message of a warning --no-warn-performance turns off contains:
…is rebuilt from its own value on every iteration of this loop
NL9003
The message of a warning --no-warn-performance turns off contains:
…already holds an allocation and this one drops it
NL9004
The message of a warning --no-warn-performance turns off contains:
…is at or beyond the
NL9005
The message of a warning --no-warn-performance turns off contains:
…is computed in i32 and can overflow it before
NL9006
The message of a warning --no-warn-performance turns off contains:
…this computes with overflow: the result
NL9007
The message of a warning --no-warn-performance turns off contains:
…is not proven to be in range for…
nl9007_guard_f64_mode.ts (compiled with --number-mode f64):
// Under `--number-mode f64` a length is an `f64`, so `i < xs.length` does not
// type-check against an `i32` index: NL9007 spells the bound
// `toI32(xs.length)`, which compiles in both modes and is credited.
export const sumAt = (xs: number[], idx: i32[]): number => {
let total = 0
for (const i of idx) {
total = total + xs[i]
}
return total
}
`i` is not proven to be in range for `xs` here, so this access keeps its bounds check and compares against the length on every iteration: guard it with a test that reaches the access — `if (i >= 0 && i < toI32(xs.length))` proves both ends, and an unsigned index needs only the upper one
nl9007_guard_f64_shadowed_to_i32.ts (compiled with --number-mode f64):
// Under `--number-mode f64` the credited guard for an `i32` index calls the
// builtin `toI32`, which this module's own `toI32` hides: NL9007 offers no
// guard spelling.
const toI32 = (x: number): number => x
export const sumAt = (xs: number[], idx: i32[]): number => {
let total = toI32(0)
for (const i of idx) {
total = total + xs[i]
}
return total
}
`i` is not proven to be in range for `xs` here, so this access keeps its bounds check and compares against the length on every iteration: no guard removes the check here, because the one the compiler credits for this index calls the builtin `toI32`, which this module's own `toI32` hides: rename it to guard the access
// An `i64` index has no guard the bounds analysis credits: `i < xs.length`
// does not type-check against it, and a `toI64(xs.length)` bound is not read.
// NL9007 offers no guard spelling.
export const sumAt = (xs: i32[], idx: i64[]): i32 => {
let total = 0
for (const i of idx) {
total = total + xs[i]
}
return total
}
`i` is not proven to be in range for `xs` here, so this access keeps its bounds check and compares against the length on every iteration: no guard removes the check on an index of type `i64`, because the compiler credits no compare of it with the length: an `i32` index, or a `u8`, `u16` or `u32` one, has a guard that does
nl9007_guard_shadowed_to_u32.ts:
// The module declares its own `toU32`, so the credited guard for a `u32`
// index, `toU32(i) < toU32(xs.length)`, would call it rather than the
// builtin. NL9007 offers no guard spelling.
const toU32 = (x: i32): i32 => x
export const sumAt = (xs: i32[], idx: u32[]): i32 => {
let total = toU32(0)
for (const i of idx) {
total = total + xs[i]
}
return total
}
`i` is not proven to be in range for `xs` here, so this access keeps its bounds check and compares against the length on every iteration: no guard removes the check here, because the one the compiler credits for this index calls the builtin `toU32`, which this module's own `toU32` hides: rename it to guard the access
// A `u16` index is offered the credited `u32` compare, its type the lower end.
export const sumAt = (xs: i32[], idx: u16[]): i32 => {
let total = 0
for (const i of idx) {
total = total + xs[i]
}
return total
}
`i` is not proven to be in range for `xs` here, so this access keeps its bounds check and compares against the length on every iteration: guard it with a test that reaches the access — `if (toU32(i) < toU32(xs.length))` proves it, because an unsigned index needs only the upper end
// A `u64` index has no guard the bounds analysis credits: `i < xs.length`
// does not type-check against it, `toU32` would keep its low 32 bits, and a
// `toU64(xs.length)` bound is not read. NL9007 offers no guard spelling.
export const sumAt = (xs: i32[], idx: u64[]): i32 => {
let total = 0
for (const i of idx) {
total = total + xs[i]
}
return total
}
`i` is not proven to be in range for `xs` here, so this access keeps its bounds check and compares against the length on every iteration: no guard removes the check on an index of type `u64`, because the compiler credits no compare of it with the length: an `i32` index, or a `u8`, `u16` or `u32` one, has a guard that does
// An offset index that keeps its check: NL9007 names the sum and a guard on
// it, which the bounds analysis credits.
export const pairSums = (xs: i32[], ys: i32[]): i32 => {
let total = 0
for (let i = 0; i + 1 < xs.length; i++) {
total = total + ys[i + 1]
}
return total
}
`i + 1` is not proven to be in range for `ys` here, so this access keeps its bounds check and compares against the length on every iteration: guard the sum with a test that reaches the access — `if (i >= 0 && i + 1 < ys.length)` proves both ends, and so does a loop condition `i + 1 < ys.length` on a counter that starts at 0
nl9007_offset_f64_mode.ts (compiled with --number-mode f64):
// Under `--number-mode f64` a length is an `f64`, so the guard NL9007 names
// for an offset index spells the bound `toI32(ys.length)`, which compiles in
// both modes and is credited.
export const pairSums = (xs: number[], ys: number[]): number => {
let total = 0
for (let i: i32 = 0; i + 1 < toI32(xs.length); i++) {
total = total + ys[i + 1]
}
return total
}
`i + 1` is not proven to be in range for `ys` here, so this access keeps its bounds check and compares against the length on every iteration: guard the sum with a test that reaches the access — `if (i >= 0 && i + 1 < toI32(ys.length))` proves both ends, and so does a loop condition `i + 1 < toI32(ys.length)` on a counter that starts at 0
NL9008
The message of a warning --no-warn-performance turns off contains:
…is called here inside a loop and
NL9009
The message of a warning --no-warn-performance turns off contains:
…is not provably within
NL9010
The message of a warning --no-warn-performance turns off contains:
…are padding the alignment rules insert and nothing reads
NL9011
The message of a warning --no-warn-performance turns off contains:
…leaves arena memory behind on every pass of this loop…
// NL9011: a loop over a call that leaves arena memory behind, where neither a
// scope around each pass nor one around the function takes it back, names
// what refused each of them.
class Log {
last: string = "";
}
const digits = (n: i32): string => `${n}`;
export const count = (log: Log, rounds: i32): i32 => {
let total = 0;
for (let i = 0; i < rounds; i++) {
total += digits(i).length;
log.last = `${i}`;
}
return total;
};
`digits` leaves arena memory behind on every pass of this loop, which cannot release it after each pass because the allocation on line 14 is stored into memory, where this analysis stops following it, and neither can `count` when it returns, so all of it lives as long as the caller's memory does. Keep what a pass allocates out of locals declared outside the loop and out of memory older than the pass, or open the loop body with `using a = arena()`
NL9012
The message of a warning --no-warn-performance turns off contains:
…` but allocates on every call, so each thread marks and releases its arena around every element. Compute the answer without building a string, an array or an object to save both
// NL9012: a parallel body that allocates compiles, recycled per element, and
// the call says what every element pays for it.
import { parallelReduce } from "nish/threads";
const widest = (a: i32, b: i32): i32 => (`${a}`.length > `${b}`.length ? a : b);
export const main = (): i32 => parallelReduce([7, 42, 1000], widest, 0);
the body of this `parallelReduce` allocates per element: `widest` answers `i32` but allocates on every call, so each thread marks and releases its arena around every element. Compute the answer without building a string, an array or an object to save both
NL9013
The message of a warning --no-warn-performance turns off contains:
…so entering the range keeps its check on every iteration of this loop…
// NL9013: a value entering a range inside a loop, with nothing that proves it
// already lies there, keeps its check on every pass and names the guard that
// would prove it.
export const total = (xs: i32[]): i32 => {
let sum = 0
for (let k = 0; k < xs.length; k++) {
const slot: integer<0, 63> = k
sum = sum + slot
}
return sum
}
`k` is not proven to lie in integer<0, 63> here, so entering the range keeps its check on every iteration of this loop: guard it with a test that reaches it — `if (k >= 0 && k <= 63)` proves it
NL9014
The message of a warning --no-warn-performance turns off contains:
…--unchecked-indexing is deprecated and reaches only the modules of the entry package, never a dependency or the standard library: call `uncheckedGet` and `uncheckedSet` from `
Retired: the note that --unchecked-indexing reaches only the entry package, removed with the flag; the CLI refuses it with exit 2
NL9015
The message of a warning --no-warn-performance turns off contains:
…--wrapping is deprecated and reaches only the modules of the entry package, never a dependency or the standard library: call `wrappingAdd`, `wrappingSub` and `wrappingMul` from `
Retired: the note that --wrapping reaches only the entry package, removed with the flag; the CLI refuses it with exit 2 and wrappingAdd, wrappingSub and wrappingMul from nish:unsafe wrap
NL9016
The message of a warning --no-warn-performance turns off contains:
…drops the one the pass before gave it…
// NL9016: a local declared outside a loop and given a new allocation on every
// pass drops the value the pass before gave it.
export const lastLine = (rows: i32): i32 => {
let last = "";
for (let i = 0; i < rows; i++) {
last = `row ${i}`;
}
return last.length;
};
`last` is given a new allocation on every pass of this loop and drops the one the pass before gave it: nothing can reach that value again and nothing frees it while the loop runs, so memory grows with every pass. Declare `last` with `const` inside the loop when only its pass reads it, or keep what the last pass needs as numbers and build the value once after the loop
NL9017
The message of a warning --no-warn-performance turns off contains:
… functions, more than eight, so it branches on a `switch` of them where a direct call would not…
// NL9017: a call through a reference whose set holds more than eight
// functions branches on a `switch` of them where a direct call would not.
const f0 = (x: i32): i32 => {
return x + 0;
};
const f1 = (x: i32): i32 => {
return x + 1;
};
const f2 = (x: i32): i32 => {
return x + 2;
};
const f3 = (x: i32): i32 => {
return x + 3;
};
const f4 = (x: i32): i32 => {
return x + 4;
};
const f5 = (x: i32): i32 => {
return x + 5;
};
const f6 = (x: i32): i32 => {
return x + 6;
};
const f7 = (x: i32): i32 => {
return x + 7;
};
const f8 = (x: i32): i32 => {
return x + 8;
};
export const main = (): i32 => {
const ops: ((x: i32) => i32)[] = [f0, f1, f2, f3, f4, f5, f6, f7, f8];
let total = 0;
for (const op of ops) {
total = total + op(2);
}
return total;
};
this call through `op` can reach 9 functions, more than eight, so it branches on a `switch` of them where a direct call would not; they came in through `ops: ((x: i32) => i32)[] = [f0, f1, f2, f3, f4, f5, f6, f7, f8]` (nl9017_fnref_wide_set.ts:40)
NL9018
The message of a warning --no-warn-performance turns off contains:
…and nothing bounds how many passes it runs…
// NL9018: a loop that nothing bounds keeps what each pass allocates, because
// each pass stores into a structure older than the loop.
class Last {
n: i32;
constructor(n: i32) {
this.n = n;
}
}
class Tally {
last: Last;
total: i32;
open: boolean;
constructor() {
this.last = new Last(0);
this.total = 0;
this.open = true;
}
}
export const drain = (tally: Tally, budget: i32): void => {
let k = 0;
while (tally.open) {
const line = `line ${k}`;
tally.total = tally.total + line.length;
tally.last = new Last(k);
k = k + 1;
tally.open = k < budget;
}
};
this loop keeps what each pass allocates until the memory of `drain`'s caller is released, because the allocation on line 26 is stored into memory, where this analysis stops following it, and nothing bounds how many passes it runs, so the string built on line 24 piles up for as long as it runs. Move the per-pass work that builds it into a block inside the loop body that opens with `using a = arena()`, which the compiler refuses if anything allocated there could outlive the block
NL9019
The message of a warning --no-warn-performance turns off contains:
…keeps an allocation on every call and runs on every pass of a loop that releases nothing…
// NL9019: a function a loop runs on every pass keeps an allocation each call
// (a `let` given a second string), and the loop's passes release nothing.
const relabel = (n: i32): i32 => {
let s = "none";
if (n > 2) {
s = `b${n}`;
}
return s.length;
};
export const labels = (rows: i32): i32 => {
const out: string[] = [];
for (let i = 0; i < rows; i++) {
out.push(`${relabel(i)}`);
}
return out.length;
};
`relabel` keeps an allocation on every call and runs on every pass of a loop that releases nothing, so the memory it keeps grows with every pass: the first allocation it holds is on line 6, where this analysis does not prove it dead when the function returns (a local assigned more than once, or an array of its own). Give each value its own `const`, open a block with `using a = arena()` around the work, or keep what must outlive the call in a region
NL9020
The message of a warning --no-warn-performance turns off contains:
…beside the compatibility dialect: a bare `number` is the 32-bit integer
NL9021
The message of a warning --no-warn-performance turns off contains:
…` is never `null` or `undefined`, so `??` never runs its default: the build keeps the left operand alone (drop `?? …
nl9021_nullish_never_missing.ts (compiled with --compat=nullish):
// Under `--compat=nullish` a `??` whose left operand can never be missing
// compiles to that operand, as Node computes it. NL9021 names the default the
// build drops, so the program can say what it means.
const f = (a: number): number => a ?? 1
export const main = (): void => {
console.log(f(4))
}
`a` is never `null` or `undefined`, so `??` never runs its default: the build keeps the left operand alone (drop `?? 1`)

