From af875ede9212488056363daef590e4e91220f2b5 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 17 Nov 2024 08:17:19 -0300 Subject: [PATCH] v: do a minor optimizations on `cmd/v` (#22880) --- vlib/builtin/string.v | 2 +- vlib/sync/stdatomic/atomic.c.v | 8 ++++++++ vlib/v/ast/ast.v | 2 ++ vlib/v/checker/checker.v | 1 + vlib/v/parser/parser.v | 1 + vlib/v/scanner/scanner.v | 2 ++ 6 files changed, 15 insertions(+), 1 deletion(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 73aa4b25a5..42152dba02 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1394,7 +1394,7 @@ pub fn (s string) index_u8_last(c u8) int { } // last_index_u8 returns the index of the last occurrence of byte `c` if it was found in the string. -@[inline] +@[direct_array_access; inline] pub fn (s string) last_index_u8(c u8) int { for i := s.len - 1; i >= 0; i-- { if s[i] == c { diff --git a/vlib/sync/stdatomic/atomic.c.v b/vlib/sync/stdatomic/atomic.c.v index fa975a7c41..980de984f0 100644 --- a/vlib/sync/stdatomic/atomic.c.v +++ b/vlib/sync/stdatomic/atomic.c.v @@ -9,24 +9,28 @@ module stdatomic // much more. // add_u64 adds provided delta as an atomic operation +@[inline] pub fn add_u64(ptr &u64, delta int) u64 { C.atomic_fetch_add_u64(voidptr(ptr), delta) return *ptr } // sub_u64 subtracts provided delta as an atomic operation +@[inline] pub fn sub_u64(ptr &u64, delta int) u64 { C.atomic_fetch_sub_u64(voidptr(ptr), delta) return *ptr } // add_i64 adds provided delta as an atomic operation +@[inline] pub fn add_i64(ptr &i64, delta int) i64 { C.atomic_fetch_add_u64(voidptr(ptr), delta) return *ptr } // add_i64 subtracts provided delta as an atomic operation +@[inline] pub fn sub_i64(ptr &i64, delta int) i64 { C.atomic_fetch_sub_u64(voidptr(ptr), delta) return *ptr @@ -34,21 +38,25 @@ pub fn sub_i64(ptr &i64, delta int) i64 { // atomic store/load operations have to be used when there might be another concurrent access // atomicall set a value +@[inline] pub fn store_u64(ptr &u64, val u64) { C.atomic_store_u64(voidptr(ptr), val) } // atomicall get a value +@[inline] pub fn load_u64(ptr &u64) u64 { return C.atomic_load_u64(voidptr(ptr)) } // atomicall set a value +@[inline] pub fn store_i64(ptr &i64, val i64) { C.atomic_store_u64(voidptr(ptr), val) } // atomicall get a value +@[inline] pub fn load_i64(ptr &i64) i64 { return i64(C.atomic_load_u64(voidptr(ptr))) } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 0595da7514..57eb93db75 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -2675,10 +2675,12 @@ pub fn (expr Expr) is_literal() bool { } } +@[inline] pub fn (e Expr) is_nil() bool { return e is Nil || (e is UnsafeExpr && e.expr is Nil) } +@[direct_array_access] pub fn type_can_start_with_token(tok &token.Token) bool { return match tok.kind { .name { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1fd062b4e3..6c03c5cf98 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -491,6 +491,7 @@ fn (mut c Checker) file_has_main_fn(file &ast.File) bool { return has_main_fn } +@[direct_array_access] fn (mut c Checker) check_valid_snake_case(name string, identifier string, pos token.Pos) { if c.pref.translated || c.file.is_translated { return diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 9977da7630..841bfd1777 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -655,6 +655,7 @@ fn (mut p Parser) mark_last_call_return_as_used(mut last_stmt ast.Stmt) { } } +@[inline] fn (mut p Parser) next() { p.prev_tok = p.tok p.tok = p.peek_tok diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index a0eeeb9457..1f4f31e1ec 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -1183,10 +1183,12 @@ fn (mut s Scanner) invalid_character() { s.error('invalid character `${c}`') } +@[inline] fn (s &Scanner) current_column() int { return s.pos - s.last_nl_pos } +@[direct_array_access] fn (s &Scanner) count_symbol_before(p int, sym u8) int { mut count := 0 for i := p; i >= 0; i-- {