performance/list_index_loop.bend checks
raw source on the hub · import bend-lint-rules@0.3.6.0/performance/list_index_loop.bend as List_index_loop
performance/list_index_loop: a loop that reads a list from its head on each step. A definition passes
a list parameter unchanged to a call of itself, and, in the same branch, gives it to List.get,
List.drop, List.take, List.last or List.length, or to a definition of the file that gives its
parameter to one of those.
Heuristics: each of those walks the list from its head, so a loop of n steps over a list of n items
takes O(n²) time, where walking the list takes O(n). A branch is the innermost case around the
read; the loop's call must be in it too. It looks one definition deep, not further. A read in a type
does not run, and is skipped. A read whose result is an argument of a call of the loop itself moves
the loop along the list (a step, or a split in two: bend-ml's tensor-array fills a tree of bands so),
and is not reported.
4 imports
import Base import bend-lint-api@0.3.6.0/linter/api/impl.bend as LintImpl import bend-lint-api@0.3.6.0/linter/api/spec.bend as Lint import bend-lint-api@0.3.6.0/shared.bend as Shared
Types
type Walker source · line 21 · raw
Data
A function that walks a list from its head (named as the check names it): which argument is the list, and the Base function that walks it.
Walker@at:Nat -> @through:String -> Walker
type Loop source · line 25 · raw
Data
What judging the calls in one definition reads: the walkers of the file, and the definition.
Loop@walkers:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/shared.Names.Named<Walker>> -> @owner:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> Loop
Definitions
def bases source · line 30 · raw
List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/shared.Names.Named<Walker>>
Functions =========
def binder source · line 40 · raw
@node:Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> Maybe<&2, U32>
The binder of the variable node uses, if it is a variable.
def Walker.at source · line 47 · raw
@k:Maybe<&2, Nat> -> @target:String -> @through:String -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/shared.Names.Named<Walker>>
def Walker.helped source · line 58 · raw
@walked:Maybe<&2, U32> -> @through:String -> @owner:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/shared.Names.Named<Walker>>
def Walker.helper source · line 70 · raw
@base:Maybe<&2, Walker> -> @args:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @owner:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/shared.Names.Named<Walker>>
The walker owner is, if a call in it of a Base walker (base, with args) walks one of its parameters.
def Walker.visit source · line 81 · raw
@+owner:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> @+path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @+node:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/shared.Names.Named<Walker>>
def Walker.helpers source · line 99 · raw
@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @out:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/shared.Names.Named<Walker>> -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/shared.Names.Named<Walker>>
The definitions among nodes that give a parameter to a Base walker, as walkers, onto out.
def is_passed source · line 121 · raw
@args:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @+b:U32 -> @found:Bool -> Bool
Whether a variable of the binder b is among args, or found.
def is_passed.if source · line 133 · raw
@called:Bool -> @args:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @b:U32 -> Bool
Whether args, the arguments of a call of target (if called), pass the binder b unchanged.
def passes source · line 141 · raw
@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @+target:String -> @+b:U32 -> @found:Bool -> Bool
Whether a call of target among nodes, or under them, passes the binder b unchanged, or found.
def innermost source · line 162 · raw
@path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @owner:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node
The innermost case above path, if any: a call of the loop must be in it to pass the list on in the same branch.
def advice source · line 171 · raw
@through:String -> @list:String -> String
def message source · line 179 · raw
@+callee:String -> @+through:String -> @+list:String -> @loop:String -> String
def loops source · line 195 · raw
@param:Maybe<&2, Nat> -> @path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @owner:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> @target:String -> @b:U32 -> Bool
Whether the loop owner, if a parameter of it declares the binder b (param), passes it unchanged to a call of
itself (target), in the innermost case above path.
def feeds source · line 209 · raw
@path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @target:String -> Bool
Whether the call below path is an argument of a call of target, the loop itself.
def judged.list source · line 230 · raw
@walked:Maybe<&2, U32> -> @list:String -> @callee:String -> @through:String -> @+owner:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> @+path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @span:Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Span> -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>
The finding about a walker's call, below path, of the list list with the binder walked that the loop owner
passes unchanged to a call of itself in the same branch.
def judged source · line 259 · raw
@walker:Maybe<&2, Walker> -> @+args:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @callee:String -> @owner:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> @path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @span:Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Span> -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>
The finding about a call of the walker walker, with args.
def call source · line 281 · raw
@recursive:Bool -> @+loop:Loop -> @path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @node:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>
def visit source · line 302 · raw
@+loop:Loop -> @+path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @+node:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>
def defs source · line 305 · raw
@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @+walkers:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/shared.Names.Named<Walker>> -> @out:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding> -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>
def run source · line 328 · raw
@+here:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Here -> 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Ask(List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>)
def rule source · line 343 · raw
0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Rule