Skip to content

Commit

Permalink
cgen: reduce RAM usage, by avoiding a .str() call, for the final stri…
Browse files Browse the repository at this point in the history
…ng builder, containing the final C program, used to write it to a file (#23226)
  • Loading branch information
spytheman authored Dec 21, 2024
1 parent 74f0ce6 commit 290f58a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
11 changes: 6 additions & 5 deletions vlib/v/builder/cbuilder/cbuilder.v
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module cbuilder

import os
import strings
import v.pref
import v.util
import v.builder
Expand Down Expand Up @@ -60,17 +61,17 @@ pub fn build_c(mut b builder.Builder, v_files []string, out_file string) {
if b.pref.is_vlines {
output2 = c.fix_reset_dbg_line(output2, out_file)
}
os.write_file(out_file, output2) or { panic(err) }
os.write_file_array(out_file, output2) or { panic(err) }
if b.pref.is_stats {
b.stats_lines = output2.count('\n') + 1
b.stats_lines = output2.count(it == `\n`) + 1
b.stats_bytes = output2.len
}
}

pub fn gen_c(mut b builder.Builder, v_files []string) string {
pub fn gen_c(mut b builder.Builder, v_files []string) strings.Builder {
b.front_and_middle_stages(v_files) or {
if err.code() > 7000 {
return ''
return []u8{}
}
builder.verror(err.msg())
}
Expand All @@ -86,5 +87,5 @@ pub fn gen_c(mut b builder.Builder, v_files []string) string {
util.timing_measure('Parallel C compilation')
}

return result.res
return result.res_builder
}
23 changes: 11 additions & 12 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,12 @@ mut:
@[heap]
pub struct GenOutput {
pub:
header string // produced output for out.h (-parallel-cc)
res string // produced output (complete)
out_str string // produced output from g.out
out0_str string // helpers output (auto fns, dump fns) for out_0.c (-parallel-cc)
extern_str string // extern chunk for (-parallel-cc)
out_fn_start_pos []int // fn decl positions
header string // produced output for out.h (-parallel-cc)
res_builder strings.Builder // produced output (complete)
out_str string // produced output from g.out
out0_str string // helpers output (auto fns, dump fns) for out_0.c (-parallel-cc)
extern_str string // extern chunk for (-parallel-cc)
out_fn_start_pos []int // fn decl positions
}

pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenOutput {
Expand Down Expand Up @@ -723,34 +723,33 @@ pub fn gen(files []&ast.File, mut table ast.Table, pref_ &pref.Preferences) GenO
}
// End of out_0.c

shelpers := helpers.str()

if !g.pref.parallel_cc {
b.write_string(helpers.str())
b.write_string(shelpers)
}

// The rest of the output
out_str := g.out.str()
out0_str := helpers.str()
extern_out_str := g.extern_out.str()
b.write_string(out_str)
b.writeln('// THE END.')
util.timing_measure('cgen common')
res := b.str()
$if trace_all_generic_fn_keys ? {
gkeys := g.table.fn_generic_types.keys()
for gkey in gkeys {
eprintln('>> g.table.fn_generic_types key: ${gkey}')
}
}
out_fn_start_pos := g.out_fn_start_pos.clone()
unsafe { b.free() }
unsafe { helpers.free() }
unsafe { g.free_builders() }

return GenOutput{
header: header
res: res
res_builder: b
out_str: out_str
out0_str: out0_str
out0_str: shelpers
extern_str: extern_out_str
out_fn_start_pos: out_fn_start_pos
}
Expand Down
12 changes: 6 additions & 6 deletions vlib/v/gen/c/cmain.v
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn (mut g Gen) gen_vlines_reset() {
}
}

pub fn fix_reset_dbg_line(src string, out_file string) string {
pub fn fix_reset_dbg_line(src strings.Builder, out_file string) strings.Builder {
util.timing_start(@FN)
defer {
util.timing_measure(@FN)
Expand All @@ -60,7 +60,7 @@ pub fn fix_reset_dbg_line(src string, out_file string) string {
for idx, ob in src {
if ob == `\n` {
lines++
if unsafe { vmemcmp(src.str + idx + 1, reset_dbg_line.str, reset_dbg_line.len) } == 0 {
if unsafe { vmemcmp(&u8(src.data) + idx + 1, reset_dbg_line.str, reset_dbg_line.len) } == 0 {
dbg_reset_line_idx = idx + 1
break
}
Expand All @@ -69,7 +69,7 @@ pub fn fix_reset_dbg_line(src string, out_file string) string {
// find the position of the "..\..\..\src.tmp.c":
mut first_quote_idx := 0
for idx := dbg_reset_line_idx; idx < src.len; idx++ {
if unsafe { src.str[idx] } == `"` {
if unsafe { &u8(src.data)[idx] } == `"` {
first_quote_idx = idx
break
}
Expand All @@ -78,15 +78,15 @@ pub fn fix_reset_dbg_line(src string, out_file string) string {
// before and after it unchanged:
mut sb := strings.new_builder(src.len)
unsafe {
sb.write_ptr(src.str, dbg_reset_line_idx)
sb.write_ptr(&u8(src.data), dbg_reset_line_idx)
sb.write_string('#line ')
sb.write_decimal(lines)
sb.write_ptr(src.str + first_quote_idx - 1, src.len - first_quote_idx)
sb.write_ptr(&u8(src.data) + first_quote_idx - 1, src.len - first_quote_idx)
}
$if trace_reset_dbg_line ? {
eprintln('> reset_dbg_line: ${out_file}:${lines} | first_quote_idx: ${first_quote_idx} | src.len: ${src.len} | sb.len: ${sb.len} | sb.cap: ${sb.cap}')
}
return sb.str()
return sb
}

fn (mut g Gen) gen_c_main_function_only_header() {
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/parser/v_parser_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn test_one() {
mut checker_ := checker.new_checker(table, vpref)
checker_.check(mut program)
result := c.gen([program], mut table, vpref)
res := result.res.replace('\n', '').trim_space().after('#endif')
res := result.res_builder.bytestr().replace('\n', '').trim_space().after('#endif')
println(res)
ok := expected == res
println(res)
Expand Down Expand Up @@ -153,7 +153,7 @@ fn test_parse_expr() {
}
chk.check(mut program)
result := c.gen([program], mut table, vpref)
res := result.res.after('#endif')
res := result.res_builder.bytestr().after('#endif')
println('========')
println(res)
println('========')
Expand Down

0 comments on commit 290f58a

Please sign in to comment.