~/bend-docscommunity

performance/loop_invariant_call.bend checks

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

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

Types

type Owner source · line 20 · 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 25 · raw

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

Functions =========

def loop.visit source · line 31 · raw

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

def loops source · line 35 · raw

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

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

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

def passed.at source · line 79 · raw

@arg:Maybe<&2, 0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.Node> -> @b:U32 -> Bool

def passed source · line 87 · raw

@calls:List<&2, List<&2, 0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.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 98 · raw

@nodes:List<&2, 0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.Node> -> @+calls:List<&2, List<&2, 0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.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 118 · raw

@loops:List<&2, String> -> @+node:0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.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 143 · raw

@+fuel:Nat -> @nodes:List<&2, 0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.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 165 · raw

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

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

def branch source · line 186 · raw

@path:List<&2, 0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.Above> -> @last:Maybe<&2, 0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.Node> -> Maybe<&2, 0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.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 195 · raw

@b:Maybe<&2, 0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.Node> -> @target:String -> Bool

def repeats.if source · line 203 · raw

@found:Bool -> @path:List<&2, 0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.Above> -> @target:String -> Bool

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

def visit source · line 210 · raw

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

def is_proof source · line 245 · raw

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

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

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

def defs source · line 267 · raw

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

def run source · line 294 · raw

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

def rule source · line 298 · raw

0xd69a1e32f167d67a827d0d362dfcf729/linter/lint.Rule