Skip to content

Commit 396dc77

Browse files
authored
markused: reduce hello world csize by 49%, do more cleanups and improvements (#24971)
1 parent 59d408f commit 396dc77

File tree

16 files changed

+424
-303
lines changed

16 files changed

+424
-303
lines changed

cmd/tools/vast/vast.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,7 @@ fn (t Tree) index_expr(node ast.IndexExpr) &Node {
14681468
obj.add_terse('is_setter', t.bool_node(node.is_setter))
14691469
obj.add_terse('is_direct', t.bool_node(node.is_direct))
14701470
obj.add_terse('or_expr', t.or_expr(node.or_expr))
1471+
obj.add_terse('typ', t.type_node(node.typ))
14711472
obj.add('pos', t.pos(node.pos))
14721473
return obj
14731474
}

vlib/builtin/string.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub fn tos2(s &u8) string {
134134
// It will panic, when the pointer `s` is 0.
135135
// It is the same as `tos2`, but for &char pointers, avoiding callsite casts.
136136
// See also `tos_clone`.
137-
@[markused; unsafe]
137+
@[unsafe]
138138
pub fn tos3(s &char) string {
139139
if s == 0 {
140140
panic('tos3: nil string')
@@ -151,7 +151,7 @@ pub fn tos3(s &char) string {
151151
// It returns '', when given a 0 pointer `s`, it does NOT panic.
152152
// It is the same as `tos5`, but for &u8 pointers, avoiding callsite casts.
153153
// See also `tos_clone`.
154-
@[markused; unsafe]
154+
@[unsafe]
155155
pub fn tos4(s &u8) string {
156156
if s == 0 {
157157
return ''
@@ -168,7 +168,7 @@ pub fn tos4(s &u8) string {
168168
// It returns '', when given a 0 pointer `s`, it does NOT panic.
169169
// It is the same as `tos4`, but for &char pointers, avoiding callsite casts.
170170
// See also `tos_clone`.
171-
@[markused; unsafe]
171+
@[unsafe]
172172
pub fn tos5(s &char) string {
173173
if s == 0 {
174174
return ''

vlib/v/ast/ast.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,7 @@ pub mut:
12071207
is_option bool // IfGuard
12081208
is_direct bool // Set if the underlying memory can be safely accessed
12091209
is_gated bool // #[] gated array
1210+
typ Type
12101211
}
12111212

12121213
@[minify]

vlib/v/ast/table.v

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub mut:
1414
index bool // string[0]
1515
range_index bool // string[0..1]
1616
cast_ptr bool // &u8(...)
17-
asserts bool // assert expr
1817
anon_fn bool // fn () { }
1918
auto_str bool // auto str fns
2019
auto_str_ptr bool // auto str fns for ptr type
@@ -25,10 +24,8 @@ pub mut:
2524
arr_pop bool // arr.pop()
2625
arr_delete bool // arr.delete()
2726
arr_reverse bool // arr.reverse()
28-
arr_init bool // [1, 2, 3]
2927
arr_map bool // []map[key]value
3028
type_name bool // var.type_name()
31-
map_update bool // {...foo}
3229
print_options bool // print option type
3330
print_types map[int]bool // print() idx types
3431
used_fns map[string]bool // filled in by markused
@@ -39,11 +36,8 @@ pub mut:
3936
used_maps int // how many times maps were used, filled in by markused
4037
used_none int // how many times `none` was used, filled in by markused
4138
// json bool // json is imported
42-
debugger bool // debugger is used
4339
comptime_calls map[string]bool // resolved name to call on comptime
44-
comptime_syms map[int]bool // resolved syms (generic)
45-
comptime_for bool // uses $for
46-
memory_align bool // @[aligned] for struct
40+
comptime_syms map[Type]bool // resolved syms (generic)
4741
}
4842

4943
@[unsafe]

vlib/v/checker/checker.v

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,9 +2385,7 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
23852385
}
23862386
}
23872387
ast.NodeError {}
2388-
ast.DebuggerStmt {
2389-
c.table.used_features.debugger = true
2390-
}
2388+
ast.DebuggerStmt {}
23912389
ast.AsmStmt {
23922390
c.asm_stmt(mut node)
23932391
}
@@ -2398,9 +2396,6 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
23982396
}
23992397

24002398
fn (mut c Checker) assert_stmt(mut node ast.AssertStmt) {
2401-
if node.is_used {
2402-
c.table.used_features.asserts = true
2403-
}
24042399
cur_exp_typ := c.expected_type
24052400
c.expected_type = ast.bool_type
24062401
assert_type := c.check_expr_option_or_result_call(node.expr, c.expr(mut node.expr))
@@ -5140,6 +5135,7 @@ fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
51405135
}
51415136
c.stmts_ending_with_expression(mut node.or_expr.stmts, c.expected_or_type)
51425137
c.check_expr_option_or_result_call(node, typ)
5138+
node.typ = typ
51435139
return typ
51445140
}
51455141

vlib/v/checker/comptime.v

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
277277
prev_inside_x_matches_type := c.inside_x_matches_type
278278
c.push_new_comptime_info()
279279
c.comptime.inside_comptime_for = true
280-
c.table.used_features.comptime_for = true
281280
if c.field_data_type == 0 {
282281
c.field_data_type = c.table.find_type('FieldData')
283282
}
@@ -322,7 +321,6 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
322321
c.type_resolver.update_ct_type('${node.val_var}.typ', node.typ)
323322
c.stmts(mut node.stmts)
324323
c.pop_comptime_info()
325-
c.table.used_features.comptime_for = true
326324
} else {
327325
c.error('iterating over .values is supported only for enums, and ${sym.name} is not an enum',
328326
node.typ_pos)
@@ -340,7 +338,6 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) {
340338
c.stmts(mut node.stmts)
341339
c.pop_comptime_info()
342340
}
343-
c.table.used_features.comptime_for = true
344341
} else if node.kind == .params {
345342
if !(sym.kind == .function || sym.name == 'FunctionData') {
346343
c.error('iterating over `.params` is supported only for functions, and `${sym.name}` is not a function',

vlib/v/checker/containers.v

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import v.token
88
fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
99
mut elem_type := ast.void_type
1010
unwrap_elem_type := c.unwrap_generic(node.elem_type)
11+
if node.typ.has_flag(.generic) {
12+
c.table.used_features.comptime_syms[c.unwrap_generic(node.typ)] = true
13+
}
1114
if c.pref.warn_about_allocs {
1215
c.warn_alloc('array initialization', node.pos)
1316
}
1417
// `x := []string{}` (the type was set in the parser)
1518
if node.typ != ast.void_type {
16-
if !c.is_builtin_mod && c.mod !in ['builtin', 'strings', 'strconv', 'math.bits'] {
17-
c.table.used_features.arr_init = true
18-
}
1919
if node.elem_type != 0 {
2020
elem_sym := c.table.sym(node.elem_type)
2121
c.check_any_type(node.elem_type, elem_sym, node.pos)
@@ -159,9 +159,6 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
159159
}
160160
// `[1,2,3]`
161161
if node.exprs.len > 0 && node.elem_type == ast.void_type {
162-
if !c.is_builtin_mod && c.mod !in ['builtin', 'strings', 'strconv', 'math.bits'] {
163-
c.table.used_features.arr_init = true
164-
}
165162
mut expected_value_type := ast.void_type
166163
mut expecting_interface_array := false
167164
mut expecting_sumtype_array := false
@@ -502,6 +499,9 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
502499
// `x := map[string]string` - set in parser
503500
if node.typ != 0 {
504501
info := c.table.sym(node.typ).map_info()
502+
if node.typ.has_flag(.generic) {
503+
c.table.used_features.comptime_syms[c.unwrap_generic(node.typ)] = true
504+
}
505505
if info.value_type != 0 {
506506
if info.value_type.has_flag(.result) {
507507
c.error('cannot use Result type as map value type', node.pos)
@@ -541,7 +541,6 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
541541
}
542542

543543
if (node.keys.len > 0 && node.vals.len > 0) || node.has_update_expr {
544-
c.table.used_features.map_update = true
545544
mut map_type := ast.void_type
546545
use_expected_type := c.expected_type != ast.void_type && !c.inside_const
547546
&& c.table.sym(c.expected_type).kind == .map && !(c.inside_fn_arg

vlib/v/checker/fn.v

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,15 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
342342
if c.pref.skip_unused {
343343
if param.typ.has_flag(.generic) {
344344
c.table.used_features.comptime_syms[c.unwrap_generic(param.typ)] = true
345+
c.table.used_features.comptime_syms[param.typ] = true
345346
}
346347
if node.return_type.has_flag(.generic) {
347348
c.table.used_features.comptime_syms[c.unwrap_generic(node.return_type)] = true
349+
c.table.used_features.comptime_syms[node.return_type] = true
350+
}
351+
if node.receiver.typ.has_flag(.generic) {
352+
c.table.used_features.comptime_syms[node.receiver.typ] = true
353+
c.table.used_features.comptime_syms[c.unwrap_generic(node.receiver.typ)] = true
348354
}
349355
}
350356
if param.name == node.mod && param.name != 'main' {
@@ -1895,6 +1901,7 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
18951901
c.register_trace_call(node, func)
18961902
if func.return_type.has_flag(.generic) {
18971903
c.table.used_features.comptime_syms[typ.clear_option_and_result()] = true
1904+
c.table.used_features.comptime_syms[func.return_type] = true
18981905
}
18991906
return typ
19001907
}
@@ -2028,6 +2035,10 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool)
20282035
node.return_type = left_type
20292036
node.receiver_type = left_type
20302037

2038+
if is_generic {
2039+
c.table.used_features.comptime_syms[c.unwrap_generic(left_type)] = true
2040+
}
2041+
20312042
if c.table.cur_fn != unsafe { nil } && c.table.cur_fn.generic_names.len > 0 {
20322043
c.table.unwrap_generic_type(left_type, c.table.cur_fn.generic_names, c.table.cur_concrete_types)
20332044
}

vlib/v/checker/struct.v

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
6868
if node.language != .c && attr.name == 'typedef' {
6969
c.error('`typedef` attribute can only be used with C structs', node.pos)
7070
}
71-
aligned := if attr.arg == '' { 0 } else { attr.arg.int() }
72-
if aligned > 1 {
73-
c.table.used_features.memory_align = true
74-
}
7571
}
7672

7773
// Evaluate the size of the unresolved fixed array
@@ -533,6 +529,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
533529
true)
534530
if c.pref.skip_unused && node.typ.has_flag(.generic) {
535531
c.table.used_features.comptime_syms[c.unwrap_generic(node.typ)] = true
532+
c.table.used_features.comptime_syms[node.typ] = true
536533
}
537534
}
538535
if !is_field_zero_struct_init {
@@ -865,8 +862,6 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
865862
mut info := first_sym.info as ast.Struct
866863
c.check_uninitialized_struct_fields_and_embeds(node, first_sym, mut info, mut
867864
inited_fields)
868-
} else if first_sym.kind == .array {
869-
c.table.used_features.arr_init = true
870865
}
871866
}
872867
.none {

vlib/v/checker/used_features.v

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ fn (mut c Checker) markused_infixexpr(check bool) {
175175
return
176176
}
177177
c.table.used_features.index = true
178-
c.table.used_features.arr_init = true
179178
}
180179

181180
fn (mut c Checker) markused_array_method(check bool, method_name string) {
@@ -184,7 +183,6 @@ fn (mut c Checker) markused_array_method(check bool, method_name string) {
184183
}
185184
match method_name {
186185
'' { // array init
187-
c.table.used_features.arr_init = true
188186
}
189187
'first' {
190188
c.table.used_features.arr_first = true

0 commit comments

Comments
 (0)