cgen: minor optimization in infix_expr (#8625)

pull/8630/head
yuyi 2021-02-08 00:22:54 +08:00 committed by GitHub
parent 46f8e68bec
commit 81e8c3bc1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 10 deletions

View File

@ -3029,10 +3029,13 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} else { } else {
left_type left_type
} }
if node.op in [.key_is, .not_is] { op_is_key_is_or_not_is := node.op in [.key_is, .not_is]
if op_is_key_is_or_not_is {
g.is_expr(node) g.is_expr(node)
return return
} }
op_is_key_in_or_not_in := node.op in [.key_in, .not_in]
op_is_eq_or_ne := node.op in [.eq, .ne]
right_sym := g.table.get_type_symbol(node.right_type) right_sym := g.table.get_type_symbol(node.right_type)
has_eq_overloaded := !left_sym.has_method('==') has_eq_overloaded := !left_sym.has_method('==')
unaliased_right := if right_sym.info is table.Alias { unaliased_right := if right_sym.info is table.Alias {
@ -3040,7 +3043,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} else { } else {
node.right_type node.right_type
} }
if unaliased_left == table.ustring_type_idx && node.op != .key_in && node.op != .not_in { if unaliased_left == table.ustring_type_idx && !op_is_key_in_or_not_in {
fn_name := match node.op { fn_name := match node.op {
.plus { .plus {
'ustring_add(' 'ustring_add('
@ -3073,7 +3076,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
g.write(', ') g.write(', ')
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} else if unaliased_left == table.string_type_idx && node.op !in [.key_in, .not_in] { } else if unaliased_left == table.string_type_idx && !op_is_key_in_or_not_in {
// `str == ''` -> `str.len == 0` optimization // `str == ''` -> `str.len == 0` optimization
if node.op in [.eq, .ne] && node.right is ast.StringLiteral if node.op in [.eq, .ne] && node.right is ast.StringLiteral
&& (node.right as ast.StringLiteral).val == '' { && (node.right as ast.StringLiteral).val == '' {
@ -3116,7 +3119,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} }
} else if node.op in [.eq, .ne] && left_sym.kind == .array && right_sym.kind == .array { } else if op_is_eq_or_ne && left_sym.kind == .array && right_sym.kind == .array {
ptr_typ := g.gen_array_equality_fn(left_type) ptr_typ := g.gen_array_equality_fn(left_type)
if node.op == .ne { if node.op == .ne {
g.write('!') g.write('!')
@ -3132,8 +3135,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} }
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} else if node.op in [.eq, .ne] && left_sym.kind == .array_fixed } else if op_is_eq_or_ne && left_sym.kind == .array_fixed && right_sym.kind == .array_fixed {
&& right_sym.kind == .array_fixed {
ptr_typ := g.gen_fixed_array_equality_fn(left_type) ptr_typ := g.gen_fixed_array_equality_fn(left_type)
if node.op == .ne { if node.op == .ne {
g.write('!') g.write('!')
@ -3154,7 +3156,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} }
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} else if node.op in [.eq, .ne] && left_sym.kind == .alias && right_sym.kind == .alias { } else if op_is_eq_or_ne && left_sym.kind == .alias && right_sym.kind == .alias {
ptr_typ := g.gen_alias_equality_fn(left_type) ptr_typ := g.gen_alias_equality_fn(left_type)
if node.op == .eq { if node.op == .eq {
g.write('${ptr_typ}_alias_eq(') g.write('${ptr_typ}_alias_eq(')
@ -3171,7 +3173,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} }
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} else if node.op in [.eq, .ne] && left_sym.kind == .map && right_sym.kind == .map { } else if op_is_eq_or_ne && left_sym.kind == .map && right_sym.kind == .map {
ptr_typ := g.gen_map_equality_fn(left_type) ptr_typ := g.gen_map_equality_fn(left_type)
if node.op == .eq { if node.op == .eq {
g.write('${ptr_typ}_map_eq(') g.write('${ptr_typ}_map_eq(')
@ -3188,7 +3190,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} }
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} else if node.op in [.eq, .ne] && left_sym.kind == .struct_ && right_sym.kind == .struct_ { } else if op_is_eq_or_ne && left_sym.kind == .struct_ && right_sym.kind == .struct_ {
if !has_eq_overloaded { if !has_eq_overloaded {
// Define `==` as negation of Autogenerated `!=` // Define `==` as negation of Autogenerated `!=`
styp := g.typ(left_type) styp := g.typ(left_type)
@ -3217,7 +3219,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
} }
g.expr(node.right) g.expr(node.right)
g.write(')') g.write(')')
} else if node.op in [.key_in, .not_in] { } else if op_is_key_in_or_not_in {
if node.op == .not_in { if node.op == .not_in {
g.write('!') g.write('!')
} }