Xo is experimental. The source is now public on GitHub, or try Xo in your browser.
Menu · XO0914

Docs

XO0914: a small list allocated and freed every iteration

note · reported by the vet --cost

Reported by xo vet --cost. Each list literal or with_capacity allocates; a small one made every iteration costs an allocation and a free each time. Keep the values in locals, or make one list before the loop and reuse it: std path’s clean in eight locals instead of an offsets list ran in 0.927 of the time (knowledgebase/std-performance-guide.md 3).

Example

fn sum(xs: List[Int]) -> Int {
  var t = 0
  for x in xs {
    let q = [x, x * 2]
    t += q[0] + q[1]
  }
  t
}

Fix

fn sum(xs: List[Int]) -> Int {
  var t = 0
  for x in xs {
    let (a, b) = (x, x * 2)
    t += a + b
  }
  t
}

Run xo explain XO0914 for this text in a terminal, or see section 10.6 of the specification.