Skip to content

Commit

Permalink
cgen: remove double string cloning (#23331)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Jan 1, 2025
1 parent 42222e6 commit c59d640
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
8 changes: 5 additions & 3 deletions vlib/v/gen/c/array.v
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn (mut g Gen) array_init(node ast.ArrayInit, var_name string) {
}
for i, expr in node.exprs {
if node.expr_types[i] == ast.string_type
&& expr !in [ast.StringLiteral, ast.StringInterLiteral] {
&& expr !in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral, ast.InfixExpr] {
g.write('string_clone(')
g.expr(expr)
g.write(')')
Expand Down Expand Up @@ -1035,12 +1035,14 @@ fn (mut g Gen) gen_array_insert(node ast.CallExpr) {
g.expr(node.args[1].expr)
g.write('.len)')
} else {
needs_clone := left_info.elem_type == ast.string_type
&& node.args[1].expr !in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral, ast.InfixExpr]
g.write(', &(${elem_type_str}[]){')
if left_info.elem_type == ast.string_type {
if needs_clone {
g.write('string_clone(')
}
g.expr_with_cast(node.args[1].expr, node.args[1].typ, left_info.elem_type)
if left_info.elem_type == ast.string_type {
if needs_clone {
g.write(')')
}
g.write('})')
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/gen/c/assert.v
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ fn (mut g Gen) gen_assert_single_expr(expr ast.Expr, typ ast.Type) {
}
else {
mut should_clone := true
if typ == ast.string_type && expr is ast.StringLiteral {
if typ == ast.string_type
&& expr in [ast.IndexExpr, ast.CallExpr, ast.StringLiteral, ast.StringInterLiteral] {
should_clone = false
}
if expr is ast.CTempVar {
Expand Down
1 change: 1 addition & 0 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -1054,6 +1054,7 @@ fn (mut g Gen) infix_expr_left_shift_op(node ast.InfixExpr) {
needs_clone := !g.is_builtin_mod
&& array_info.elem_type.idx() == ast.string_type_idx
&& array_info.elem_type.nr_muls() == 0
&& node.right !in [ast.StringLiteral, ast.StringInterLiteral, ast.CallExpr, ast.IndexExpr, ast.InfixExpr]
if needs_clone {
g.write('string_clone(')
}
Expand Down

0 comments on commit c59d640

Please sign in to comment.