@@ -962,6 +962,19 @@ fn (mut b Builder) write_cached_called_fn_names(cache_dir string, cache_name str
962962 os.write_file (cached_called_fn_names_path (cache_dir, cache_name), names.join ('\n ' )) or {}
963963}
964964
965+ fn (b &Builder) cgen_builder_stats_enabled () bool {
966+ return b.pref != unsafe { nil } && b.pref.stats
967+ }
968+
969+ fn (b &Builder) mark_cgen_builder_step (stats_enabled bool , cache_name string , mut sw time.StopWatch, stage_start time.Duration, step string ) time.Duration {
970+ if ! stats_enabled {
971+ return stage_start
972+ }
973+ now := sw.elapsed ()
974+ println (' - C Gen/cache:${cache_name } cache.${step }: ${time .Duration (now - stage_start ).milliseconds ()}ms' )
975+ return now
976+ }
977+
965978fn cache_type_module_names (cache_bundle_name string , emit_modules []string ) []string {
966979 if cache_bundle_name == '' {
967980 return emit_modules
@@ -1457,17 +1470,29 @@ fn (mut b Builder) ensure_cached_module_object(cache_dir string, cache_name stri
14571470 return error ('missing cached ${cache_name } object for .vh parse' )
14581471 }
14591472
1473+ stats_enabled := b.cgen_builder_stats_enabled ()
1474+ mut stats_sw := time.new_stopwatch ()
1475+ mut stage_start := stats_sw.elapsed ()
14601476 cached_called_before := b.cached_called_fn_names.clone ()
1477+ stage_start = b.mark_cgen_builder_step (stats_enabled, cache_name, mut stats_sw, stage_start,
1478+ 'called_before_clone' )
14611479 module_source := b.gen_cleanc_source_for_cache (emit_modules, cache_name, use_markused)
1480+ stage_start = b.mark_cgen_builder_step (stats_enabled, cache_name, mut stats_sw, stage_start,
1481+ 'source' )
14621482 if module_source == '' {
14631483 return error ('failed to generate C source for ${cache_name }' )
14641484 }
14651485 os.write_file (c_path, module_source)!
1486+ stage_start = b.mark_cgen_builder_step (stats_enabled, cache_name, mut stats_sw, stage_start,
1487+ 'write_c' )
14661488
14671489 compile_cmd := '${cc } ${cc_flags } -w -Wno-incompatible-function-pointer-types -c "${c_path }" -o "${obj_path }"${error_limit_flag }'
14681490 run_cc_cmd_or_exit (compile_cmd, 'C compilation' , b.pref.show_cc)
1491+ stage_start =
1492+ b.mark_cgen_builder_step (stats_enabled, cache_name, mut stats_sw, stage_start, 'cc' )
14691493 b.write_cached_called_fn_names (cache_dir, cache_name, cached_called_before)
14701494 os.write_file (stamp_path, expected_stamp)!
1495+ _ = b.mark_cgen_builder_step (stats_enabled, cache_name, mut stats_sw, stage_start, 'metadata' )
14711496 return obj_path
14721497}
14731498
@@ -1499,17 +1524,29 @@ fn (mut b Builder) ensure_cached_parsed_module_object(cache_dir string, cache_na
14991524 return error ('missing cached ${cache_name } object for .vh parse' )
15001525 }
15011526
1527+ stats_enabled := b.cgen_builder_stats_enabled ()
1528+ mut stats_sw := time.new_stopwatch ()
1529+ mut stage_start := stats_sw.elapsed ()
15021530 cached_called_before := b.cached_called_fn_names.clone ()
1531+ stage_start = b.mark_cgen_builder_step (stats_enabled, cache_name, mut stats_sw, stage_start,
1532+ 'called_before_clone' )
15031533 mut module_source := b.gen_cleanc_source_for_cache (module_names, cache_name, use_markused)
1534+ stage_start = b.mark_cgen_builder_step (stats_enabled, cache_name, mut stats_sw, stage_start,
1535+ 'source' )
15041536 if module_source == '' {
15051537 return error ('failed to generate C source for ${cache_name }' )
15061538 }
15071539 os.write_file (c_path, module_source)!
1540+ stage_start = b.mark_cgen_builder_step (stats_enabled, cache_name, mut stats_sw, stage_start,
1541+ 'write_c' )
15081542
15091543 compile_cmd := '${cc } ${cc_flags } -w -Wno-incompatible-function-pointer-types -c "${c_path }" -o "${obj_path }"${error_limit_flag }'
15101544 run_cc_cmd_or_exit (compile_cmd, 'C compilation' , b.pref.show_cc)
1545+ stage_start =
1546+ b.mark_cgen_builder_step (stats_enabled, cache_name, mut stats_sw, stage_start, 'cc' )
15111547 b.write_cached_called_fn_names (cache_dir, cache_name, cached_called_before)
15121548 os.write_file (stamp_path, expected_stamp)!
1549+ _ = b.mark_cgen_builder_step (stats_enabled, cache_name, mut stats_sw, stage_start, 'metadata' )
15131550 return obj_path
15141551}
15151552
@@ -1542,18 +1579,31 @@ fn (mut b Builder) ensure_cached_virtual_module_object(cache_dir string, groups
15421579 }
15431580
15441581 emit_files := virtual_module_source_files (groups)
1582+ stats_enabled := b.cgen_builder_stats_enabled ()
1583+ mut stats_sw := time.new_stopwatch ()
1584+ mut stage_start := stats_sw.elapsed ()
15451585 cached_called_before := b.cached_called_fn_names.clone ()
1586+ stage_start = b.mark_cgen_builder_step (stats_enabled, virtuals_cache_name, mut stats_sw,
1587+ stage_start, 'called_before_clone' )
15461588 mut module_source := b.gen_cleanc_source_for_cache_files (['main' ], emit_files,
15471589 virtuals_cache_name, use_markused)
1590+ stage_start = b.mark_cgen_builder_step (stats_enabled, virtuals_cache_name, mut stats_sw,
1591+ stage_start, 'source' )
15481592 if module_source == '' {
15491593 return error ('failed to generate C source for ${virtuals_cache_name }' )
15501594 }
15511595 os.write_file (c_path, module_source)!
1596+ stage_start = b.mark_cgen_builder_step (stats_enabled, virtuals_cache_name, mut stats_sw,
1597+ stage_start, 'write_c' )
15521598
15531599 compile_cmd := '${cc } ${cc_flags } -w -Wno-incompatible-function-pointer-types -c "${c_path }" -o "${obj_path }"${error_limit_flag }'
15541600 run_cc_cmd_or_exit (compile_cmd, 'C compilation' , b.pref.show_cc)
1601+ stage_start = b.mark_cgen_builder_step (stats_enabled, virtuals_cache_name, mut stats_sw,
1602+ stage_start, 'cc' )
15551603 b.write_cached_called_fn_names (cache_dir, virtuals_cache_name, cached_called_before)
15561604 os.write_file (stamp_path, expected_stamp)!
1605+ _ = b.mark_cgen_builder_step (stats_enabled, virtuals_cache_name, mut stats_sw, stage_start,
1606+ 'metadata' )
15571607 return obj_path
15581608}
15591609
0 commit comments