diff --git a/include/pocketpy/profiler.h b/include/pocketpy/profiler.h index 82c077ac..7231c447 100644 --- a/include/pocketpy/profiler.h +++ b/include/pocketpy/profiler.h @@ -11,9 +11,10 @@ struct LineRecord{ i64 hits; clock_t time; - LineRecord(int line, SourceData* src): line(line), src(src), hits(0), time(0) {} + LineRecord(): line(-1), src(nullptr), hits(0), time(0) {} std::string_view line_content() const; + bool is_valid() const { return src != nullptr; } }; struct LineProfiler{ @@ -22,7 +23,7 @@ struct LineProfiler{ int prev_line; // filename -> records - std::map> records; + std::map> records; void begin(); void _step(Frame* frame); diff --git a/src/profiler.cpp b/src/profiler.cpp index b9780b02..e7a15748 100644 --- a/src/profiler.cpp +++ b/src/profiler.cpp @@ -37,9 +37,14 @@ void LineProfiler::_step(Frame *frame){ _step_end(); } - std::map& file_records = records[filename]; - auto [it, ok] = file_records.insert({line, LineRecord(line, frame->co->src.get())}); - prev_record = &(it->second); + std::vector& file_records = records[filename]; + if(file_records.empty()) file_records.resize(frame->co->src->line_starts.size() + 10); + + prev_record = &file_records[line]; + if(!prev_record->is_valid()){ + prev_record->line = line; + prev_record->src = frame->co->src.get(); + } } void LineProfiler::_step_end(){ @@ -61,15 +66,17 @@ Str LineProfiler::stats(){ SStream ss; for(auto& [filename, file_records] : records){ clock_t total_time = 0; - for(auto& [line, record] : file_records){ - total_time += record.time; + for(auto& record: file_records){ + if(record.is_valid()) total_time += record.time; } ss << "Total time: " << (f64)total_time / CLOCKS_PER_SEC << "s\n"; ss << "File: " << filename << "\n"; // ss << "Function: " << "" << "at line " << -1 << "\n"; ss << "Line # Hits Time Per Hit % Time Line Contents\n"; ss << "==============================================================\n"; - for(auto& [line, record]: file_records){ + for(int line = 1; line < file_records.size(); line++){ + LineRecord& record = file_records[line]; + if(!record.is_valid()) continue; ss << left_pad(std::to_string(line), 6); ss << left_pad(std::to_string(record.hits), 10); ss << left_pad(std::to_string(record.time), 13);