performance/append_in_loop.bend checks
raw source on the hub · import bend-lint-rules@0.3.7.0/performance/append_in_loop.bend as Append_in_loop
performance/append_in_loop: a loop that appends to its accumulator. A definition passes
List.append(.., acc, ..) or acc ++ .. to a call of itself, where acc is one of its parameters.
Heuristics: an append copies acc, so a loop of n steps copies O(n²) items, where adding at the head
copies none. ++ copies only on the C lane: the JS lane joins strings natively. A loop that also
keeps each value it builds, by passing acc in another argument too, needs the copies: it is not
reported. It knows Base's List.append and String.append (++), not other functions that copy
their first argument.
4 imports
import Base import bend-lint-api@0.3.7.0/linter/api/impl.bend as LintImpl import bend-lint-api@0.3.7.0/linter/api/spec.bend as Lint import bend-lint-api@0.3.7.0/shared.bend as Shared
Types
type Append source · line 17 · raw
Data
A function that copies its first list or text to append to it.
ListAppendAppend
StringAppendAppend
type Loop source · line 22 · raw
Data
What judging the calls of a loop to itself reads: the loop's parameters, and the file's text.
Loop@params:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @text:String -> Loop
Definitions
def Append.of source · line 29 · raw
@+callee:String -> @+count:Nat -> Maybe<&2, Append>
The append that a call of callee with count arguments is.
def Append.at source · line 42 · raw
@a:Append -> Nat
Which argument an append copies.
def Append.message source · line 50 · raw
@a:Append -> @loop:String -> @+acc:String -> @+source:String -> @+args:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> String
What a finding says about an append of acc in the loop loop. args are the append's arguments.
def has source · line 94 · raw
@nodes:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @+b:U32 -> @found:Bool -> Bool
Whether a variable the file writes, of the binder b, is among nodes or under them, or found.
def judged source · line 105 · raw
@kept:Bool -> @a:Append -> @name:String -> @acc:String -> @span:0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Span -> @text:String -> @args:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>
def accumulated source · line 123 · raw
@acc:Maybe<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @a:Append -> @loop:Loop -> @name:String -> @span:0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Span -> @args:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @before:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @after:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>
The finding about an append (a, at span, with args) to acc, if it is a parameter of the loop name that its
call does not also use (in before and after, its nodes but the append): then it keeps each value the loop builds,
and the copies are needed.
def appended source · line 147 · raw
@append:Maybe<&2, Append> -> @+args:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @loop:Loop -> @name:String -> @span:0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Span -> @before:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @after:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>
def argument source · line 173 · raw
@arg:0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node -> @loop:Loop -> @name:String -> @before:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @after:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>
The findings about an argument of a call of the loop name, if it appends; before and after are the call's other
nodes.
def arguments source · line 201 · raw
@args:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @+loop:Loop -> @+name:String -> @+before:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @out:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding> -> List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>
The findings about the arguments args of a call of the loop name, after the call's nodes before (last first).
def call.of source · line 220 · raw
@loop:Loop -> @kids:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>
def call source · line 227 · raw
@loop:Loop -> @node:0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node -> List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>
def calls source · line 232 · raw
@recursive:Bool -> @loop:Loop -> @node:0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node -> List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>
def visit source · line 239 · raw
@+loop:Loop -> @+path:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Above> -> @+node:0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node -> List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>
def defs source · line 242 · raw
@nodes:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Node> -> @+text:String -> @out:List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding> -> List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>
def run source · line 270 · raw
@+here:0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Here -> 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Ask(List<&2, 0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Finding>)
def rule source · line 273 · raw
0x534e77ab9e7d8d3c817c84cab8139f38/linter/api/spec.Rule