Преглед на файлове

optimize `profiler.cpp`

blueloveTH преди 2 години
родител
ревизия
e88f57ae30
променени са 2 файла, в които са добавени 16 реда и са изтрити 8 реда
  1. 3 2
      include/pocketpy/profiler.h
  2. 13 6
      src/profiler.cpp

+ 3 - 2
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<std::string_view, std::map<int, LineRecord>> records;
+    std::map<std::string_view, std::vector<LineRecord>> records;
 
     void begin();
     void _step(Frame* frame);

+ 13 - 6
src/profiler.cpp

@@ -37,9 +37,14 @@ void LineProfiler::_step(Frame *frame){
         _step_end();
     }
 
-    std::map<int, LineRecord>& file_records = records[filename];
-    auto [it, ok] = file_records.insert({line, LineRecord(line, frame->co->src.get())});
-    prev_record = &(it->second);
+    std::vector<LineRecord>& 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);