From f27f3515aeb4d534731c0c70095f8294651050c7 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 23 Sep 2019 22:47:09 +0300 Subject: [PATCH] compiler: optimize mark_var_used() etc --- compiler/fn.v | 29 +++++++++++++---------------- compiler/main.v | 9 +++++++-- compiler/parser.v | 4 +++- compiler/table.v | 3 +++ 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/compiler/fn.v b/compiler/fn.v index 1d7f5da466..e443c8600f 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -69,27 +69,24 @@ fn (p mut Parser) open_scope() { } fn (p mut Parser) mark_var_used(v Var) { - for i, vv in p.local_vars { - if vv.name == v.name { - p.local_vars[i].is_used = true - } - } + if v.idx == -1 { + return + } + p.local_vars[v.idx].is_used = true } fn (p mut Parser) mark_var_returned(v Var) { - for i, vv in p.local_vars { - if vv.name == v.name { - p.local_vars[i].is_returned = true - } - } + if v.idx == -1 || v.idx >= p.local_vars.len { + return + } + p.local_vars[v.idx].is_returned = true } fn (p mut Parser) mark_var_changed(v Var) { - for i, vv in p.local_vars { - if vv.name == v.name { - p.local_vars[i].is_changed = true - } - } + if v.idx == -1 || v.idx >= p.local_vars.len { + return + } + p.local_vars[v.idx].is_changed = true } fn (p mut Parser) known_var(name string) bool { @@ -100,7 +97,7 @@ fn (p mut Parser) known_var(name string) bool { } fn (p mut Parser) register_var(v Var) { - mut new_var := {v | scope_level: p.cur_fn.scope_level} + mut new_var := {v | idx: p.var_idx, scope_level: p.cur_fn.scope_level} if v.line_nr == 0 { spos := p.scanner.get_scanner_pos() new_var.scanner_pos = spos diff --git a/compiler/main.v b/compiler/main.v index aa4a8549ec..9661799766 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -241,8 +241,7 @@ fn (v mut V) compile() { for file in v.files { mut p := v.new_parser(file) p.parse(.decl) - - + if p.pref.autofree { p.scanner.text.free() free(p.scanner) } } // Main pass cgen.pass = Pass.main @@ -308,6 +307,7 @@ fn (v mut V) compile() { for file in v.files { mut p := v.new_parser(file) p.parse(.main) + if p.pref.autofree { p.scanner.text.free() free(p.scanner) } // p.g.gen_x64() // Format all files (don't format automatically generated vlib headers) if !v.pref.nofmt && !file.contains('/vlib/') { @@ -574,11 +574,13 @@ fn (v mut V) add_v_files_to_compile() { for file in v.files { mut p := v.new_parser(file) p.parse(.imports) + if p.pref.autofree { p.scanner.text.free() free(p.scanner) } } // Parse user imports for file in user_files { mut p := v.new_parser(file) p.parse(.imports) + if p.pref.autofree { p.scanner.text.free() free(p.scanner) } } // Parse lib imports /* @@ -597,6 +599,8 @@ fn (v mut V) add_v_files_to_compile() { for file in vfiles { mut p := v.new_parser(file, Pass.imports) p.parse() + + if p.pref.autofree { p.scanner.text.free() free(p.scanner) } } } } @@ -615,6 +619,7 @@ fn (v mut V) add_v_files_to_compile() { for file in vfiles { mut p := v.new_parser(file) p.parse(.imports) + if p.pref.autofree { p.scanner.text.free() free(p.scanner) } } } if v.pref.is_verbose { diff --git a/compiler/parser.v b/compiler/parser.v index 6a5f80241e..a3d5384843 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -1777,7 +1777,9 @@ fn (p mut Parser) name_expr() string { fn (p mut Parser) var_expr(v Var) string { p.log('\nvar_expr() v.name="$v.name" v.typ="$v.typ"') // println('var expr is_tmp=$p.cgen.is_tmp\n') - p.mark_var_used(v) + if !v.is_const { + p.mark_var_used(v) + } fn_ph := p.cgen.add_placeholder() p.expr_var = v p.gen(p.table.var_cgen_name(v.name)) diff --git a/compiler/table.v b/compiler/table.v index 9c422bafc5..0c8173cb7f 100644 --- a/compiler/table.v +++ b/compiler/table.v @@ -62,6 +62,7 @@ struct Var { mut: typ string name string + idx int // index in the local_vars array is_arg bool is_const bool args []Var // function args @@ -288,6 +289,7 @@ fn (t mut Table) register_const(name, typ, mod string) { typ: typ is_const: true mod: mod + idx: -1 } } @@ -300,6 +302,7 @@ fn (p mut Parser) register_global(name, typ string) { is_global: true mod: p.mod is_mut: true + idx: -1 } }