From 8b3c06904490cd84eb5235e35ef3e57e30b5e4d2 Mon Sep 17 00:00:00 2001 From: zeozeozeo <108888572+zeozeozeo@users.noreply.github.com> Date: Sun, 11 Feb 2024 11:59:32 +0500 Subject: [PATCH 1/2] cgen: use C99 short array init syntax Before this commit, cgen would initialize fixed arrays like this: ```c Array_fixed_u8_5 buffer = {0, 0, 0, 0, 0}; ``` This means that large arrays would generate *a lot* of C code just for initializing the array, and that significantly increased compilation times. C99 allows this short initialization syntax (see C99 6.7.8/21): ```c Array_fixed_u8_2048 buffer = {0}; // array of 2048 zeroes ``` --- vlib/v/gen/c/array.v | 88 ++++++++++---------------------------------- 1 file changed, 20 insertions(+), 68 deletions(-) diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 43d049c9db4195..1d50334fe09216 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -96,26 +96,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st if var_name.len == 0 { g.write('${ret_typ} ${past.tmp_var} =') } - g.write('{') - if node.has_val { - for i in 0 .. node.exprs.len { - g.write('0') - if i != node.exprs.len - 1 { - g.write(', ') - } - } - } else if node.has_init { - info := array_type.unaliased_sym.info as ast.ArrayFixed - for i in 0 .. info.size { - g.write('0') - if i != info.size - 1 { - g.write(', ') - } - } - } else { - g.write('0') - } - g.write('}') + g.write('{0}') g.writeln(';') g.writeln('{') g.indent++ @@ -174,68 +155,39 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st } } } else if node.has_init { - info := array_type.unaliased_sym.info as ast.ArrayFixed - for i in 0 .. info.size { - g.expr_with_init(node) - if i != info.size - 1 { - g.write(', ') - } - } + g.expr_with_init(node) } else if is_amp { g.write('0') } else { elem_sym := g.table.final_sym(node.elem_type) + // short array init, e.g. `Array_fixed_u8_2048 = {0}`, this works in C99 if elem_sym.kind == .map { // fixed array for map -- [N]map[key_type]value_type - info := array_type.unaliased_sym.info as ast.ArrayFixed map_info := elem_sym.map_info() - for i in 0 .. info.size { - g.expr(ast.MapInit{ - key_type: map_info.key_type - value_type: map_info.value_type - }) - if i != info.size - 1 { - g.write(', ') - } - } + g.expr(ast.MapInit{ + key_type: map_info.key_type + value_type: map_info.value_type + }) } else if elem_sym.kind == .array_fixed { // nested fixed array -- [N][N]type - info := array_type.unaliased_sym.info as ast.ArrayFixed arr_info := elem_sym.array_fixed_info() - for i in 0 .. info.size { - g.expr(ast.ArrayInit{ - exprs: [ast.IntegerLiteral{}] - typ: node.elem_type - elem_type: arr_info.elem_type - }) - if i != info.size - 1 { - g.write(', ') - } - } + g.expr(ast.ArrayInit{ + exprs: [ast.IntegerLiteral{}] + typ: node.elem_type + elem_type: arr_info.elem_type + }) } else if elem_sym.kind == .chan { // fixed array for chan -- [N]chan - info := array_type.unaliased_sym.info as ast.ArrayFixed chan_info := elem_sym.chan_info() - for i in 0 .. info.size { - g.expr(ast.ChanInit{ - typ: node.elem_type - elem_type: chan_info.elem_type - }) - if i != info.size - 1 { - g.write(', ') - } - } + g.expr(ast.ChanInit{ + typ: node.elem_type + elem_type: chan_info.elem_type + }) } else { - info := array_type.unaliased_sym.info as ast.ArrayFixed - for i in 0 .. info.size { - if node.elem_type.has_flag(.option) { - g.expr_with_opt(ast.None{}, ast.none_type, node.elem_type) - } else { - g.write(g.type_default(node.elem_type)) - } - if i != info.size - 1 { - g.write(', ') - } + if node.elem_type.has_flag(.option) { + g.expr_with_opt(ast.None{}, ast.none_type, node.elem_type) + } else { + g.write(g.type_default(node.elem_type)) } } } From 3dc1d46ff43d9c14963fc8a7484347d726765c14 Mon Sep 17 00:00:00 2001 From: zeozeozeo <108888572+zeozeozeo@users.noreply.github.com> Date: Sun, 11 Feb 2024 12:07:48 +0500 Subject: [PATCH 2/2] small simplification --- vlib/v/gen/c/array.v | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 1d50334fe09216..c49f029f309b91 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -96,8 +96,7 @@ fn (mut g Gen) fixed_array_init(node ast.ArrayInit, array_type Type, var_name st if var_name.len == 0 { g.write('${ret_typ} ${past.tmp_var} =') } - g.write('{0}') - g.writeln(';') + g.writeln('{0};') g.writeln('{') g.indent++ g.writeln('${elem_typ}* pelem = (${elem_typ}*)${past.tmp_var};')