This commit is contained in:
blueloveTH 2024-06-06 23:30:12 +08:00
parent b62bcb5a99
commit 35cb1fba24
4 changed files with 18 additions and 19 deletions

View File

@ -196,12 +196,6 @@ struct vector {
_capacity = cap;
}
void resize(int size) {
reserve(size);
std::uninitialized_default_construct_n(_data + _size, size - _size);
_size = size;
}
template <typename... Args>
void emplace_back(Args&&... args) {
if(_size == _capacity) reserve(_capacity * 2);
@ -432,7 +426,7 @@ struct small_map {
Item* data() const { return _data.data(); }
void insert(const K& key, const V& value) {
auto it = std::lower_bound(_data.begin(), _data.end(), key);
Item* it = std::lower_bound(_data.begin(), _data.end(), key);
assert(it == _data.end() || it->first != key);
_data.insert(it, {key, value});
}

View File

@ -25,7 +25,7 @@ struct _FrameRecord {
struct LineProfiler {
// filename -> records
small_map<std::string_view, vector<_LineRecord>> records;
small_map<std::string_view, _LineRecord*> records;
vector<_FrameRecord> frames;
vector<FuncDecl*> functions;
@ -34,6 +34,7 @@ struct LineProfiler {
void _step_end(int, Frame*, int);
void end();
Str stats();
~LineProfiler();
};
} // namespace pkpy

View File

@ -28,14 +28,16 @@ void LineProfiler::_step(int callstack_size, Frame* frame) {
_step_end(callstack_size, frame, line);
}
auto& file_records = *records.try_get(filename);
if(file_records.empty()) {
// initialize file_records
_LineRecord* file_records;
auto p = records.try_get(filename);
if(p == nullptr) {
int total_lines = frame->co->src->line_starts.size();
file_records.resize(total_lines + 1);
for(int i = 1; i <= total_lines; i++) {
file_records[i].line = i;
}
file_records = new _LineRecord[total_lines + 1];
for(int i = 1; i <= total_lines; i++) file_records[i].line = i;
records.insert(filename, file_records);
}else{
file_records = *p;
}
frames.back().prev_record = &file_records[line];
@ -85,8 +87,7 @@ Str LineProfiler::stats() {
int end_line = decl->code->end_line;
if(start_line == -1 || end_line == -1) continue;
std::string_view filename = decl->code->src->filename.sv();
vector<_LineRecord>& file_records = *records.try_get(filename);
if(file_records.empty()) continue;
const _LineRecord* file_records = records[filename];
clock_t total_time = 0;
for(int line = start_line; line <= end_line; line++) {
total_time += file_records[line].time;
@ -120,4 +121,8 @@ Str LineProfiler::stats() {
return ss.str();
}
LineProfiler::~LineProfiler() {
for(auto& p: records) delete p.second;
}
} // namespace pkpy

View File

@ -25,8 +25,7 @@ std::string pkpy_platform_getline(bool* eof) {
}
std::wstring wideInput = wss.str();
int length = WideCharToMultiByte(CP_UTF8, 0, wideInput.c_str(), (int)wideInput.length(), NULL, 0, NULL, NULL);
std::string output;
output.resize(length);
std::string output(length);
WideCharToMultiByte(CP_UTF8, 0, wideInput.c_str(), (int)wideInput.length(), &output[0], length, NULL, NULL);
if(!output.empty() && output.back() == '\r') output.pop_back();
return output;