blueloveTH 2 лет назад
Родитель
Сommit
415c1f6b38

+ 6 - 5
include/pocketpy/codeobject.h

@@ -70,10 +70,11 @@ struct CodeObject {
     std::vector<Bytecode> codes;
     std::vector<int> iblocks;       // block index for each bytecode
     std::vector<LineInfo> lines;
-    List consts;
-    pod_vector<StrName> varnames;      // local variables
+    
+    small_vector<PyObject*, 6> consts;
+    small_vector<StrName, 16> varnames;      // local variables
     NameDictInt varnames_inv;
-    std::vector<CodeBlock> blocks = { CodeBlock(CodeBlockType::NO_BLOCK, -1, 0, 0) };
+    small_vector<CodeBlock, 4> blocks;
     NameDictInt labels;
     std::vector<FuncDecl_> func_decls;
 
@@ -95,8 +96,8 @@ struct FuncDecl {
         PyObject* value;        // default value
     };
     CodeObject_ code;           // code object of this function
-    pod_vector<int> args;      // indices in co->varnames
-    pod_vector<KwArg> kwargs;  // indices in co->varnames
+    small_vector<int, 4> args;      // indices in co->varnames
+    small_vector<KwArg, 4> 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

+ 4 - 2
include/pocketpy/frame.h

@@ -126,10 +126,12 @@ struct Frame {
     }
 };
 
+using CallstackContainer = small_vector<Frame, 8>;
+
 struct FrameId{
-    std::vector<pkpy::Frame>* data;
+    CallstackContainer* data;
     int index;
-    FrameId(std::vector<pkpy::Frame>* 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); }
 };

+ 1 - 1
include/pocketpy/lexer.h

@@ -104,7 +104,7 @@ struct Lexer {
     const char* curr_char;
     int current_line = 1;
     std::vector<Token> nexts;
-    stack_no_copy<int, pod_vector<int>> indents;
+    stack_no_copy<int, small_vector<int, 6>> indents;
     int brackets_level = 0;
     bool used = false;
 

+ 2 - 0
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());
         }

+ 1 - 1
include/pocketpy/vm.h

@@ -112,7 +112,7 @@ class VM {
 public:
     ManagedHeap heap;
     ValueStack s_data;
-    stack< Frame > callstack;
+    stack_no_copy<Frame, CallstackContainer> callstack;
     std::vector<PyTypeInfo> _all_types;
     
     NameDict _modules;                                 // loaded modules

+ 3 - 1
src/codeobject.cpp

@@ -3,7 +3,9 @@
 namespace pkpy{
 
     CodeObject::CodeObject(std::shared_ptr<SourceData> 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);

+ 0 - 1
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*{