Просмотр исходного кода

replace `std::vector<Expr_>` with `small_vector`

blueloveTH 2 лет назад
Родитель
Сommit
19a2b8950b
3 измененных файлов с 16 добавлено и 15 удалено
  1. 3 3
      include/pocketpy/compiler.h
  2. 5 4
      include/pocketpy/expr.h
  3. 8 8
      src/compiler.cpp

+ 3 - 3
include/pocketpy/compiler.h

@@ -124,10 +124,10 @@ class Compiler {
     bool try_compile_assignment();
     void compile_stmt();
     void consume_type_hints();
-    void _add_decorators(const std::vector<Expr_>& decorators);
-    void compile_class(const std::vector<Expr_>& decorators={});
+    void _add_decorators(const Expr_vector& decorators);
+    void compile_class(const Expr_vector& decorators={});
     void _compile_f_args(FuncDecl_ decl, bool enable_type_hints);
-    void compile_function(const std::vector<Expr_>& decorators={});
+    void compile_function(const Expr_vector& decorators={});
 
     PyObject* to_object(const TokenValue& value);
     PyObject* read_literal();

+ 5 - 4
include/pocketpy/expr.h

@@ -51,6 +51,7 @@ public:
 };
 
 typedef unique_ptr_64<Expr> Expr_;
+typedef small_vector<Expr_, 6> Expr_vector;
 
 struct Expr{
     int line = 0;
@@ -80,7 +81,7 @@ struct CodeEmitContext{
     VM* vm;
     FuncDecl_ func;     // optional
     CodeObject_ co;     // 1 CodeEmitContext <=> 1 CodeObject_
-    // some bugs on MSVC (error C2280) when using std::vector<Expr_>
+    // some bugs on MSVC (error C2280) when using Expr_vector
     // so we use stack_no_copy instead
     stack_no_copy<Expr_> s_expr;
     int level;
@@ -209,8 +210,8 @@ struct DictItemExpr: Expr{
 };
 
 struct SequenceExpr: Expr{
-    std::vector<Expr_> items;
-    SequenceExpr(std::vector<Expr_>&& items): items(std::move(items)) {}
+    Expr_vector items;
+    SequenceExpr(Expr_vector&& items): items(std::move(items)) {}
     virtual Opcode opcode() const = 0;
 
     void emit_(CodeEmitContext* ctx) override {
@@ -326,7 +327,7 @@ struct AttribExpr: Expr{
 
 struct CallExpr: Expr{
     Expr_ callable;
-    std::vector<Expr_> args;
+    Expr_vector args;
     // **a will be interpreted as a special keyword argument: {"**": a}
     std::vector<std::pair<Str, Expr_>> kwargs;
     void emit_(CodeEmitContext* ctx) override;

+ 8 - 8
src/compiler.cpp

@@ -180,7 +180,7 @@ namespace pkpy{
         parse_expression(PREC_LOWEST+1, allow_slice);
         if(!match(TK(","))) return;
         // tuple expression
-        std::vector<Expr_> items;
+        Expr_vector items;
         items.push_back(ctx()->s_expr.popx());
         do {
             if(curr().brackets_level) match_newlines_repl();
@@ -194,7 +194,7 @@ namespace pkpy{
 
     // special case for `for loop` and `comp`
     Expr_ Compiler::EXPR_VARS(){
-        std::vector<Expr_> items;
+        Expr_vector items;
         do {
             consume(TK("@id"));
             items.push_back(make_expr<NameExpr>(prev().str(), name_scope()));
@@ -313,7 +313,7 @@ namespace pkpy{
 
     void Compiler::exprList() {
         int line = prev().line;
-        std::vector<Expr_> items;
+        Expr_vector items;
         do {
             match_newlines_repl();
             if (curr().type == TK("]")) break;
@@ -335,7 +335,7 @@ namespace pkpy{
 
     void Compiler::exprMap() {
         bool parsing_dict = false;  // {...} may be dict or set
-        std::vector<Expr_> items;
+        Expr_vector items;
         do {
             match_newlines_repl();
             if (curr().type == TK("}")) break;
@@ -717,7 +717,7 @@ __EAT_DOTS_END:
     }
 
     void Compiler::compile_decorated(){
-        std::vector<Expr_> decorators;
+        Expr_vector decorators;
         do{
             EXPR();
             decorators.push_back(ctx()->s_expr.popx());
@@ -982,7 +982,7 @@ __EAT_DOTS_END:
         ctx()->s_expr.pop();
     }
 
-    void Compiler::_add_decorators(const std::vector<Expr_>& decorators){
+    void Compiler::_add_decorators(const Expr_vector& decorators){
         // [obj]
         for(auto it=decorators.rbegin(); it!=decorators.rend(); ++it){
             (*it)->emit_(ctx());                                    // [obj, f]
@@ -993,7 +993,7 @@ __EAT_DOTS_END:
         }
     }
 
-    void Compiler::compile_class(const std::vector<Expr_>& decorators){
+    void Compiler::compile_class(const Expr_vector& decorators){
         consume(TK("@id"));
         int namei = StrName(prev().sv()).index;
         Expr_ base = nullptr;
@@ -1092,7 +1092,7 @@ __EAT_DOTS_END:
         } while (match(TK(",")));
     }
 
-    void Compiler::compile_function(const std::vector<Expr_>& decorators){
+    void Compiler::compile_function(const Expr_vector& decorators){
         const char* _start = curr().start;
         consume(TK("@id"));
         Str decl_name = prev().str();