From 19a2b8950b28282a12d496bf8ad4f6e2222021ab Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 18 Feb 2024 21:31:27 +0800 Subject: [PATCH] replace `std::vector` with `small_vector` --- include/pocketpy/compiler.h | 6 +++--- include/pocketpy/expr.h | 9 +++++---- src/compiler.cpp | 16 ++++++++-------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/include/pocketpy/compiler.h b/include/pocketpy/compiler.h index 3faf07c5..499eed8f 100644 --- a/include/pocketpy/compiler.h +++ b/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& decorators); - void compile_class(const std::vector& 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& decorators={}); + void compile_function(const Expr_vector& decorators={}); PyObject* to_object(const TokenValue& value); PyObject* read_literal(); diff --git a/include/pocketpy/expr.h b/include/pocketpy/expr.h index 4eba89f6..c54da59b 100644 --- a/include/pocketpy/expr.h +++ b/include/pocketpy/expr.h @@ -51,6 +51,7 @@ public: }; typedef unique_ptr_64 Expr_; +typedef small_vector 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 + // some bugs on MSVC (error C2280) when using Expr_vector // so we use stack_no_copy instead stack_no_copy s_expr; int level; @@ -209,8 +210,8 @@ struct DictItemExpr: Expr{ }; struct SequenceExpr: Expr{ - std::vector items; - SequenceExpr(std::vector&& 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 args; + Expr_vector args; // **a will be interpreted as a special keyword argument: {"**": a} std::vector> kwargs; void emit_(CodeEmitContext* ctx) override; diff --git a/src/compiler.cpp b/src/compiler.cpp index bfa7c58d..b9554708 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -180,7 +180,7 @@ namespace pkpy{ parse_expression(PREC_LOWEST+1, allow_slice); if(!match(TK(","))) return; // tuple expression - std::vector 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 items; + Expr_vector items; do { consume(TK("@id")); items.push_back(make_expr(prev().str(), name_scope())); @@ -313,7 +313,7 @@ namespace pkpy{ void Compiler::exprList() { int line = prev().line; - std::vector 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 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 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& 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& 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& decorators){ + void Compiler::compile_function(const Expr_vector& decorators){ const char* _start = curr().start; consume(TK("@id")); Str decl_name = prev().str();