performance/eager_branch.bend checks
raw source on the hub · import bend-lint-rules@0.3.2.0/performance/eager_branch.bend as Eager_branch
performance/eager_branch: a value that Bend computes even when it is not needed, and that runs a loop
of the program to its end: a value of Bool.pick, or the right side of && or ||. These are
functions, so their arguments are computed before they choose; a match runs only its chosen case.
Heuristics: measured on the CPU, a Bool.pick whose unused value called a doubly recursive fib took 0.28
s, against 0.055 s for a match. clang drops an unused loop that compiles to one native loop: under
Bool.pick, a 2G-step tail loop cost nothing measurable, and under &&, a 400M-step one; the JS lane
still runs it. A tail loop that called Base's List.contains, which applies a closure, at each step did
not drop: 0.86 s for 40M steps, against 0.09 s for a match. So a call is reported when it is of a loop
that calls itself outside a tail position, or of one under which a parallel let, a closure's
application (not a ~ parameter's) or such a loop runs, or of a definition that calls such a loop,
through the program's files. A loop's own call in && or || is never a tail call. Base's
definitions are not seen: a call of one counts as one native loop, so a loop slow only through Base is
not reported. A call under a lambda counts, though it runs only when the lambda does. A value in a
lambda, a type or an equation does not run, and is skipped; an erased argument (a proof's) is not told
from a live one. A loop that is short in practice makes a finding that costs little to ignore.
5 imports
import Base import bend-lint-api@0.3.2.0/linter/api/impl.bend as LintImpl import bend-lint-api@0.3.2.0/linter/api/spec.bend as Lint import bend-lint-api@0.3.2.0/shared.bend as Shared import ./deep_call_in_fork.bend as Deep
Types
type Eager source · line 26 · raw
Data
A value Bend computes whether needed or not: what computes it ("pick", "&&" or "||"), and the value.
Eager@kind:String -> @value:Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> Eager
type Owner source · line 31 · raw
Data
What judging the calls in one definition reads: the definitions of the program a call of which runs a loop to its end (see costly), and the definition, as the check names them.
Owner@costly:Set -> @target:String -> Owner
Definitions
def eager source · line 38 · raw
@kids:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @+at:U32 -> String
Functions
=========
What an application with the nodes kids computes whether needed or not, as its node at at is one of its
arguments: "pick" for a value of Bool.pick, "&&" or "||" for their right side, "" for none.
def Eager.of source · line 62 · raw
@+kind:String -> @value:Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> Maybe<&2, Eager>
def eagerly source · line 67 · raw
@path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @found:Maybe<&2, Eager> -> Maybe<&2, Eager>
The eager value a call below path is in, if one is above it and no lambda comes between. found stops the walk
once one is found.
def why source · line 82 · raw
@+kind:String -> @+callee:String -> String
def message source · line 97 · raw
@kind:String -> @+callee:String -> @own:Bool -> String
def judged source · line 112 · raw
@eager:Maybe<&2, Eager> -> @callee:String -> @own:Bool -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>
def looping source · line 119 · raw
@hit:Bool -> @path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @callee:String -> @own:Bool -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>
def visit source · line 131 · 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 defs source · line 152 · raw
@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @+costly:Set -> @out:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding> -> List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>
def templates source · line 181 · raw
@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @out:List<&2, U32> -> List<&2, U32>
The binders of the ~ parameters among a definition's nodes, onto out: a call of one is inlined, not a closure's.
def applies.visit source · line 191 · raw
@+templates:List<&2, U32> -> @+path:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Above> -> @+node:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node -> List<&2, Unit>
A closure's application: of a variable that no ~ parameter (templates) binds.
def applies source · line 213 · raw
@+kids:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @found:Bool -> Bool
Whether a definition's nodes apply a closure, or found.
def unflat source · line 228 · raw
@nodes:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Node> -> @out:List<&2, String> -> List<&2, String>
The definitions among a file's nodes whose own body keeps them from compiling to a native loop, onto out: a
parallel let is in them, or a closure's application.
def program.unflat source · line 246 · raw
@sources:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Source> -> @out:List<&2, String> -> List<&2, String>
The definitions of the program whose own body keeps them from compiling to a native loop, onto out: those of
unflat, and those that call themselves outside a tail position.
def calls_itself source · line 253 · raw
@info:Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Def> -> Bool
def loops source · line 261 · raw
@names:List<&2, String> -> @+defs:Map<&2, Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Def>> -> @+unflat:Set -> @out:List<&2, String> -> List<&2, String>
The definitions among names that call themselves and are in unflat, onto out.
def costly source · line 289 · raw
@+sources:List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Source> -> @+defs:Map<&2, Maybe<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Def>> -> Set
The definitions of the program a call of which runs a loop to its end, even when its value is unused: a loop that does not compile to a native loop, as its body, or one it calls, keeps it from that (see program.unflat); or a definition that calls such a loop.
def run source · line 303 · raw
@+here:0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Here -> 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Ask(List<&2, 0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Finding>)
def rule source · line 313 · raw
0x94bdd21968f0b8ad8c1be0e7e2b606a0/linter/api/spec.Rule