From 853dcc33c7158232db7e8f841faf73970c9a968c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Sun, 6 Dec 2020 15:20:31 +0100 Subject: [PATCH] cgen: optimize map op_assign (#7162) --- vlib/v/gen/cgen.v | 45 ++++++++------------------------------------- 1 file changed, 8 insertions(+), 37 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index bd6e79bb86..1f500643ff 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -3801,8 +3801,12 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) { elem_typ := g.table.get_type_symbol(info.value_type) get_and_set_types := elem_typ.kind in [.struct_, .map] if g.is_assign_lhs && !g.is_array_set && !get_and_set_types { - g.is_array_set = true - g.write('map_set(') + if g.assign_op == .assign || info.value_type == table.string_type { + g.is_array_set = true + g.write('map_set(') + } else { + g.write('*(($elem_type_str*)map_get_and_set(') + } if !left_is_ptr || node.left_type.has_flag(.shared_f) { g.write('&') } @@ -3821,42 +3825,9 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) { } else { g.write(', &($elem_type_str[]) { ') } - if g.assign_op != .assign && - g.assign_op in token.assign_tokens && info.value_type != table.string_type { - g.write('(*(int*)map_get(') - if left_is_ptr && !node.left_type.has_flag(.shared_f) { - g.write('*') - } - g.expr(node.left) - if node.left_type.has_flag(.shared_f) { - if left_is_ptr { - g.write('->val') - } else { - g.write('.val') - } - } - g.write(', ') - g.expr(node.index) + if g.assign_op != .assign && info.value_type != table.string_type { zero := g.type_default(info.value_type) - if elem_typ.kind == .function { - g.write(', &(voidptr[]){ $zero }))') - } else { - g.write(', &($elem_type_str[]){ $zero }))') - } - op := match g.assign_op { - .mult_assign { '*' } - .plus_assign { '+' } - .minus_assign { '-' } - .div_assign { '/' } - .xor_assign { '^' } - .mod_assign { '%' } - .or_assign { '|' } - .and_assign { '&' } - .left_shift_assign { '<<' } - .right_shift_assign { '>>' } - else { '' } - } - g.write(' $op ') + g.write('$zero }))') } } else if (g.inside_map_postfix || g.inside_map_infix) || (g.is_assign_lhs && !g.is_array_set && get_and_set_types) {