From d97ed7724d7ebdc6297d7ddbc352db652d7afe3d Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 29 Sep 2025 22:19:36 +0300 Subject: [PATCH] v.token,v.util: improve the startup time for vfmt a bit, by reducing onetime initialisation costs for common V consts/tables --- vlib/v/token/keywords_matcher_trie.v | 5 +++-- vlib/v/token/token.v | 7 ++++++- vlib/v/util/scanning.v | 3 +++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/vlib/v/token/keywords_matcher_trie.v b/vlib/v/token/keywords_matcher_trie.v index 34efb8b7f..1a02084ff 100644 --- a/vlib/v/token/keywords_matcher_trie.v +++ b/vlib/v/token/keywords_matcher_trie.v @@ -137,10 +137,11 @@ pub fn (node &TrieNode) show(level int) { // `word_idx` is just used as an accumulator, and starts from 0 at the root of the tree. @[direct_array_access] pub fn (mut node TrieNode) add_word(word string, value int, word_idx int) { - first := u8(word[word_idx] or { + if word_idx < 0 || word_idx >= word.len { node.value = value return - }) + } + first := u8(word[word_idx]) // eprintln('>> node: ${ptr_str(node)} | first: $first | word_idx: $word_idx') mut child_node := node.children[first] if child_node == unsafe { nil } { diff --git a/vlib/v/token/token.v b/vlib/v/token/token.v index f8fcad1c4..d4b6be3f2 100644 --- a/vlib/v/token/token.v +++ b/vlib/v/token/token.v @@ -212,6 +212,7 @@ pub const scanner_matcher = new_keywords_matcher_trie[Kind](keywords) // build_keys generates a map with keywords' string values: // Keywords['return'] == .key_return +@[direct_array_access] fn build_keys() map[string]Kind { mut res := map[string]Kind{} for t in int(Kind.keyword_beg) + 1 .. int(Kind.keyword_end) { @@ -228,6 +229,7 @@ fn build_keys() map[string]Kind { } // TODO: remove once we have `enum Kind { name('name') if('if') ... }` +@[direct_array_access] fn build_token_str() []string { mut s := []string{len: int(Kind._end_)} s[Kind.unknown] = 'unknown' @@ -381,7 +383,7 @@ pub fn (t Kind) is_assign() bool { } // note: used for some code generation, so no quoting -@[inline] +@[direct_array_access; inline] pub fn (t Kind) str() string { idx := int(t) if idx < 0 || token_str.len <= idx { @@ -395,10 +397,12 @@ pub fn (t Token) is_next_to(pre_token Token) bool { return t.pos - pre_token.pos == pre_token.len } +@[inline] pub fn (t Token) is_key() bool { return int(t.kind) > int(Kind.keyword_beg) && int(t.kind) < int(Kind.keyword_end) } +@[direct_array_access] pub fn (t Token) str() string { mut s := t.kind.str() if s.len == 0 { @@ -441,6 +445,7 @@ pub enum Precedence { highest } +@[direct_array_access] pub fn build_precedences() []Precedence { mut p := []Precedence{len: int(Kind._end_), init: Precedence.lowest} p[Kind.lsbr] = .index diff --git a/vlib/v/util/scanning.v b/vlib/v/util/scanning.v index 370c31545..c9364fb81 100644 --- a/vlib/v/util/scanning.v +++ b/vlib/v/util/scanning.v @@ -6,6 +6,7 @@ pub const func_char_table = get_func_char_table() pub const non_whitespace_table = get_non_white_space_table() +@[direct_array_access] fn get_non_white_space_table() [256]bool { mut bytes := [256]bool{} for c in 0 .. 256 { @@ -14,6 +15,7 @@ fn get_non_white_space_table() [256]bool { return bytes } +@[direct_array_access] fn get_name_char_table() [256]bool { mut res := [256]bool{} for c in 0 .. 256 { @@ -22,6 +24,7 @@ fn get_name_char_table() [256]bool { return res } +@[direct_array_access] fn get_func_char_table() [256]bool { mut res := [256]bool{} for c in 0 .. 256 { -- 2.39.5