src/rules/pedantic/chars.bend checks
raw source on the hub · import bolt@1.40.0.0/src/rules/pedantic/chars.bend as Chars
rule chars: a match with more than eight character-literal arms
(case '.':). Char is Chr{code: U32}, so such an arm is a U32 literal
inside a constructor pattern, and bend's C backend pays for each one in
build memory, a cost that compounds through the defs downstream of the one
holding them. It was out of all proportion once (bend 2.0.16: about 90 MB
an arm; the eighteen of bolt's lexer cost 1.57 GB and 8.3 s, 0.10 GB and
0.7 s once rewritten) and is modest now (bend 2.0.40: bolt builds in 1.3
to 1.4 GB with those eighteen arms against 1.1 GB without, in the same 33
to 35 s, and runs as fast; a lone match of eighteen costs 12 MB), which is
why the rule is pedantic: a match on char literals is the plain way to
write it, and the rewrite buys a fifth of the build's memory. It is the
literals, not the arms: a match over eighteen constructors costs nothing
measurable.
Compare Char.to_u32(c) instead. Bind the fallback above the comparisons
so it is a value, not a call, or bolt's own eager rule fires on it; and
give the def the reusable quantity (+c: Char), since the code point and
the fallback both consume it. Where the arms carry linear values, leave
the match alone: a cascade would break linearity and do every branch's
work.
Only a case's first match column is read, and only an arm whose pattern
opens with a character literal counts: case 'x': does, case Con{'x', t}:
and a literal in a later column of match a b: do not.
7 imports
import Base import ../../src.bend as Src import ../../finding.bend as F import ../../syntax/lex.bend as Lex import ../../syntax/tree.bend as Tree import ../../lazy/lazy.bend as Lazy import ../tokens.bend as T
Definitions
def literal source · line 34 · raw
@kids:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> U32
one when a case pattern opens with a character literal, else zero
def tally source · line 42 · raw
@body:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> @+nn:U32 -> U32
the character-literal arms among a match's statements
def report source · line 53 · raw
@+nn:U32 -> @+path:String -> @+ll:U32 -> @+cc:U32 -> List<&2, 0x90ede7cc80486efb15d2574b6f2abb28/src/finding.Finding>
a finding when the match holds more character literals than is cheap
def on_stmt source · line 60 · raw
@+kids:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> @+body:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> @+path:String -> List<&2, 0x90ede7cc80486efb15d2574b6f2abb28/src/finding.Finding>
a statement: a match is tallied
def walk source · line 66 · raw
@nn:0x90ede7cc80486efb15d2574b6f2abb28/src/syntax/tree.Node -> @+path:String -> List<&2, 0x90ede7cc80486efb15d2574b6f2abb28/src/finding.Finding>
every statement, at any depth
def check source · line 76 · raw
@ss:0x90ede7cc80486efb15d2574b6f2abb28/src/src.Src -> List<&2, 0x90ede7cc80486efb15d2574b6f2abb28/src/finding.Finding>
the rule