~/bend-docscommunity

performance/append_in_loop.bend checks

raw source on the hub · import bend-lint-rules@0.1.1.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.

3 imports
import Base
import bend-lint-api@0.1.1.0/linter/lint.bend as Lint
import bend-lint-api@0.1.1.0/shared/lists.bend as Lists

Types

type Append source · line 16 · raw

Data

A function that copies its first list or text to append to it.

type Loop source · line 21 · raw

Data

What judging the calls of a loop to itself reads: the loop's parameters, and the file's text.

Definitions

def Append.of source · line 28 · raw

@+callee:String -> @+count:Nat -> Maybe<&2, Append>

The append that a call of callee with count arguments is.

def Append.at source · line 37 · raw

@a:Append -> Nat

Which argument an append copies.

def Append.message source · line 45 · raw

@a:Append -> @loop:String -> @+acc:String -> @+source:String -> @+args:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.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 89 · raw

@nodes:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.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 100 · raw

@kept:Bool -> @a:Append -> @name:String -> @acc:String -> @span:0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Span -> @text:String -> @args:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Finding>

def accumulated source · line 118 · raw

@acc:Maybe<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> @a:Append -> @loop:Loop -> @name:String -> @span:0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Span -> @args:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> @before:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> @after:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.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 142 · raw

@append:Maybe<&2, Append> -> @+args:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> @loop:Loop -> @name:String -> @span:0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Span -> @before:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> @after:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Finding>

def argument source · line 168 · raw

@arg:0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node -> @loop:Loop -> @name:String -> @before:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> @after:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.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 196 · raw

@args:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> @+loop:Loop -> @+name:String -> @+before:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> @out:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Finding> -> List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.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 215 · raw

@loop:Loop -> @kids:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Finding>

def call source · line 222 · raw

@loop:Loop -> @node:0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node -> List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Finding>

def calls source · line 227 · raw

@recursive:Bool -> @loop:Loop -> @node:0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node -> List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Finding>

def visit source · line 234 · raw

@+loop:Loop -> @+path:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Above> -> @+node:0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node -> List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Finding>

def defs source · line 237 · raw

@nodes:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Node> -> @+text:String -> @out:List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Finding> -> List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Finding>

def run source · line 265 · raw

@+here:0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Here -> 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Ask(List<&2, 0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Finding>)

def rule source · line 268 · raw

0xbf17cd8a773b73dcdd93291552ca53f8/linter/lint.Rule