Skip to content

Commit c59d640

Browse files
authored
cgen: remove double string cloning (#23331)
1 parent 42222e6 commit c59d640

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎vlib/v/gen/c/array.v‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
4646
}
4747
for i, expr in node.exprs {
4848
if node.expr_types[i] == ast.string_type
49-
&& expr !in [ast.StringLiteral, ast.StringInterLiteral] {
49+
&& expr !in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral, ast.InfixExpr] {
5050
g.write('string_clone(')
5151
g.expr(expr)
5252
g.write(')')
@@ -1035,12 +1035,14 @@ fn (mut g Gen) gen_array_insert(node ast.CallExpr) {
10351035
g.expr(node.args[1].expr)
10361036
g.write('.len)')
10371037
} else {
1038+
needs_clone := left_info.elem_type == ast.string_type
1039+
&& node.args[1].expr !in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral, ast.InfixExpr]
10381040
g.write(', &(${elem_type_str}[]){')
1039-
if left_info.elem_type == ast.string_type {
1041+
if needs_clone {
10401042
g.write('string_clone(')
10411043
}
10421044
g.expr_with_cast(node.args[1].expr, node.args[1].typ, left_info.elem_type)
1043-
if left_info.elem_type == ast.string_type {
1045+
if needs_clone {
10441046
g.write(')')
10451047
}
10461048
g.write('})')

‎vlib/v/gen/c/assert.v‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ fn (mut g Gen) gen_assert_single_expr(expr ast.Expr, typ ast.Type) {
229229
}
230230
else {
231231
mut should_clone := true
232-
if typ == ast.string_type && expr is ast.StringLiteral {
232+
if typ == ast.string_type
233+
&& expr in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral] {
233234
should_clone = false
234235
}
235236
if expr is ast.CTempVar {

‎vlib/v/gen/c/infix.v‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
10541054
needs_clone := !g.is_builtin_mod
10551055
&& array_info.elem_type.idx() == ast.string_type_idx
10561056
&& array_info.elem_type.nr_muls() == 0
1057+
&& node.right !in [ast.StringLiteral, ast.StringInterLiteral, ast.CallExpr, ast.IndexExpr, ast.InfixExpr]
10571058
if needs_clone {
10581059
g.write('string_clone(')
10591060
}

0 commit comments

Comments
 (0)