Skip to content

Commit d97ed77

Browse files
committed
v.token,v.util: improve the startup time for vfmt a bit, by reducing onetime initialisation costs for common V consts/tables
1 parent c1d1efb commit d97ed77

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

vlib/v/token/keywords_matcher_trie.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ pub fn (node &TrieNode) show(level int) {
137137
// `word_idx` is just used as an accumulator, and starts from 0 at the root of the tree.
138138
@[direct_array_access]
139139
pub fn (mut node TrieNode) add_word(word string, value int, word_idx int) {
140-
first := u8(word[word_idx] or {
140+
if word_idx < 0 || word_idx >= word.len {
141141
node.value = value
142142
return
143-
})
143+
}
144+
first := u8(word[word_idx])
144145
// eprintln('>> node: ${ptr_str(node)} | first: $first | word_idx: $word_idx')
145146
mut child_node := node.children[first]
146147
if child_node == unsafe { nil } {

vlib/v/token/token.v

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub const scanner_matcher = new_keywords_matcher_trie[Kind](keywords)
212212

213213
// build_keys generates a map with keywords' string values:
214214
// Keywords['return'] == .key_return
215+
@[direct_array_access]
215216
fn build_keys() map[string]Kind {
216217
mut res := map[string]Kind{}
217218
for t in int(Kind.keyword_beg) + 1 .. int(Kind.keyword_end) {
@@ -228,6 +229,7 @@ fn build_keys() map[string]Kind {
228229
}
229230

230231
// TODO: remove once we have `enum Kind { name('name') if('if') ... }`
232+
@[direct_array_access]
231233
fn build_token_str() []string {
232234
mut s := []string{len: int(Kind._end_)}
233235
s[Kind.unknown] = 'unknown'
@@ -381,7 +383,7 @@ pub fn (t Kind) is_assign() bool {
381383
}
382384

383385
// note: used for some code generation, so no quoting
384-
@[inline]
386+
@[direct_array_access; inline]
385387
pub fn (t Kind) str() string {
386388
idx := int(t)
387389
if idx < 0 || token_str.len <= idx {
@@ -395,10 +397,12 @@ pub fn (t Token) is_next_to(pre_token Token) bool {
395397
return t.pos - pre_token.pos == pre_token.len
396398
}
397399

400+
@[inline]
398401
pub fn (t Token) is_key() bool {
399402
return int(t.kind) > int(Kind.keyword_beg) && int(t.kind) < int(Kind.keyword_end)
400403
}
401404

405+
@[direct_array_access]
402406
pub fn (t Token) str() string {
403407
mut s := t.kind.str()
404408
if s.len == 0 {
@@ -441,6 +445,7 @@ pub enum Precedence {
441445
highest
442446
}
443447

448+
@[direct_array_access]
444449
pub fn build_precedences() []Precedence {
445450
mut p := []Precedence{len: int(Kind._end_), init: Precedence.lowest}
446451
p[Kind.lsbr] = .index

vlib/v/util/scanning.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ pub const func_char_table = get_func_char_table()
66

77
pub const non_whitespace_table = get_non_white_space_table()
88

9+
@[direct_array_access]
910
fn get_non_white_space_table() [256]bool {
1011
mut bytes := [256]bool{}
1112
for c in 0 .. 256 {
@@ -14,6 +15,7 @@ fn get_non_white_space_table() [256]bool {
1415
return bytes
1516
}
1617

18+
@[direct_array_access]
1719
fn get_name_char_table() [256]bool {
1820
mut res := [256]bool{}
1921
for c in 0 .. 256 {
@@ -22,6 +24,7 @@ fn get_name_char_table() [256]bool {
2224
return res
2325
}
2426

27+
@[direct_array_access]
2528
fn get_func_char_table() [256]bool {
2629
mut res := [256]bool{}
2730
for c in 0 .. 256 {

0 commit comments

Comments
 (0)