Răsfoiți Sursa

improve exception performance

blueloveTH 2 ani în urmă
părinte
comite
2cf3bdc097
4 a modificat fișierele cu 33 adăugiri și 15 ștergeri
  1. 24 4
      include/pocketpy/error.h
  2. 6 8
      src/error.cpp
  3. 2 2
      src/lexer.cpp
  4. 1 1
      src/vm.cpp

+ 24 - 4
include/pocketpy/error.h

@@ -34,21 +34,41 @@ struct SourceData {
 
 
     SourceData(const Str& source, const Str& filename, CompileMode mode);
     SourceData(const Str& source, const Str& filename, CompileMode mode);
     std::pair<const char*,const char*> get_line(int lineno) const;
     std::pair<const char*,const char*> get_line(int lineno) const;
-    Str snapshot(int lineno, const char* cursor=nullptr, std::string_view name="");
+    Str snapshot(int lineno, const char* cursor, std::string_view name) const;
+};
+
+struct ExceptionLine{
+    std::shared_ptr<SourceData> src;
+    int lineno;
+    const char* cursor;
+    std::string name;
+
+    Str snapshot() const { return src->snapshot(lineno, cursor, name); }
+
+    ExceptionLine(std::shared_ptr<SourceData> src, int lineno, const char* cursor, std::string_view name):
+        src(src), lineno(lineno), cursor(cursor), name(name) {}
 };
 };
 
 
 struct Exception {
 struct Exception {
     StrName type;
     StrName type;
     Str msg;
     Str msg;
     bool is_re;
     bool is_re;
-    stack<Str> stacktrace;
 
 
     int _ip_on_error;
     int _ip_on_error;
     void* _code_on_error;
     void* _code_on_error;
+    
+    stack<ExceptionLine> stacktrace;
 
 
-    Exception(StrName type, Str msg): type(type), msg(msg), is_re(true), _ip_on_error(-1), _code_on_error(nullptr) {}
+    Exception(StrName type, Str msg): 
+        type(type), msg(msg), is_re(true), _ip_on_error(-1), _code_on_error(nullptr) {}
     bool match_type(StrName t) const { return this->type == t;}
     bool match_type(StrName t) const { return this->type == t;}
-    void st_push(Str&& snapshot);
+
+    template<typename... Args>
+    void st_push(Args&&... args){
+        if(stacktrace.size() >= 8) return;
+        stacktrace.emplace(std::forward<Args>(args)...);
+    }
+
     Str summary() const;
     Str summary() const;
 };
 };
 
 

+ 6 - 8
src/error.cpp

@@ -29,7 +29,7 @@ namespace pkpy{
         return {_start, i};
         return {_start, i};
     }
     }
 
 
-    Str SourceData::snapshot(int lineno, const char* cursor, std::string_view name){
+    Str SourceData::snapshot(int lineno, const char* cursor, std::string_view name) const{
         std::stringstream ss;
         std::stringstream ss;
         ss << "  " << "File \"" << filename << "\", line " << lineno;
         ss << "  " << "File \"" << filename << "\", line " << lineno;
         if(!name.empty()) ss << ", in " << name;
         if(!name.empty()) ss << ", in " << name;
@@ -50,16 +50,14 @@ namespace pkpy{
         return ss.str();
         return ss.str();
     }
     }
 
 
-    void Exception::st_push(Str&& snapshot){
-        if(stacktrace.size() >= 8) return;
-        stacktrace.push(std::move(snapshot));
-    }
-
     Str Exception::summary() const {
     Str Exception::summary() const {
-        stack<Str> st(stacktrace);
+        stack<ExceptionLine> st(stacktrace);
         std::stringstream ss;
         std::stringstream ss;
         if(is_re) ss << "Traceback (most recent call last):\n";
         if(is_re) ss << "Traceback (most recent call last):\n";
-        while(!st.empty()) { ss << st.top() << '\n'; st.pop(); }
+        while(!st.empty()) {
+            ss << st.top().snapshot() << '\n';
+            st.pop();
+        }
         if (!msg.empty()) ss << type.sv() << ": " << msg;
         if (!msg.empty()) ss << type.sv() << ": " << msg;
         else ss << type.sv();
         else ss << type.sv();
         return ss.str();
         return ss.str();

+ 2 - 2
src/lexer.cpp

@@ -454,8 +454,8 @@ static bool is_unicode_Lo_char(uint32_t c) {
     }
     }
 
 
     void Lexer::throw_err(Str type, Str msg, int lineno, const char* cursor){
     void Lexer::throw_err(Str type, Str msg, int lineno, const char* cursor){
-        auto e = Exception(type, msg);
-        e.st_push(src->snapshot(lineno, cursor));
+        Exception e(type, msg);
+        e.st_push(src, lineno, cursor, "");
         throw e;
         throw e;
     }
     }
 
 

+ 1 - 1
src/vm.cpp

@@ -1095,7 +1095,7 @@ void VM::_raise(bool re_raise){
     int current_line = frame->co->lines[actual_ip];         // current line
     int current_line = frame->co->lines[actual_ip];         // current line
     auto current_f_name = frame->co->name.sv();             // current function name
     auto current_f_name = frame->co->name.sv();             // current function name
     if(frame->_callable == nullptr) current_f_name = "";    // not in a function
     if(frame->_callable == nullptr) current_f_name = "";    // not in a function
-    e.st_push(frame->co->src->snapshot(current_line, nullptr, current_f_name));
+    e.st_push(frame->co->src, current_line, nullptr, current_f_name);
 
 
     if(ok) throw HandledException();
     if(ok) throw HandledException();
     else throw UnhandledException();
     else throw UnhandledException();