From cb4ddb10d9f3850ab0cf64334f903975a68e4422 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 22 Oct 2024 05:02:12 -0300 Subject: [PATCH] cgen: add caching to contains_ptr return (#22605) --- vlib/v/gen/c/cgen.v | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 68dc6df3de..b8fbe76ef1 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -121,6 +121,7 @@ mut: chan_push_options map[string]string // types for `ch <- x or {...}` mtxs string // array of mutexes if the `lock` has multiple variables labeled_loops map[string]&ast.Stmt + contains_ptr_cache map[ast.Type]bool inner_loop &ast.Stmt = unsafe { nil } cur_indexexpr []int // list of nested indexexpr which generates array_set/map_set shareds map[int]string // types with hidden mutex for which decl has been emitted @@ -8173,8 +8174,13 @@ pub fn (mut g Gen) get_array_depth(el_typ ast.Type) int { // returns true if `t` includes any pointer(s) - during garbage collection heap regions // that contain no pointers do not have to be scanned +@[direct_array_access] pub fn (mut g Gen) contains_ptr(el_typ ast.Type) bool { + if t_typ := g.contains_ptr_cache[el_typ] { + return t_typ + } if el_typ.is_any_kind_of_pointer() { + g.contains_ptr_cache[el_typ] = true return true } typ := g.unwrap_generic(el_typ) @@ -8183,10 +8189,12 @@ pub fn (mut g Gen) contains_ptr(el_typ ast.Type) bool { } sym := g.table.final_sym(typ) if sym.language != .v { + g.contains_ptr_cache[typ] = true return true } match sym.kind { .i8, .i16, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .char, .rune, .bool, .enum { + g.contains_ptr_cache[typ] = false return false } .array_fixed { @@ -8226,6 +8234,7 @@ pub fn (mut g Gen) contains_ptr(el_typ ast.Type) bool { return false } else { + g.contains_ptr_cache[typ] = true return true } }