cgen: add caching to contains_ptr return (#22605)

This commit is contained in:
Felipe Pena 2024-10-22 05:02:12 -03:00 committed by GitHub
parent 6a8ab333d8
commit cb4ddb10d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -121,6 +121,7 @@ mut:
chan_push_options map[string]string // types for `ch <- x or {...}` chan_push_options map[string]string // types for `ch <- x or {...}`
mtxs string // array of mutexes if the `lock` has multiple variables mtxs string // array of mutexes if the `lock` has multiple variables
labeled_loops map[string]&ast.Stmt labeled_loops map[string]&ast.Stmt
contains_ptr_cache map[ast.Type]bool
inner_loop &ast.Stmt = unsafe { nil } inner_loop &ast.Stmt = unsafe { nil }
cur_indexexpr []int // list of nested indexexpr which generates array_set/map_set 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 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 // returns true if `t` includes any pointer(s) - during garbage collection heap regions
// that contain no pointers do not have to be scanned // 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 { 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() { if el_typ.is_any_kind_of_pointer() {
g.contains_ptr_cache[el_typ] = true
return true return true
} }
typ := g.unwrap_generic(el_typ) 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) sym := g.table.final_sym(typ)
if sym.language != .v { if sym.language != .v {
g.contains_ptr_cache[typ] = true
return true return true
} }
match sym.kind { match sym.kind {
.i8, .i16, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .char, .rune, .bool, .enum { .i8, .i16, .int, .i64, .u8, .u16, .u32, .u64, .f32, .f64, .char, .rune, .bool, .enum {
g.contains_ptr_cache[typ] = false
return false return false
} }
.array_fixed { .array_fixed {
@ -8226,6 +8234,7 @@ pub fn (mut g Gen) contains_ptr(el_typ ast.Type) bool {
return false return false
} }
else { else {
g.contains_ptr_cache[typ] = true
return true return true
} }
} }