XO0911: a view of a parameter's text is stored and keeps the whole input alive
Reported by xo vet --cost. Slicing a Str or Bytes (s[a..b], trim, split, lines) makes a view of the same object, not a copy (decision 0330), so a small token kept in a struct, a map, or a returned list keeps the whole input alive for as long as it is held: 2,000 stored 16 byte tokens of 1 MiB request bodies hold 2.1 GB. Call clone() on the token where it is stored. Not noted: a slice used and dropped, a list that stays local, or storing the full text. The note cannot see how large the input is; a small input (a path, a flag) can keep the view.
Example
struct Tok {
name: Str
}
fn parse(text: Str) -> List[Tok] {
var out: List[Tok] = []
for line in text.lines() {
out.push(Tok{name: line[0..1]})
}
out
}Fix
struct Tok {
name: Str
}
fn parse(text: Str) -> List[Tok] {
var out: List[Tok] = []
for line in text.lines() {
out.push(Tok{name: line[0..1].clone()})
}
out
}Run xo explain XO0911 for this text in a terminal, or see
section 10.6 of the specification.