cgen: minor optimization in index_of_array/map() (#14429)

master
yuyi 2022-05-17 17:08:44 +08:00 committed by GitHub
parent d6aa85d059
commit 60e817ff32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 6 deletions

View File

@ -164,11 +164,12 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym ast.TypeSymbol) {
gen_or := node.or_expr.kind != .absent || node.is_option gen_or := node.or_expr.kind != .absent || node.is_option
left_is_ptr := node.left_type.is_ptr() left_is_ptr := node.left_type.is_ptr()
info := sym.info as ast.Array info := sym.info as ast.Array
mut elem_type_str := g.typ(info.elem_type)
elem_type := info.elem_type elem_type := info.elem_type
elem_sym := g.table.sym(elem_type) elem_sym := g.table.sym(elem_type)
if elem_sym.kind == .function { elem_type_str := if elem_sym.kind == .function {
elem_type_str = 'voidptr' 'voidptr'
} else {
g.typ(info.elem_type)
} }
// `vals[i].field = x` is an exception and requires `array_get`: // `vals[i].field = x` is an exception and requires `array_get`:
// `(*(Val*)array_get(vals, i)).field = x;` // `(*(Val*)array_get(vals, i)).field = x;`
@ -355,10 +356,11 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) {
info := sym.info as ast.Map info := sym.info as ast.Map
key_type_str := g.typ(info.key_type) key_type_str := g.typ(info.key_type)
elem_type := info.value_type elem_type := info.value_type
mut elem_type_str := g.typ(elem_type)
elem_sym := g.table.sym(elem_type) elem_sym := g.table.sym(elem_type)
if elem_sym.kind == .function { elem_type_str := if elem_sym.kind == .function {
elem_type_str = 'voidptr' 'voidptr'
} else {
g.typ(elem_type)
} }
get_and_set_types := elem_sym.kind in [.struct_, .map] get_and_set_types := elem_sym.kind in [.struct_, .map]
if g.is_assign_lhs && !g.is_arraymap_set && !get_and_set_types { if g.is_assign_lhs && !g.is_arraymap_set && !get_and_set_types {