mirror of
https://github.com/vlang/v.git
synced 2025-09-13 09:25:45 -04:00
cgen: optimise the generated code for returning literal values and option/result values (#22622)
This commit is contained in:
parent
ea8ae7ba17
commit
4a2210f8d0
@ -5454,18 +5454,24 @@ fn (mut g Gen) return_stmt(node ast.Return) {
|
||||
tmpvar := g.new_tmp_var()
|
||||
g.defer_return_tmp_var = tmpvar
|
||||
ret_typ := g.ret_styp(g.unwrap_generic(fn_ret_type))
|
||||
if node.exprs.len == 1 {
|
||||
// `return fn_call_opt()`
|
||||
if (fn_return_is_option || fn_return_is_result) && node.exprs[0] is ast.CallExpr
|
||||
&& node.exprs[0].return_type == g.fn_decl.return_type
|
||||
&& node.exprs[0].or_block.kind == .absent {
|
||||
|
||||
// `return fn_call_opt()`
|
||||
if node.exprs.len == 1 && (fn_return_is_option || fn_return_is_result)
|
||||
&& node.exprs[0] is ast.CallExpr && node.exprs[0].return_type == g.fn_decl.return_type
|
||||
&& node.exprs[0].or_block.kind == .absent {
|
||||
if g.defer_stmts.len > 0 {
|
||||
g.write('${ret_typ} ${tmpvar} = ')
|
||||
g.expr(node.exprs[0])
|
||||
g.writeln(';')
|
||||
g.write_defer_stmts_when_needed()
|
||||
g.writeln('return ${tmpvar};')
|
||||
return
|
||||
} else {
|
||||
g.write_defer_stmts_when_needed()
|
||||
g.write('return ')
|
||||
g.expr(node.exprs[0])
|
||||
g.writeln(';')
|
||||
}
|
||||
return
|
||||
}
|
||||
mut use_tmp_var := g.defer_stmts.len > 0 || g.defer_profile_code.len > 0
|
||||
|| g.cur_lock.lockeds.len > 0
|
||||
@ -5758,7 +5764,7 @@ fn (mut g Gen) return_stmt(node ast.Return) {
|
||||
// Don't use a tmp var if a variable is simply returned: `return x`
|
||||
// Just in case of defer statements exists, that the return values cannot
|
||||
// be modified.
|
||||
if node.exprs[0] !is ast.Ident || use_tmp_var {
|
||||
if use_tmp_var || !(node.exprs[0].is_literal() || node.exprs[0] is ast.Ident) {
|
||||
use_tmp_var = true
|
||||
g.write('${ret_typ} ${tmpvar} = ')
|
||||
} else {
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
VV_LOCAL_SYMBOL void v__preludes__embed_file__zlib__init(void);
|
||||
VV_LOCAL_SYMBOL _result_Array_u8 v__preludes__embed_file__zlib__ZLibDecoder_decompress(v__preludes__embed_file__zlib__ZLibDecoder _d1, Array_u8 data) {
|
||||
= compress__zlib__decompress(data);
|
||||
return compress__zlib__decompress(data);
|
||||
|
||||
res.compressed = v__embed_file__find_index_entry_by_path((voidptr)_v_embed_file_index, _SLIT("embed.vv"), _SLIT("zlib"))->data;
|
||||
res.compression_type = _SLIT("zlib");
|
||||
|
Loading…
x
Reference in New Issue
Block a user