diff src/vim9instr.c @ 26729:b969fdb8cd46 v8.2.3893

patch 8.2.3893: Vim9: many local variables are initialized with an instruction Commit: https://github.com/vim/vim/commit/5cd647935d0834b3064aa36384b8f6730fadadd6 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Dec 25 18:23:24 2021 +0000 patch 8.2.3893: Vim9: many local variables are initialized with an instruction Problem: Vim9: many local variables are initialized with an instruction. Solution: Initialize local variables to zero to avoid the instructions.
author Bram Moolenaar <Bram@vim.org>
date Sat, 25 Dec 2021 19:30:03 +0100
parents c3cffd372c6f
children 06a137af96f8
line wrap: on
line diff
--- a/src/vim9instr.c
+++ b/src/vim9instr.c
@@ -1886,7 +1886,7 @@ generate_store_var(
 }
 
     int
-generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count)
+generate_store_lhs(cctx_T *cctx, lhs_T *lhs, int instr_count, int is_decl)
 {
     if (lhs->lhs_dest != dest_local)
 	return generate_store_var(cctx, lhs->lhs_dest,
@@ -1899,8 +1899,9 @@ generate_store_lhs(cctx_T *cctx, lhs_T *
 	garray_T	*instr = &cctx->ctx_instr;
 	isn_T		*isn = ((isn_T *)instr->ga_data) + instr->ga_len - 1;
 
-	// optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
-	// ISN_STORENR
+	// Optimization: turn "var = 123" from ISN_PUSHNR + ISN_STORE into
+	// ISN_STORENR.
+	// And "var = 0" does not need any instruction.
 	if (lhs->lhs_lvar->lv_from_outer == 0
 		&& instr->ga_len == instr_count + 1
 		&& isn->isn_type == ISN_PUSHNR)
@@ -1908,9 +1909,16 @@ generate_store_lhs(cctx_T *cctx, lhs_T *
 	    varnumber_T val = isn->isn_arg.number;
 	    garray_T    *stack = &cctx->ctx_type_stack;
 
-	    isn->isn_type = ISN_STORENR;
-	    isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
-	    isn->isn_arg.storenr.stnr_val = val;
+	    if (val == 0 && is_decl)
+	    {
+		--instr->ga_len;
+	    }
+	    else
+	    {
+		isn->isn_type = ISN_STORENR;
+		isn->isn_arg.storenr.stnr_idx = lhs->lhs_lvar->lv_idx;
+		isn->isn_arg.storenr.stnr_val = val;
+	    }
 	    if (stack->ga_len > 0)
 		--stack->ga_len;
 	}