~/bend-docscommunity

performance/loop_invariant_call.bend checks

raw source on the hub · import bend-lint-rules@0.3.5.0/performance/loop_invariant_call.bend as Loop_invariant_call

performance/loop_invariant_call: a call that gets the same arguments on every step of a loop, so it computes the same value each time: in a definition that calls itself, a call of a definition of the file that loops too, in the branch that repeats (the one with the loop's call of itself), whose arguments are literals, or parameters that the loop passes unchanged to each call of itself. Heuristics: clang hoists plain arithmetic out of a loop (and folds constants), but not a call of a definition that loops: Bend runs it through its own calls. Compute it once, before the loop, and pass it as a parameter. A definition that does not call itself is cheap, and not reported. A proof (a definition that fills a law, or returns an equality) does not run, and is skipped. A loop that repeats work on purpose, such as a benchmark's, is the case to expect.

4 imports
import Base
import bend-lint-api@0.3.5.0/linter/api/impl.bend as LintImpl
import bend-lint-api@0.3.5.0/linter/api/spec.bend as Lint
import bend-lint-api@0.3.5.0/shared.bend as Shared

Types

type Owner source · line 21 · raw

Data

What judging the calls of one definition reads: the definitions of the file that call themselves (as the check names them), the definition (as the check names it, and as written), and its parameters that it passes unchanged to each call of itself, by binder.

Definitions

def message source · line 26 · raw

@callee:String -> @loop:String -> String

Functions =========

def loop.visit source · line 32 · raw

@+u:Unit -> @+path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @+node:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> List<&2, Unit>

def loops source · line 36 · raw

@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @out:List<&2, String> -> List<&2, String>

The definitions among nodes that call themselves, as the check names them, onto out.

def self.visit source · line 69 · raw

@+u:Unit -> @+path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @+node:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> List<&2, List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node>>

The arguments of each call of the definition below path to itself.

def passed.at source · line 80 · raw

@arg:Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @b:U32 -> Bool

def passed source · line 88 · raw

@calls:List<&2, List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node>> -> @+at:Nat -> @+b:U32 -> @ok:Bool -> Bool

Whether the argument at at of each of calls is the variable of b.

def fixed source · line 99 · raw

@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @+calls:List<&2, List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node>> -> @+at:Nat -> @out:List<&2, U32> -> List<&2, U32>

The runtime parameters among a definition's nodes that it passes unchanged to each of calls (its calls of itself), by binder; at counts the runtime parameters, as the calls' arguments do.

def Owner.of source · line 119 · raw

@loops:List<&2, String> -> @+node:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> Owner

What judging the calls of the definition node reads, with loops, the definitions of the file that call themselves.

def invariant source · line 144 · raw

@+fuel:Nat -> @nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @+fixed:List<&2, U32> -> @ok:Bool -> Bool

Whether nodes, the arguments of a call, are the same on every step: literals, the variables of fixed, and calls, annotations or constructions of those alone; fuel bounds the depth.

def has_call source · line 166 · raw

@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @+target:String -> @found:Bool -> Bool

Whether a call of target is among nodes or under them, or found.

def branch source · line 187 · raw

@path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @last:Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node>

The branch a node below path is in: the innermost case above it, or the definition's body (the last of path).

def repeats source · line 196 · raw

@b:Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @target:String -> Bool

def repeats.if source · line 204 · raw

@found:Bool -> @path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @target:String -> Bool

Whether the branch a node below path is in calls target again, if found.

def visit source · line 211 · raw

@+owner:Owner -> @+path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @+node:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>

def is_proof source · line 246 · raw

@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> Bool

Whether a definition's nodes say it is a proof: it has no return type written (it fills a law), or it returns an equality.

def defs.if source · line 261 · raw

@skipped:Bool -> @loops:List<&2, String> -> @+owner:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>

The findings in the definition owner, unless it is skipped.

def defs source · line 268 · raw

@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @+loops:List<&2, String> -> @out:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding> -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>

def run source · line 295 · raw

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

def rule source · line 299 · raw

0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Rule