From 65f6866b985fb3d0993fc2b6798c74015fb4fa6a Mon Sep 17 00:00:00 2001 From: skal Date: Fri, 13 Feb 2026 08:55:18 +0100 Subject: Refactor: Extract duplicate logic in compiler tools Consolidate repeated timeline/resource analysis code to improve maintainability and reduce duplication. seq_compiler.cc changes: - Extract timeline analysis (max time, sorting) into analyze_timeline() - Extract sequence end calculation into get_sequence_end() - Reduces ~45 lines of duplicate code tracker_compiler.cc changes: - Extract resource analysis into ResourceAnalysis struct - Consolidate sample counting and recommendations - Reduces ~75 lines of duplicate code Both tools verified with successful builds. Co-Authored-By: Claude Sonnet 4.5 --- tools/seq_compiler.cc | 130 +++++++++++++++++++++----------------------------- 1 file changed, 55 insertions(+), 75 deletions(-) (limited to 'tools/seq_compiler.cc') diff --git a/tools/seq_compiler.cc b/tools/seq_compiler.cc index daf1294..2448a3b 100644 --- a/tools/seq_compiler.cc +++ b/tools/seq_compiler.cc @@ -91,20 +91,55 @@ int calculate_tick_interval(float max_time) { return 20; } -// Analyze effect stacking depth across the timeline -void analyze_effect_depth(const std::vector& sequences, - const std::string& demo_end_time, - float sample_rate = 10.0f) { - // Find max time for analysis +// Timeline analysis result: max time and sequences sorted by start time +struct TimelineMetrics { + float max_time; + std::vector sorted_sequences; +}; + +// Calculate sequence end time (explicit or derived from latest effect) +float get_sequence_end(const SequenceEntry& seq) { + float seq_start = std::stof(seq.start_time); + if (seq.end_time != "-1.0") { + return seq_start + std::stof(seq.end_time); + } + float seq_end = seq_start; + for (const auto& eff : seq.effects) { + seq_end = std::max(seq_end, seq_start + std::stof(eff.end)); + } + return seq_end; +} + +// Analyze timeline: find max time and sort sequences by start time +TimelineMetrics analyze_timeline(const std::vector& sequences, + const std::string& demo_end_time) { float max_time = demo_end_time.empty() ? 0.0f : std::stof(demo_end_time); for (const auto& seq : sequences) { float seq_start = std::stof(seq.start_time); for (const auto& eff : seq.effects) { - float eff_end = seq_start + std::stof(eff.end); - max_time = std::max(max_time, eff_end); + max_time = std::max(max_time, seq_start + std::stof(eff.end)); + } + if (seq.end_time != "-1.0") { + max_time = std::max(max_time, seq_start + std::stof(seq.end_time)); } } + std::vector sorted = sequences; + std::sort(sorted.begin(), sorted.end(), + [](const SequenceEntry& a, const SequenceEntry& b) { + return std::stof(a.start_time) < std::stof(b.start_time); + }); + + return {max_time, sorted}; +} + +// Analyze effect stacking depth across the timeline +void analyze_effect_depth(const std::vector& sequences, + const std::string& demo_end_time, + float sample_rate = 10.0f) { + TimelineMetrics metrics = analyze_timeline(sequences, demo_end_time); + float max_time = metrics.max_time; + if (max_time <= 0.0f) { std::cout << "\n=== Effect Depth Analysis ===\n"; std::cout << "No effects found in timeline.\n"; @@ -122,10 +157,7 @@ void analyze_effect_depth(const std::vector& sequences, for (const auto& seq : sequences) { float seq_start = std::stof(seq.start_time); - float seq_end = seq_start; - if (seq.end_time != "-1.0") { - seq_end = seq_start + std::stof(seq.end_time); - } + float seq_end = get_sequence_end(seq); for (const auto& eff : seq.effects) { float eff_start = seq_start + std::stof(eff.start); @@ -268,18 +300,8 @@ void generate_gantt_chart(const std::string& output_file, return; } - // Find max time for the chart - float max_time = demo_end_time.empty() ? 0.0f : std::stof(demo_end_time); - for (const auto& seq : sequences) { - float seq_start = std::stof(seq.start_time); - for (const auto& eff : seq.effects) { - float eff_end = seq_start + std::stof(eff.end); - max_time = std::max(max_time, eff_end); - } - if (seq.end_time != "-1.0") { - max_time = std::max(max_time, seq_start + std::stof(seq.end_time)); - } - } + TimelineMetrics metrics = analyze_timeline(sequences, demo_end_time); + float max_time = metrics.max_time; // Chart configuration const int chart_width = 100; @@ -320,28 +342,11 @@ void generate_gantt_chart(const std::string& output_file, } out << "\n\n"; - // Sort sequences by start time for better readability - std::vector sorted_sequences = sequences; - std::sort(sorted_sequences.begin(), sorted_sequences.end(), - [](const SequenceEntry& a, const SequenceEntry& b) { - return std::stof(a.start_time) < std::stof(b.start_time); - }); - // Draw sequences and effects - for (size_t seq_idx = 0; seq_idx < sorted_sequences.size(); ++seq_idx) { - const auto& seq = sorted_sequences[seq_idx]; + for (size_t seq_idx = 0; seq_idx < metrics.sorted_sequences.size(); ++seq_idx) { + const auto& seq = metrics.sorted_sequences[seq_idx]; float seq_start = std::stof(seq.start_time); - float seq_end = seq_start; // Start at sequence start - - // Check if sequence has explicit end time - if (seq.end_time != "-1.0") { - seq_end = seq_start + std::stof(seq.end_time); - } else { - // Calculate implicit end from latest effect - for (const auto& eff : seq.effects) { - seq_end = std::max(seq_end, seq_start + std::stof(eff.end)); - } - } + float seq_end = get_sequence_end(seq); // Draw sequence bar out << "SEQ@" << seq_start << "s"; @@ -398,7 +403,7 @@ void generate_gantt_chart(const std::string& output_file, } // Add separator between sequences - if (seq_idx < sorted_sequences.size() - 1) { + if (seq_idx < metrics.sorted_sequences.size() - 1) { out << " "; for (int i = 0; i < chart_width; ++i) { out << "─"; @@ -429,18 +434,8 @@ void generate_gantt_html(const std::string& output_file, return; } - // Find max time for the chart - float max_time = demo_end_time.empty() ? 0.0f : std::stof(demo_end_time); - for (const auto& seq : sequences) { - float seq_start = std::stof(seq.start_time); - for (const auto& eff : seq.effects) { - float eff_end = seq_start + std::stof(eff.end); - max_time = std::max(max_time, eff_end); - } - if (seq.end_time != "-1.0") { - max_time = std::max(max_time, seq_start + std::stof(seq.end_time)); - } - } + TimelineMetrics metrics = analyze_timeline(sequences, demo_end_time); + float max_time = metrics.max_time; const int svg_width = 1400; const int row_height = 30; @@ -513,27 +508,12 @@ void generate_gantt_html(const std::string& output_file, << "\" y2=\"" << svg_height - 20 << "\" class=\"time-marker\"/>\n"; } - // Sort sequences by start time for better readability - std::vector sorted_sequences = sequences; - std::sort(sorted_sequences.begin(), sorted_sequences.end(), - [](const SequenceEntry& a, const SequenceEntry& b) { - return std::stof(a.start_time) < std::stof(b.start_time); - }); - // Draw sequences and effects int y_offset = margin_top; - for (size_t seq_idx = 0; seq_idx < sorted_sequences.size(); ++seq_idx) { - const auto& seq = sorted_sequences[seq_idx]; + for (size_t seq_idx = 0; seq_idx < metrics.sorted_sequences.size(); ++seq_idx) { + const auto& seq = metrics.sorted_sequences[seq_idx]; float seq_start = std::stof(seq.start_time); - float seq_end = seq_start; // Start at sequence start - - if (seq.end_time != "-1.0") { - seq_end = seq_start + std::stof(seq.end_time); - } else { - for (const auto& eff : seq.effects) { - seq_end = std::max(seq_end, seq_start + std::stof(eff.end)); - } - } + float seq_end = get_sequence_end(seq); int x1 = margin_left + (int)(seq_start * time_scale); int x2 = margin_left + (int)(seq_end * time_scale); @@ -592,7 +572,7 @@ void generate_gantt_html(const std::string& output_file, } // Add separator between sequences - if (seq_idx < sorted_sequences.size() - 1) { + if (seq_idx < metrics.sorted_sequences.size() - 1) { out << " \n"; out << "