diff --git a/include/pocketpy/codeobject.h b/include/pocketpy/codeobject.h index db3eec05..4da74cdc 100644 --- a/include/pocketpy/codeobject.h +++ b/include/pocketpy/codeobject.h @@ -71,7 +71,8 @@ struct CodeObject { std::vector iblocks; // block index for each bytecode std::vector lines; - List consts; + small_vector consts; + pod_vector varnames; // local variables NameDictInt varnames_inv; std::vector blocks; diff --git a/include/pocketpy/frame.h b/include/pocketpy/frame.h index b5d84902..06df37cd 100644 --- a/include/pocketpy/frame.h +++ b/include/pocketpy/frame.h @@ -126,7 +126,7 @@ struct Frame { } }; -using CallstackContainer = std::vector; +using CallstackContainer = small_vector; struct FrameId{ CallstackContainer* data; diff --git a/include/pocketpy/vector.h b/include/pocketpy/vector.h index 62470969..73c02038 100644 --- a/include/pocketpy/vector.h +++ b/include/pocketpy/vector.h @@ -162,7 +162,8 @@ public: const T& top() const { return vec.back(); } T popx(){ T t = std::move(vec.back()); vec.pop_back(); return t; } void reserve(int n){ vec.reserve(n); } - Container& data() { return vec; } + Container& container() { return vec; } + const Container& container() const { return vec; } }; template > @@ -229,8 +230,8 @@ namespace pkpy pointer data() { return m_begin; } const_pointer data() const { return m_begin; } - reference operator[](size_type index) { return data()[index]; } - const_reference operator[](size_type index) const { return data()[index]; } + reference operator[](size_type index) { return m_begin[index]; } + const_reference operator[](size_type index) const { return m_begin[index]; } iterator begin() { return m_begin; } const_iterator begin() const { return m_begin; } iterator end() { return m_end; } diff --git a/src/compiler.cpp b/src/compiler.cpp index b9554708..dd4fb3cd 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -1011,7 +1011,7 @@ __EAT_DOTS_END: } ctx()->emit_(OP_BEGIN_CLASS, namei, BC_KEEPLINE); - for(auto& c: this->contexts.data()){ + for(auto& c: this->contexts.container()){ if(c.is_compiling_class){ SyntaxError("nested class is not allowed"); } diff --git a/src/vm.cpp b/src/vm.cpp index 5ea4804b..b63e9541 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -69,7 +69,6 @@ namespace pkpy{ VM::VM(bool enable_os) : heap(this), enable_os(enable_os) { this->vm = this; this->_c.error = nullptr; - this->callstack.reserve(8); _stdout = [](const char* buf, int size) { std::cout.write(buf, size); }; _stderr = [](const char* buf, int size) { std::cerr.write(buf, size); }; _main = nullptr; @@ -124,7 +123,7 @@ namespace pkpy{ #if PK_DEBUG_EXTRA_CHECK if(callstack.empty()) PK_FATAL_ERROR(); #endif - return FrameId(&callstack.data(), callstack.size()-1); + return FrameId(&callstack.container(), callstack.size()-1); } void VM::_pop_frame(){ @@ -1262,7 +1261,7 @@ void VM::_raise(bool re_raise){ void ManagedHeap::mark() { for(PyObject* obj: _no_gc) PK_OBJ_MARK(obj); - for(auto& frame : vm->callstack.data()) frame._gc_mark(); + for(auto& frame : vm->callstack.container()) frame._gc_mark(); for(PyObject* obj: vm->s_data) PK_OBJ_MARK(obj); for(auto [_, co]: vm->_cached_codes) co->_gc_mark(); if(vm->_last_exception) PK_OBJ_MARK(vm->_last_exception);