src/rules/perf/strings.bend checks
raw source on the hub · import bolt@1.50.0.0/src/rules/perf/strings.bend as Strings
rule strings: a match whose arms are string literals (case "foo":) with
more than 64 literal characters in all. Each string arm lowers into nested
per-char tests, and the time and memory of a check and of a build grow
with the characters (bend 2.0.40, a native build: 45 characters take 0.9 s
and 0.20 GB, 100 take 1.3 s and 0.28 GB, 480 take 7.8 s and 1.1 GB, 1,000
take 13.7 s and 1.8 GB, where the same strings compared with String.eq
take 0.6 to 0.7 s and 0.13 GB at every size; the check alone is 2.6 s and
0.49 GB at 480. On bend 2.0.16 the check at 480 was 12 s and 4.8 GB;
16 long event names are enough to reach it).
The number of arms barely matters, so only the characters count. Classify
the string once through a lookup table into a sum type, and match on that.
Only a case's first match column is read: string literals in a later
column of match a b: do not count. A literal with no closing quote (an
unterminated string) is not a string arm.
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
Types
type Tally source · line 24 · raw
Data
the string arms of a match, and their characters
Tally@arms:U32 -> @chars:U32 -> Tally
Definitions
def closes source · line 30 · raw
@cs:List<&2, Char> -> @+esc:Bool -> @+shut:Bool -> Bool
does the text end on a closing quote, its escapes read as a backslash and the one char after it? esc: the char before was an unescaped backslash; shut: it was an unescaped quote
def opened source · line 39 · raw
@cs:List<&2, Char> -> Bool
past its opening char, do the chars end on a closing quote?
def terminated source · line 48 · raw
@+tt:String -> Bool
is a string token closed: past its opening quote, does it end on a quote no
backslash escapes? "\", an unclosed string at the end of the file, is not
def literal source · line 53 · raw
@kids:0xb639465bf787ad19500fd0ac3a14266d/src/syntax/tree.Node -> Maybe<&2, U32>
the characters of the string a case pattern opens with, if it does and the string is closed
def one source · line 62 · raw
@mm:Maybe<&2, U32> -> U32
one for a string arm, else zero
def size source · line 70 · raw
@mm:Maybe<&2, U32> -> U32
a string arm's characters, else zero
def tally source · line 78 · raw
@body:0xb639465bf787ad19500fd0ac3a14266d/src/syntax/tree.Node -> @+nn:U32 -> @+chars:U32 -> Tally
the string arms among a match's statements
def report source · line 91 · raw
@tt:Tally -> @+path:String -> @+ll:U32 -> @+cc:U32 -> List<&2, 0xb639465bf787ad19500fd0ac3a14266d/src/finding.Finding>
a finding when the match's strings are too long in all
def on_stmt source · line 99 · raw
@+kids:0xb639465bf787ad19500fd0ac3a14266d/src/syntax/tree.Node -> @+body:0xb639465bf787ad19500fd0ac3a14266d/src/syntax/tree.Node -> @+path:String -> List<&2, 0xb639465bf787ad19500fd0ac3a14266d/src/finding.Finding>
a statement: a match is tallied
def walk source · line 105 · raw
@nn:0xb639465bf787ad19500fd0ac3a14266d/src/syntax/tree.Node -> @+path:String -> List<&2, 0xb639465bf787ad19500fd0ac3a14266d/src/finding.Finding>
every statement, at any depth
def check source · line 115 · raw
@ss:0xb639465bf787ad19500fd0ac3a14266d/src/src.Src -> List<&2, 0xb639465bf787ad19500fd0ac3a14266d/src/finding.Finding>
the rule