Skip to content

Commit

Permalink
checker: reduce allocations part 2 (vlang#22434)
Browse files Browse the repository at this point in the history
  • Loading branch information
spytheman authored Oct 7, 2024
1 parent 184a863 commit d3c3c39
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
2 changes: 1 addition & 1 deletion vlib/v/checker/autocomplete.v
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn (mut c Checker) ident_autocomplete(node ast.Ident) {
}
}

fn build_method_summary(method ast.Fn) string {
fn build_method_summary(method &ast.Fn) string {
mut s := method.name + '('
for i, param in method.params {
s += param.name
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/check_types.v
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ fn (g Checker) get_generic_array_fixed_element_type(array ast.ArrayFixed) ast.Ty
return typ
}

fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr) {
fn (mut c Checker) infer_fn_generic_types(func &ast.Fn, mut node ast.CallExpr) {
mut inferred_types := []ast.Type{}
mut arg_inferred := []int{}
for gi, gt_name in func.generic_names {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3713,7 +3713,7 @@ struct ACFieldMethod {
typ string
}

fn (mut c Checker) resolve_var_fn(func ast.Fn, mut node ast.Ident, name string) ast.Type {
fn (mut c Checker) resolve_var_fn(func &ast.Fn, mut node ast.Ident, name string) ast.Type {
mut fn_type := ast.new_type(c.table.find_or_register_fn_type(func, false, true))
if func.generic_names.len > 0 {
concrete_types := node.concrete_types.map(c.unwrap_generic(it))
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/checker/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ fn (mut c Checker) eval_comptime_const_expr(expr ast.Expr, nlevel int) ?ast.Comp
return none
}

fn (mut c Checker) verify_vweb_params_for_method(node ast.Fn) (bool, int, int) {
fn (mut c Checker) verify_vweb_params_for_method(node &ast.Fn) (bool, int, int) {
margs := node.params.len - 1 // first arg is the receiver/this
// if node.attrs.len == 0 || (node.attrs.len == 1 && node.attrs[0].name == 'post') {
if node.attrs.len == 0 {
Expand Down
50 changes: 29 additions & 21 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type {
return typ
}

fn (mut c Checker) builtin_args(mut node ast.CallExpr, fn_name string, func ast.Fn) {
fn (mut c Checker) builtin_args(mut node ast.CallExpr, fn_name string, func &ast.Fn) {
c.inside_interface_deref = true
c.expected_type = ast.string_type
if !(node.language != .js && node.args[0].expr is ast.CallExpr) {
Expand Down Expand Up @@ -1690,25 +1690,33 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
}

// register_trace_call registers the wrapper funcs for calling funcs for callstack feature
fn (mut c Checker) register_trace_call(node ast.CallExpr, func ast.Fn) {
is_traceable := (c.pref.is_callstack || c.pref.is_trace) && c.table.cur_fn != unsafe { nil }
&& node.language == .v && c.file.imports.any(it.mod == 'v.debug')
&& node.name !in ['v.debug.callstack', 'v.debug.add_after_call', 'v.debug.add_before_call', 'v.debug.remove_after_call', 'v.debug.remove_before_call']
if is_traceable {
hash_fn, fn_name := c.table.get_trace_fn_name(c.table.cur_fn, node)
calling_fn := if func.is_method {
'${c.table.type_to_str(c.unwrap_generic(node.left_type))}_${fn_name}'
} else {
fn_name
}
c.table.cur_fn.trace_fns[hash_fn] = ast.FnTrace{
name: calling_fn
file: c.file.path
line: node.pos.line_nr + 1
return_type: node.return_type
func: &func
is_fn_var: node.is_fn_var
fn (mut c Checker) register_trace_call(node &ast.CallExpr, func &ast.Fn) {
if !(c.pref.is_callstack || c.pref.is_trace) || c.table.cur_fn == unsafe { nil }
|| node.language != .v {
return
}
if node.name in ['v.debug.callstack', 'v.debug.add_after_call', 'v.debug.add_before_call',
'v.debug.remove_after_call', 'v.debug.remove_before_call'] {
return
}
if !c.file.imports.any(it.mod == 'v.debug') {
return
}
hash_fn, fn_name := c.table.get_trace_fn_name(c.table.cur_fn, node)
calling_fn := if func.is_method {
'${c.table.type_to_str(c.unwrap_generic(node.left_type))}_${fn_name}'
} else {
fn_name
}
c.table.cur_fn.trace_fns[hash_fn] = ast.FnTrace{
name: calling_fn
file: c.file.path
line: node.pos.line_nr + 1
return_type: node.return_type
func: &ast.Fn{
...func
}
is_fn_var: node.is_fn_var
}
}

Expand Down Expand Up @@ -1741,7 +1749,7 @@ fn (mut c Checker) is_generic_expr(node ast.Expr) bool {
}
}

fn (mut c Checker) resolve_comptime_args(func ast.Fn, node_ ast.CallExpr, concrete_types []ast.Type) map[int]ast.Type {
fn (mut c Checker) resolve_comptime_args(func &ast.Fn, node_ ast.CallExpr, concrete_types []ast.Type) map[int]ast.Type {
mut comptime_args := map[int]ast.Type{}
has_dynamic_vars := (c.table.cur_fn != unsafe { nil } && c.table.cur_fn.generic_names.len > 0)
|| c.comptime.comptime_for_field_var != ''
Expand Down Expand Up @@ -1890,7 +1898,7 @@ fn (mut c Checker) resolve_comptime_args(func ast.Fn, node_ ast.CallExpr, concre
return comptime_args
}

fn (mut c Checker) resolve_fn_generic_args(func ast.Fn, mut node ast.CallExpr) []ast.Type {
fn (mut c Checker) resolve_fn_generic_args(func &ast.Fn, mut node ast.CallExpr) []ast.Type {
mut concrete_types := node.concrete_types.map(c.unwrap_generic(it))

// dynamic values from comptime and generic parameters
Expand Down

0 comments on commit d3c3c39

Please sign in to comment.