Skip to content

Commit

Permalink
v.scanner: use table lookups for very frequently done character checks (
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman authored Jul 25, 2024
1 parent 581c5c7 commit 97432c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
8 changes: 4 additions & 4 deletions vlib/v/scanner/scanner.v
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fn (mut s Scanner) ident_name() string {
s.pos++
for s.pos < s.text.len {
c := s.text[s.pos]
if c.is_alnum() || c == `_` {
if util.func_char_table[c] {
s.pos++
continue
}
Expand Down Expand Up @@ -537,7 +537,7 @@ fn (mut s Scanner) skip_whitespace() {
s.pos++
continue
}
if !(c == 32 || (c > 8 && c < 14) || c == 0x85 || c == 0xa0) {
if util.non_whitespace_table[c] {
return
}
c_is_nl := c == scanner.b_cr || c == scanner.b_lf
Expand Down Expand Up @@ -686,7 +686,7 @@ pub fn (mut s Scanner) text_scan() token.Token {
c := s.text[s.pos]
nextc := s.look_ahead(1)
// name or keyword
if util.is_name_char(c) {
if util.name_char_table[c] {
name := s.ident_name()
// tmp hack to detect . in ${}
// Check if not .eof to prevent panic
Expand Down Expand Up @@ -1315,7 +1315,7 @@ pub fn (mut s Scanner) ident_string() string {
break
}
// $var
if prevc == `$` && util.is_name_char(c) && !is_raw
if prevc == `$` && util.name_char_table[c] && !is_raw
&& s.count_symbol_before(s.pos - 2, scanner.backslash) & 1 == 0 {
s.is_inside_string = true
s.is_inter_start = true
Expand Down
38 changes: 34 additions & 4 deletions vlib/v/util/scanning.v
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
module util

@[inline]
pub const name_char_table = get_name_char_table()

pub const func_char_table = get_func_char_table()

pub const non_whitespace_table = get_non_white_space_table()

fn get_non_white_space_table() [256]bool {
mut bytes := [256]bool{}
for c in 0 .. 256 {
bytes[c] = !u8(c).is_space()
}
return bytes
}

fn get_name_char_table() [256]bool {
mut res := [256]bool{}
for c in 0 .. 256 {
res[c] = u8(c).is_letter() || c == `_`
}
return res
}

fn get_func_char_table() [256]bool {
mut res := [256]bool{}
for c in 0 .. 256 {
res[c] = u8(c).is_letter() || u8(c).is_digit() || c == `_`
}
return res
}

@[direct_array_access; inline]
pub fn is_name_char(c u8) bool {
return c.is_letter() || c == `_`
return util.name_char_table[c]
}

@[inline]
@[direct_array_access; inline]
pub fn is_func_char(c u8) bool {
return c.is_letter() || c == `_` || c.is_digit()
return util.func_char_table[c]
}

pub fn contains_capital(s string) bool {
Expand Down

0 comments on commit 97432c3

Please sign in to comment.