diff --git a/vlib/builtin/builtin.v b/vlib/builtin/builtin.v index 7cbcb6fb1f..22e6bb6bb7 100644 --- a/vlib/builtin/builtin.v +++ b/vlib/builtin/builtin.v @@ -136,6 +136,7 @@ pub fn malloc(n int) byteptr { panic('malloc(<=0)') } $if prealloc { + //println('p') res := g_m2_ptr unsafe { g_m2_ptr += n diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 2374316c74..ae4a8d74fc 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -15,8 +15,8 @@ import v.depgraph const ( c_reserved = ['delete', 'exit', 'link', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto', 'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register', - 'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned', - 'void', 'volatile', 'while', 'new', 'namespace', 'class', 'typename'] + 'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned', 'void', + 'volatile', 'while', 'new', 'namespace', 'class', 'typename'] // same order as in token.Kind cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le'] // when operands are switched @@ -286,6 +286,9 @@ pub fn (mut g Gen) init() { g.comptime_defines.writeln('#define _VAUTOFREE (1)') // g.comptime_defines.writeln('unsigned char* g_cur_str;') } + if g.pref.prealloc { + g.comptime_defines.writeln('#define _VPREALLOC (1)') + } if g.pref.is_livemain || g.pref.is_liveshared { g.generate_hotcode_reloading_declarations() } @@ -409,7 +412,7 @@ fn (mut g Gen) register_optional(t table.Type) string { } fn (mut g Gen) find_or_register_shared(t table.Type, base string) string { - sh_typ := '__shared__$base' + sh_typ := '__shared__$base' t_idx := t.idx() if t_idx in g.shareds { return sh_typ @@ -968,13 +971,11 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type, expected_type table.Type) g.write('/* sum type cast */ ($exp_styp) {.obj = ') g.expr(expr) g.write(', .typ = $got_idx /* $got_sym.name */}') - } - else { + } else { g.write('/* sum type cast */ ($exp_styp) {.obj = memdup(&($got_styp[]) {') g.expr(expr) g.write('}, sizeof($got_styp)), .typ = $got_idx /* $got_sym.name */}') } - return } } @@ -1073,16 +1074,30 @@ fn (mut g Gen) gen_assert_metainfo(a ast.AssertStmt) string { fn (mut g Gen) gen_assert_single_expr(e ast.Expr, t table.Type) { unknown_value := '*unknown value*' match e { - ast.CallExpr { g.write(ctoslit(unknown_value)) } - ast.CastExpr { g.write(ctoslit(unknown_value)) } - ast.IndexExpr { g.write(ctoslit(unknown_value)) } - ast.PrefixExpr { g.write(ctoslit(unknown_value)) } - ast.MatchExpr { g.write(ctoslit(unknown_value)) } + ast.CallExpr { + g.write(ctoslit(unknown_value)) + } + ast.CastExpr { + g.write(ctoslit(unknown_value)) + } + ast.IndexExpr { + g.write(ctoslit(unknown_value)) + } + ast.PrefixExpr { + g.write(ctoslit(unknown_value)) + } + ast.MatchExpr { + g.write(ctoslit(unknown_value)) + } ast.Type { sym := g.table.get_type_symbol(t) g.write(ctoslit('$sym.name')) } - else { g.gen_expr_to_string(e, t) or { g.write(ctoslit('[$err]')) } } + else { + g.gen_expr_to_string(e, t) or { + g.write(ctoslit('[$err]')) + } + } } g.write(' /* typeof: ' + typeof(e) + ' type: ' + t.str() + ' */ ') } @@ -1152,14 +1167,13 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { arg_len := func.func.args.len for j, arg in func.func.args { arg_type := g.table.get_type_symbol(arg.typ) - g.write('${arg_type.str()} ${arg.name}') + g.write('$arg_type.str() $arg.name') if j < arg_len - 1 { g.write(', ') } } g.writeln(') = $left.name;') - } - else { + } else { styp := g.typ(var_type) g.writeln('$styp _var_$left.pos.pos = $left.name;') } @@ -1240,7 +1254,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { is_call = true return_type = val.return_type } - // TODO: no buffer fiddling + // TODO: no buffer fiddling ast.AnonFn { if blank_assign { g.write('{') @@ -2112,16 +2126,16 @@ fn (mut g Gen) lock_expr(node ast.LockExpr) { deref := if id.is_mut { '->' } else { '.' } lock_prefix := if node.is_rlock { `r` } else { `w` } lock_prefixes << lock_prefix // keep for unlock - g.writeln('sync__RwMutex_${lock_prefix:c}_lock(${name}${deref}mtx);') + g.writeln('sync__RwMutex_${lock_prefix:c}_lock($name${deref}mtx);') } g.stmts(node.stmts) // unlock in reverse order - for i := node.lockeds.len-1; i >= 0; i-- { + for i := node.lockeds.len - 1; i >= 0; i-- { id := node.lockeds[i] lock_prefix := lock_prefixes[i] name := id.name deref := if id.is_mut { '->' } else { '.' } - g.writeln('sync__RwMutex_${lock_prefix:c}_unlock(${name}${deref}mtx);') + g.writeln('sync__RwMutex_${lock_prefix:c}_unlock($name${deref}mtx);') } } @@ -2863,7 +2877,9 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) { fields = struct_init.fields } */ - if is_multiline { g.indent++ } + if is_multiline { + g.indent++ + } // User set fields mut initialized := false for i, field in struct_init.fields { @@ -2929,7 +2945,6 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) { } else { g.write(',') } - initialized = true continue } @@ -2947,11 +2962,12 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) { } else { g.write(',') } - initialized = true } } - if is_multiline { g.indent-- } + if is_multiline { + g.indent-- + } // if struct_init.fields.len == 0 && info.fields.len == 0 { if !initialized { g.write('\n#ifndef __cplusplus\n0\n#endif\n') @@ -3105,6 +3121,10 @@ fn (mut g Gen) write_init_function() { // mb_size := if s_str_buf_size == '' { 1 } else { s_str_buf_size.int() } // g.writeln('g_str_buf = malloc( ${mb_size} * 1024 * 1000 );') } + if g.pref.prealloc { + g.writeln('g_m2_buf = malloc(50 * 1000 * 1000);') + g.writeln('g_m2_ptr = g_m2_buf;') + } g.writeln('\tbuiltin_init();') g.writeln('\tvinit_string_literals();') // @@ -3297,7 +3317,7 @@ fn (g Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol { field_deps << dep } } - // table.Interface {} + // table.Interface {} else {} } // add type and dependant types to graph @@ -3757,11 +3777,11 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string { 'linux_or_macos' { return '' } - // + // 'js' { return '_VJS' } - // compilers: + // compilers: 'tinyc' { return '__TINYC__' } @@ -3777,7 +3797,7 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string { 'cplusplus' { return '__cplusplus' } - // other: + // other: 'debug' { return '_VDEBUG' } @@ -3788,7 +3808,7 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string { return '__GLIBC__' } 'prealloc' { - return 'VPREALLOC' + return '_VPREALLOC' } 'no_bounds_checking' { return 'CUSTOM_DEFINE_no_bounds_checking' @@ -4174,7 +4194,7 @@ fn (mut g Gen) gen_str_for_enum(info table.Enum, styp, str_fn_name string) { g.auto_str_funcs.writeln('string ${str_fn_name}($styp it) { /* gen_str_for_enum */') g.auto_str_funcs.writeln('\tswitch(it) {') // Only use the first multi value on the lookup - mut seen := []string{len:info.vals.len} + mut seen := []string{len: info.vals.len} for val in info.vals { if info.is_multi_allowed && val in seen { continue @@ -4246,7 +4266,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) { g.auto_str_funcs.write('indents, ') if has_custom_str { g.auto_str_funcs.write('${field_styp_fn_name}( it->${c_name(field.name)} ) ') - }else{ + } else { g.auto_str_funcs.write('indent_${field_styp_fn_name}( it->${c_name(field.name)}, indent_count + 1 ) ') } } else if sym.kind in [.array, .array_fixed, .map] { diff --git a/vlib/v/pref/pref.v b/vlib/v/pref/pref.v index e03c4230f4..e3a31708b3 100644 --- a/vlib/v/pref/pref.v +++ b/vlib/v/pref/pref.v @@ -195,6 +195,9 @@ pub fn parse_args(args []string) (&Preferences, string) { '-usecache' { res.use_cache = true } + '-prealloc' { + res.prealloc = true + } '-keepc' { eprintln('-keepc is deprecated. V always keeps the generated .tmp.c files now.') }