Skip to content

Commit ecfa70a

Browse files
committed
perf(scanner): skip empty lines, grow stringbuilder by default
this change decreases the total runtime for around 1.2k lines by 0.2-0.5ms
1 parent fe49303 commit ecfa70a

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

scanner/scanner.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,12 @@ func (s *Scanner) Parse() {
183183
// PERF: option no3:
184184
// 1.0ms, 1.2k lines, 4.5k token
185185
var res strings.Builder
186+
187+
// PERF: growing the string build to the len of the current line - the current pos on the line keeps go from having to grow the builder itself
188+
res.Grow(len(s.curLine) - int(s.linePos))
186189
out:
187190
for {
191+
// PERF: using a switch instead of a hashmap decreases the runtime by around 50% for large files
188192
switch s.curChar {
189193
case '\n', '!', '#', '_', '*', '-', '[', ']', '(', ')', '`', '>':
190194
break out
@@ -194,7 +198,10 @@ func (s *Scanner) Parse() {
194198
s.advance()
195199
}
196200

197-
s.addToken(TEXT, res.String())
201+
// skip empty texts
202+
if res.Len() != 0 {
203+
s.addToken(TEXT, res.String())
204+
}
198205

199206
// PERF: performing this here instead of in the next loop interation decreases execution time by around 0.1ms
200207
if s.curChar == '\n' {

0 commit comments

Comments
 (0)