Skip to content

Commit 597a774

Browse files
committed
scanner: speed up Scanner.skip_whitespace (~2-3% speed up of -o x.c cmd/v)
1 parent d3f2d6f commit 597a774

File tree

4 files changed

+29
-39
lines changed

4 files changed

+29
-39
lines changed

vlib/builtin/string.v

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ pub fn (s string) ends_with(p string) bool {
952952
return false
953953
}
954954
for i in 0 .. p.len {
955-
if p[i] != s[s.len - p.len + i] {
955+
if unsafe { p.str[i] != s.str[s.len - p.len + i] } {
956956
return false
957957
}
958958
}
@@ -1087,12 +1087,13 @@ pub fn (s string) find_between(start string, end string) string {
10871087
}
10881088

10891089
// is_space returns `true` if the byte is a white space character.
1090-
// The following list is considered white space characters: ` `, `\n`, `\t`, `\v`, `\f`, `\r`, 0x85, 0xa0
1090+
// The following list is considered white space characters: ` `, `\t`, `\n`, `\v`, `\f`, `\r`, 0x85, 0xa0
10911091
// Example: assert byte(` `).is_space() == true
1092+
[inline]
10921093
pub fn (c byte) is_space() bool {
1093-
// 0x0085 is NEXT LINE (NEL)
1094-
// 0x00a0 is NO-BREAK SPACE
1095-
return c in [` `, `\n`, `\t`, `\v`, `\f`, `\r`, 0x85, 0xa0]
1094+
// 0x85 is NEXT LINE (NEL)
1095+
// 0xa0 is NO-BREAK SPACE
1096+
return c == 32 || (c > 8 && c < 14) || (c == 0x85) || (c == 0xa0)
10961097
}
10971098

10981099
// trim_space strips any of ` `, `\n`, `\t`, `\v`, `\f`, `\r` from the start and end of the string.

vlib/math/mathutil/mathutil.v

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,15 @@ module mathutil
55

66
[inline]
77
pub fn min<T>(a T, b T) T {
8-
if a < b {
9-
return a
10-
} else {
11-
return b
12-
}
8+
return if a < b { a } else { b }
139
}
1410

1511
[inline]
1612
pub fn max<T>(a T, b T) T {
17-
if a > b {
18-
return a
19-
} else {
20-
return b
21-
}
13+
return if a > b { a } else { b }
2214
}
2315

2416
[inline]
2517
pub fn abs<T>(a T) T {
26-
if a > 0 {
27-
return a
28-
} else {
29-
return -a
30-
}
18+
return if a > 0 { a } else { -a }
3119
}

vlib/v/scanner/scanner.v

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const (
1717
double_quote = `"`
1818
// char used as number separator
1919
num_sep = `_`
20+
b_lf = 10
21+
b_cr = 13
2022
)
2123

2224
pub struct Scanner {
@@ -492,18 +494,22 @@ fn (mut s Scanner) ident_number() string {
492494
}
493495
}
494496

495-
[inline]
497+
[direct_array_access; inline]
496498
fn (mut s Scanner) skip_whitespace() {
497-
// if s.is_vh { println('vh') return }
498-
for s.pos < s.text.len && s.text[s.pos].is_space() {
499-
if util.is_nl(s.text[s.pos]) && s.is_vh {
499+
for s.pos < s.text.len {
500+
c := s.text[s.pos]
501+
if !c.is_space() {
500502
return
501503
}
502-
if s.pos + 1 < s.text.len && s.text[s.pos] == `\r` && s.text[s.pos + 1] == `\n` {
504+
c_is_nl := c == scanner.b_cr || c == scanner.b_lf
505+
if c_is_nl && s.is_vh {
506+
return
507+
}
508+
if s.pos + 1 < s.text.len && c == scanner.b_cr && s.text[s.pos + 1] == scanner.b_lf {
503509
s.is_crlf = true
504510
}
505511
// Count \r\n as one line
506-
if util.is_nl(s.text[s.pos]) && !s.expect('\r\n', s.pos - 1) {
512+
if c_is_nl && !(s.pos > 0 && s.text[s.pos - 1] == scanner.b_cr && c == scanner.b_lf) {
507513
s.inc_line_number()
508514
}
509515
s.pos++
@@ -975,7 +981,7 @@ fn (mut s Scanner) text_scan() token.Token {
975981
start := s.pos + 1
976982
s.ignore_line()
977983
mut comment_line_end := s.pos
978-
if s.text[s.pos - 1] == `\r` {
984+
if s.text[s.pos - 1] == scanner.b_cr {
979985
comment_line_end--
980986
} else {
981987
// fix line_nr, \n was read; the comment is marked on the next line
@@ -987,7 +993,7 @@ fn (mut s Scanner) text_scan() token.Token {
987993
mut comment := s.line_comment
988994
// Find out if this comment is on its own line (for vfmt)
989995
mut is_separate_line_comment := true
990-
for j := start - 2; j >= 0 && s.text[j] != `\n`; j-- {
996+
for j := start - 2; j >= 0 && s.text[j] != scanner.b_lf; j-- {
991997
if s.text[j] !in [`\t`, ` `] {
992998
is_separate_line_comment = false
993999
}
@@ -1015,7 +1021,7 @@ fn (mut s Scanner) text_scan() token.Token {
10151021
s.line_nr--
10161022
s.error('comment not terminated')
10171023
}
1018-
if s.text[s.pos] == `\n` {
1024+
if s.text[s.pos] == scanner.b_lf {
10191025
s.inc_line_number()
10201026
continue
10211027
}
@@ -1098,7 +1104,7 @@ fn (mut s Scanner) ident_string() string {
10981104
if start_char == s.quote
10991105
|| (start_char == s.inter_quote && (s.is_inter_start || s.is_enclosed_inter)) {
11001106
start++
1101-
} else if start_char == `\n` {
1107+
} else if start_char == scanner.b_lf {
11021108
s.inc_line_number()
11031109
}
11041110
s.is_inside_string = false
@@ -1120,10 +1126,10 @@ fn (mut s Scanner) ident_string() string {
11201126
if c == s.inter_quote && (s.is_inter_start || s.is_enclosed_inter) {
11211127
break
11221128
}
1123-
if c == `\r` {
1129+
if c == scanner.b_cr {
11241130
n_cr_chars++
11251131
}
1126-
if c == `\n` {
1132+
if c == scanner.b_lf {
11271133
s.inc_line_number()
11281134
}
11291135
// Don't allow \0
@@ -1288,9 +1294,9 @@ fn (mut s Scanner) ignore_line() {
12881294
s.inc_line_number()
12891295
}
12901296

1291-
[inline]
1297+
[direct_array_access; inline]
12921298
fn (mut s Scanner) eat_to_end_of_line() {
1293-
for s.pos < s.text.len && s.text[s.pos] != `\n` {
1299+
for s.pos < s.text.len && s.text[s.pos] != scanner.b_lf {
12941300
s.pos++
12951301
}
12961302
}

vlib/v/util/scanning.v

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@ pub fn is_func_char(c byte) bool {
1010
return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || c == `_` || c.is_digit()
1111
}
1212

13-
[inline]
14-
pub fn is_nl(c byte) bool {
15-
return c == `\r` || c == `\n`
16-
}
17-
1813
pub fn contains_capital(s string) bool {
1914
for c in s {
2015
if c >= `A` && c <= `Z` {

0 commit comments

Comments
 (0)