Skip to content

Commit 81e8c3b

Browse files
authored
cgen: minor optimization in infix_expr (#8625)
1 parent 46f8e68 commit 81e8c3b

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,18 +3029,21 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
30293029
} else {
30303030
left_type
30313031
}
3032-
if node.op in [.key_is, .not_is] {
3032+
op_is_key_is_or_not_is := node.op in [.key_is, .not_is]
3033+
if op_is_key_is_or_not_is {
30333034
g.is_expr(node)
30343035
return
30353036
}
3037+
op_is_key_in_or_not_in := node.op in [.key_in, .not_in]
3038+
op_is_eq_or_ne := node.op in [.eq, .ne]
30363039
right_sym := g.table.get_type_symbol(node.right_type)
30373040
has_eq_overloaded := !left_sym.has_method('==')
30383041
unaliased_right := if right_sym.info is table.Alias {
30393042
right_sym.info.parent_type
30403043
} else {
30413044
node.right_type
30423045
}
3043-
if unaliased_left == table.ustring_type_idx && node.op != .key_in && node.op != .not_in {
3046+
if unaliased_left == table.ustring_type_idx && !op_is_key_in_or_not_in {
30443047
fn_name := match node.op {
30453048
.plus {
30463049
'ustring_add('
@@ -3073,7 +3076,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
30733076
g.write(', ')
30743077
g.expr(node.right)
30753078
g.write(')')
3076-
} else if unaliased_left == table.string_type_idx && node.op !in [.key_in, .not_in] {
3079+
} else if unaliased_left == table.string_type_idx && !op_is_key_in_or_not_in {
30773080
// `str == ''` -> `str.len == 0` optimization
30783081
if node.op in [.eq, .ne] && node.right is ast.StringLiteral
30793082
&& (node.right as ast.StringLiteral).val == '' {
@@ -3116,7 +3119,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
31163119
g.expr(node.right)
31173120
g.write(')')
31183121
}
3119-
} else if node.op in [.eq, .ne] && left_sym.kind == .array && right_sym.kind == .array {
3122+
} else if op_is_eq_or_ne && left_sym.kind == .array && right_sym.kind == .array {
31203123
ptr_typ := g.gen_array_equality_fn(left_type)
31213124
if node.op == .ne {
31223125
g.write('!')
@@ -3132,8 +3135,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
31323135
}
31333136
g.expr(node.right)
31343137
g.write(')')
3135-
} else if node.op in [.eq, .ne] && left_sym.kind == .array_fixed
3136-
&& right_sym.kind == .array_fixed {
3138+
} else if op_is_eq_or_ne && left_sym.kind == .array_fixed && right_sym.kind == .array_fixed {
31373139
ptr_typ := g.gen_fixed_array_equality_fn(left_type)
31383140
if node.op == .ne {
31393141
g.write('!')
@@ -3154,7 +3156,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
31543156
}
31553157
g.expr(node.right)
31563158
g.write(')')
3157-
} else if node.op in [.eq, .ne] && left_sym.kind == .alias && right_sym.kind == .alias {
3159+
} else if op_is_eq_or_ne && left_sym.kind == .alias && right_sym.kind == .alias {
31583160
ptr_typ := g.gen_alias_equality_fn(left_type)
31593161
if node.op == .eq {
31603162
g.write('${ptr_typ}_alias_eq(')
@@ -3171,7 +3173,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
31713173
}
31723174
g.expr(node.right)
31733175
g.write(')')
3174-
} else if node.op in [.eq, .ne] && left_sym.kind == .map && right_sym.kind == .map {
3176+
} else if op_is_eq_or_ne && left_sym.kind == .map && right_sym.kind == .map {
31753177
ptr_typ := g.gen_map_equality_fn(left_type)
31763178
if node.op == .eq {
31773179
g.write('${ptr_typ}_map_eq(')
@@ -3188,7 +3190,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
31883190
}
31893191
g.expr(node.right)
31903192
g.write(')')
3191-
} else if node.op in [.eq, .ne] && left_sym.kind == .struct_ && right_sym.kind == .struct_ {
3193+
} else if op_is_eq_or_ne && left_sym.kind == .struct_ && right_sym.kind == .struct_ {
31923194
if !has_eq_overloaded {
31933195
// Define `==` as negation of Autogenerated `!=`
31943196
styp := g.typ(left_type)
@@ -3217,7 +3219,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
32173219
}
32183220
g.expr(node.right)
32193221
g.write(')')
3220-
} else if node.op in [.key_in, .not_in] {
3222+
} else if op_is_key_in_or_not_in {
32213223
if node.op == .not_in {
32223224
g.write('!')
32233225
}

0 commit comments

Comments
 (0)