AppendInLoop.Append.message: def of bend-lint-rules@0.2.0.0 Import: import bend-lint-rules@0.2.0.0/performance/append_in_loop.bend as AppendInLoop Lines 44-86 of bend-lint-rules@0.2.0.0/performance/append_in_loop.bend: https://jend.leeeet.dev/read.txt?path=bend-lint-rules@0.2.0.0/performance/append_in_loop.bend&start_line=44&end_line=86 import Base import bend-lint-api@0.2.0.0/linter/lint.bend as Lint # What a finding says about an append of `acc` in the loop `loop`. `args` are the append's arguments. def Append.message( a: Append, loop: String, +acc: String, +source: String, +args: List<&2, Lint.Node> ) -> String: match a: case ListAppend{}: +q = Lint.Span.text(source, Lint.Node.span(List.get(&2, Lint.Node, args, 0n))) +t = Lint.Span.text(source, Lint.Node.span(List.get(&2, Lint.Node, args, 1n))) "List.append copies " ++ acc ++ " on each call of " ++ loop ++ ", so a loop of n steps copies O(n²) items. All lanes. Confidence: high, as the copies grow with the square of the steps. Add each item at the head instead, with `item <> " ++ acc ++ "`, and reverse " ++ acc ++ " once when the loop ends: `List.reverse(" ++ q ++ ", " ++ t ++ ", " ++ acc ++ ")`; to add a list each step, collect the lists, with `piece <> pieces`, and join them once: `List.concat(" ++ q ++ ", " ++ t ++ ", List.reverse(" ++ q ++ ", List<" ++ q ++ ", " ++ t ++ ">, pieces))`. If the loop runs only a few steps, expect this finding." case StringAppend{}: "++ copies " ++ acc ++ " on each call of " ++ loop ++ ", so a loop of n steps copies O(n²) characters. CPU and GPU: measured 12.6 s for 20,000 lines, against 0.07 s after the fix. JS: strings join natively, at no cost. Confidence: high, as the copies grow with the square of the steps. Collect the pieces in a list instead, with `piece <> pieces`, and join them once when the loop ends: `String.concat(List.reverse(&2, String, pieces))`. If the loop runs only a few steps, expect this finding." Uses (8): https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=List https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=List.get https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=String https://jend.leeeet.dev/definition.txt?path=Base/base.bend&name=String.append https://jend.leeeet.dev/definition.txt?path=bend-lint-api@0.2.0.0/linter/lint.bend&name=Node https://jend.leeeet.dev/definition.txt?path=bend-lint-api@0.2.0.0/linter/lint.bend&name=Node.span https://jend.leeeet.dev/definition.txt?path=bend-lint-api@0.2.0.0/linter/lint.bend&name=Span.text https://jend.leeeet.dev/definition.txt?path=bend-lint-rules@0.2.0.0/performance/append_in_loop.bend&name=Append