From 415c1f6b38969ea58189c37af8663b8676d0830b Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 18 Feb 2024 22:43:09 +0800 Subject: [PATCH] some more replace --- include/pocketpy/codeobject.h | 11 ++++++----- include/pocketpy/frame.h | 6 ++++-- include/pocketpy/lexer.h | 2 +- include/pocketpy/vector.h | 2 ++ include/pocketpy/vm.h | 2 +- src/codeobject.cpp | 4 +++- src/vm.cpp | 1 - 7 files changed, 17 insertions(+), 11 deletions(-) diff --git a/include/pocketpy/codeobject.h b/include/pocketpy/codeobject.h index 0b32860d..bb37c1eb 100644 --- a/include/pocketpy/codeobject.h +++ b/include/pocketpy/codeobject.h @@ -70,10 +70,11 @@ struct CodeObject { std::vector codes; std::vector iblocks; // block index for each bytecode std::vector lines; - List consts; - pod_vector varnames; // local variables + + small_vector consts; + small_vector varnames; // local variables NameDictInt varnames_inv; - std::vector blocks = { CodeBlock(CodeBlockType::NO_BLOCK, -1, 0, 0) }; + small_vector blocks; NameDictInt labels; std::vector func_decls; @@ -95,8 +96,8 @@ struct FuncDecl { PyObject* value; // default value }; CodeObject_ code; // code object of this function - pod_vector args; // indices in co->varnames - pod_vector kwargs; // indices in co->varnames + small_vector args; // indices in co->varnames + small_vector kwargs; // indices in co->varnames int starred_arg = -1; // index in co->varnames, -1 if no *arg int starred_kwarg = -1; // index in co->varnames, -1 if no **kwarg bool nested = false; // whether this function is nested diff --git a/include/pocketpy/frame.h b/include/pocketpy/frame.h index ad3ad113..28ef6b9e 100644 --- a/include/pocketpy/frame.h +++ b/include/pocketpy/frame.h @@ -126,10 +126,12 @@ struct Frame { } }; +using CallstackContainer = small_vector; + struct FrameId{ - std::vector* data; + CallstackContainer* data; int index; - FrameId(std::vector* data, int index) : data(data), index(index) {} + FrameId(CallstackContainer* data, int index) : data(data), index(index) {} Frame* operator->() const { return &data->operator[](index); } Frame* get() const { return &data->operator[](index); } }; diff --git a/include/pocketpy/lexer.h b/include/pocketpy/lexer.h index 97fed808..34d0f819 100644 --- a/include/pocketpy/lexer.h +++ b/include/pocketpy/lexer.h @@ -104,7 +104,7 @@ struct Lexer { const char* curr_char; int current_line = 1; std::vector nexts; - stack_no_copy> indents; + stack_no_copy> indents; int brackets_level = 0; bool used = false; diff --git a/include/pocketpy/vector.h b/include/pocketpy/vector.h index f2dcea15..e1aeda58 100644 --- a/include/pocketpy/vector.h +++ b/include/pocketpy/vector.h @@ -259,6 +259,8 @@ namespace pkpy { reverse_iterator rbegin() { return reverse_iterator(end()); } + void clear() { while (m_size > 0) pop_back(); } + const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 89e82f98..eee9fdaf 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -112,7 +112,7 @@ class VM { public: ManagedHeap heap; ValueStack s_data; - stack< Frame > callstack; + stack_no_copy callstack; std::vector _all_types; NameDict _modules; // loaded modules diff --git a/src/codeobject.cpp b/src/codeobject.cpp index 79bd9f3c..ef156c18 100644 --- a/src/codeobject.cpp +++ b/src/codeobject.cpp @@ -3,7 +3,9 @@ namespace pkpy{ CodeObject::CodeObject(std::shared_ptr src, const Str& name): - src(src), name(name), start_line(-1), end_line(-1) {} + src(src), name(name), start_line(-1), end_line(-1) { + blocks.push_back(CodeBlock(CodeBlockType::NO_BLOCK, -1, 0, 0)); + } void CodeObject::_gc_mark() const { for(PyObject* v : consts) PK_OBJ_MARK(v); diff --git a/src/vm.cpp b/src/vm.cpp index 83f07682..5bf3bb33 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -75,7 +75,6 @@ namespace pkpy{ _stderr = [](const char* buf, int size) { std::cerr.write(buf, size); }; - callstack.reserve(8); _main = nullptr; _last_exception = nullptr; _import_handler = [](const char* name_p, int name_size, int* out_size) -> unsigned char*{