From 97c923e5140f0211586cec6dea654eac600633ae Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 18 Feb 2024 17:51:35 +0800 Subject: [PATCH] use `pod_vector` for small vectors --- build_with_warnings.sh | 2 +- include/pocketpy/codeobject.h | 8 ++++---- include/pocketpy/error.h | 2 +- include/pocketpy/expr.h | 2 +- include/pocketpy/obj.h | 1 - include/pocketpy/str.h | 4 ++-- include/pocketpy/vm.h | 4 ++-- src/compiler.cpp | 4 ++-- src/error.cpp | 2 +- src/expr.cpp | 6 +++--- src/lexer.cpp | 6 +++--- src/memory.cpp | 4 ++-- src/obj.cpp | 5 ----- src/pocketpy.cpp | 13 +++++++------ src/profiler.cpp | 2 +- src/random.cpp | 2 +- src/str.cpp | 10 +++++----- src/vm.cpp | 8 ++++---- 18 files changed, 40 insertions(+), 45 deletions(-) diff --git a/build_with_warnings.sh b/build_with_warnings.sh index bde1e437..8febbaf0 100644 --- a/build_with_warnings.sh +++ b/build_with_warnings.sh @@ -1,5 +1,5 @@ SRC=$(find src/ -name "*.cpp") -FLAGS="-std=c++17 -O1 -stdlib=libc++ -Iinclude -W -Wno-unused-parameter -Wno-sign-compare" +FLAGS="-std=c++17 -O1 -stdlib=libc++ -Iinclude -W -Wno-unused-parameter" clang++ $FLAGS -o main -O1 src2/main.cpp $SRC diff --git a/include/pocketpy/codeobject.h b/include/pocketpy/codeobject.h index 2692810e..0b32860d 100644 --- a/include/pocketpy/codeobject.h +++ b/include/pocketpy/codeobject.h @@ -71,7 +71,7 @@ struct CodeObject { std::vector iblocks; // block index for each bytecode std::vector lines; List consts; - std::vector varnames; // local variables + pod_vector varnames; // local variables NameDictInt varnames_inv; std::vector blocks = { CodeBlock(CodeBlockType::NO_BLOCK, -1, 0, 0) }; NameDictInt labels; @@ -95,8 +95,8 @@ struct FuncDecl { PyObject* value; // default value }; CodeObject_ code; // code object of this function - std::vector args; // indices in co->varnames - std::vector kwargs; // indices in co->varnames + pod_vector args; // indices in co->varnames + pod_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 @@ -109,7 +109,7 @@ struct FuncDecl { void add_kwarg(int index, StrName key, PyObject* value){ kw_to_index.set(key, index); - kwargs.push_back({index, key, value}); + kwargs.push_back(KwArg{index, key, value}); } void _gc_mark() const; diff --git a/include/pocketpy/error.h b/include/pocketpy/error.h index ca284a00..a1a9a062 100644 --- a/include/pocketpy/error.h +++ b/include/pocketpy/error.h @@ -30,7 +30,7 @@ struct SourceData { CompileMode mode; Str source; - std::vector line_starts; + pod_vector line_starts; SourceData(std::string_view source, const Str& filename, CompileMode mode); SourceData(const Str& filename, CompileMode mode); diff --git a/include/pocketpy/expr.h b/include/pocketpy/expr.h index 29bdf411..8046e86a 100644 --- a/include/pocketpy/expr.h +++ b/include/pocketpy/expr.h @@ -314,7 +314,7 @@ struct BinaryExpr: Expr{ Expr_ lhs; Expr_ rhs; bool is_compare() const override; - void _emit_compare(CodeEmitContext* ctx, std::vector& jmps); + void _emit_compare(CodeEmitContext* ctx, pod_vector& jmps); void emit_(CodeEmitContext* ctx) override; }; diff --git a/include/pocketpy/obj.h b/include/pocketpy/obj.h index 2b97c0fd..bf76e63c 100644 --- a/include/pocketpy/obj.h +++ b/include/pocketpy/obj.h @@ -75,7 +75,6 @@ struct Bytes{ Bytes(const Str& str): Bytes(str.sv()) {} operator bool() const noexcept { return _data != nullptr; } - Bytes(const std::vector& v); Bytes(std::string_view sv); Bytes(const Bytes& rhs); Bytes(Bytes&& rhs) noexcept; diff --git a/include/pocketpy/str.h b/include/pocketpy/str.h index d45518cd..f03cca05 100644 --- a/include/pocketpy/str.h +++ b/include/pocketpy/str.h @@ -76,8 +76,8 @@ struct Str{ int index(const Str& sub, int start=0) const; Str replace(char old, char new_) const; Str replace(const Str& old, const Str& new_, int count=-1) const; - std::vector split(const Str& sep) const; - std::vector split(char sep) const; + pod_vector split(const Str& sep) const; + pod_vector split(char sep) const; int count(const Str& sub) const; /*************unicode*************/ diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 5a06a7cb..89e82f98 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -54,7 +54,7 @@ struct PyTypeInfo{ StrName name; bool subclass_enabled; - std::vector annotated_fields = {}; + pod_vector annotated_fields = {}; // cached special methods // unary operators @@ -385,7 +385,7 @@ public: struct ImportContext{ std::vector pending; - std::vector pending_is_init; // a.k.a __init__.py + pod_vector pending_is_init; // a.k.a __init__.py struct Temp{ ImportContext* ctx; Temp(ImportContext* ctx, Str name, bool is_init) : ctx(ctx){ diff --git a/src/compiler.cpp b/src/compiler.cpp index bb54f47b..39fca240 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -39,7 +39,7 @@ namespace pkpy{ ctx()->co->end_line = tokens[j].line; // some check here - std::vector& codes = ctx()->co->codes; + auto& codes = ctx()->co->codes; if(ctx()->co->varnames.size() > PK_MAX_CO_VARNAMES){ SyntaxError("maximum number of local variables exceeded"); } @@ -659,7 +659,7 @@ __EAT_DOTS_END: void Compiler::compile_try_except() { ctx()->enter_block(CodeBlockType::TRY_EXCEPT); compile_block_body(); - std::vector patches = { + pod_vector patches = { ctx()->emit_(OP_JUMP_ABSOLUTE, BC_NOARG, BC_KEEPLINE) }; ctx()->exit_block(); diff --git a/src/error.cpp b/src/error.cpp index 07dbb074..f3333fa0 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -24,7 +24,7 @@ namespace pkpy{ if(lineno == -1) return {nullptr, nullptr}; lineno -= 1; if(lineno < 0) lineno = 0; - const char* _start = line_starts.at(lineno); + const char* _start = line_starts[lineno]; const char* i = _start; // max 300 chars while(*i != '\n' && *i != '\0' && i-_start < 300) i++; diff --git a/src/expr.cpp b/src/expr.cpp index 19d76a9a..8c9ed9cb 100644 --- a/src/expr.cpp +++ b/src/expr.cpp @@ -55,7 +55,7 @@ namespace pkpy{ int CodeEmitContext::emit_(Opcode opcode, uint16_t arg, int line, bool is_virtual) { co->codes.push_back(Bytecode{(uint8_t)opcode, arg}); co->iblocks.push_back(curr_block_i); - co->lines.push_back({line, is_virtual}); + co->lines.push_back(CodeObject::LineInfo{line, is_virtual}); int i = co->codes.size() - 1; if(line == BC_KEEPLINE){ if(i >= 1) co->lines[i].lineno = co->lines[i-1].lineno; @@ -613,7 +613,7 @@ namespace pkpy{ } } - void BinaryExpr::_emit_compare(CodeEmitContext* ctx, std::vector& jmps){ + void BinaryExpr::_emit_compare(CodeEmitContext* ctx, pod_vector& jmps){ if(lhs->is_compare()){ static_cast(lhs.get())->_emit_compare(ctx, jmps); }else{ @@ -637,7 +637,7 @@ namespace pkpy{ } void BinaryExpr::emit_(CodeEmitContext* ctx) { - std::vector jmps; + pod_vector jmps; if(is_compare() && lhs->is_compare()){ // (a < b) < c static_cast(lhs.get())->_emit_compare(ctx, jmps); diff --git a/src/lexer.cpp b/src/lexer.cpp index 4d296ce0..71d56cf6 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -109,8 +109,8 @@ static bool is_unicode_Lo_char(uint32_t c) { } } // handle multibyte char - std::string u8str(curr_char, u8bytes); - if(u8str.size() != u8bytes) return 2; + Str u8str(curr_char, u8bytes); + if(u8str.size != u8bytes) return 2; uint32_t value = 0; for(int k=0; k < u8bytes; k++){ uint8_t b = u8str[k]; @@ -204,7 +204,7 @@ static bool is_unicode_Lo_char(uint32_t c) { Str Lexer::eat_string_until(char quote, bool raw) { bool quote3 = match_n_chars(2, quote); - std::vector buff; + pod_vector buff; while (true) { char c = eatchar_include_newline(); if (c == quote){ diff --git a/src/memory.cpp b/src/memory.cpp index 7677ba81..ef7a9f78 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -123,8 +123,8 @@ struct DoubleLinkedList{ template struct MemoryPool{ - static const size_t __MaxBlocks = 256*1024 / __BlockSize; - static const size_t __MinArenaCount = PK_GC_MIN_THRESHOLD*100 / (256*1024); + static const int __MaxBlocks = 256*1024 / __BlockSize; + static const int __MinArenaCount = PK_GC_MIN_THRESHOLD*100 / (256*1024); struct Block{ void* arena; diff --git a/src/obj.cpp b/src/obj.cpp index 9b8722e9..ef9ce681 100644 --- a/src/obj.cpp +++ b/src/obj.cpp @@ -14,11 +14,6 @@ namespace pkpy{ } bool Bytes::operator!=(const Bytes& rhs) const{ return !(*this == rhs); } - Bytes::Bytes(const std::vector& v){ - _data = new unsigned char[v.size()]; - _size = v.size(); - for(int i=0; i<_size; i++) _data[i] = v[i]; - } Bytes::Bytes(std::string_view sv){ _data = new unsigned char[sv.size()]; _size = sv.size(); diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 1b7b0f4a..92a81a38 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -299,11 +299,11 @@ void init_builtins(VM* _vm) { _vm->bind_func<1>(_vm->builtins, "dir", [](VM* vm, ArgsView args) { std::set names; if(!is_tagged(args[0]) && args[0]->is_attr_valid()){ - std::vector keys = args[0]->attr().keys(); + auto keys = args[0]->attr().keys(); names.insert(keys.begin(), keys.end()); } const NameDict& t_attr = vm->_t(args[0])->attr(); - std::vector keys = t_attr.keys(); + auto keys = t_attr.keys(); names.insert(keys.begin(), keys.end()); List ret; for (StrName name : names) ret.push_back(VAR(name.sv())); @@ -568,7 +568,7 @@ void init_builtins(VM* _vm) { const Str& self = _CAST(Str&, args[0]); const Str& sep = CAST(Str&, args[1]); if(sep.empty()) vm->ValueError("empty separator"); - std::vector parts; + pod_vector parts; if(sep.size == 1){ parts = self.split(sep[0]); }else{ @@ -581,7 +581,7 @@ void init_builtins(VM* _vm) { _vm->bind(_vm->_t(VM::tp_str), "splitlines(self)", [](VM* vm, ArgsView args) { const Str& self = _CAST(Str&, args[0]); - std::vector parts; + pod_vector parts; parts = self.split('\n'); List ret(parts.size()); for(int i=0; ibind_constructor<2>(_vm->_t(VM::tp_bytes), [](VM* vm, ArgsView args){ List& list = CAST(List&, args[1]); - std::vector buffer(list.size()); + pod_vector buffer(list.size()); for(int i=0; i255) vm->ValueError("byte must be in range[0, 256)"); buffer[i] = (char)b; } - return VAR(Bytes(buffer)); + auto detached = buffer.detach(); + return VAR(Bytes(detached.first, detached.second)); }); _vm->bind__getitem__(VM::tp_bytes, [](VM* vm, PyObject* obj, PyObject* index) { diff --git a/src/profiler.cpp b/src/profiler.cpp index 5ab2fbd9..c5c8399f 100644 --- a/src/profiler.cpp +++ b/src/profiler.cpp @@ -30,7 +30,7 @@ void LineProfiler::_step(FrameId frame){ _step_end(frame, line); } - std::vector<_LineRecord>& file_records = records[filename]; + auto& file_records = records[filename]; if(file_records.empty()){ // initialize file_records int total_lines = frame->co->src->line_starts.size(); diff --git a/src/random.cpp b/src/random.cpp index 2b06c856..3415ba58 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -61,7 +61,7 @@ struct Random{ Random& self = _CAST(Random&, args[0]); auto [data, size] = vm->_cast_array(args[1]); if(size == 0) vm->IndexError("cannot choose from an empty sequence"); - std::vector cum_weights(size); + pod_vector cum_weights(size); if(args[2] == vm->None){ for(int i = 0; i < size; i++) cum_weights[i] = i + 1; }else{ diff --git a/src/str.cpp b/src/str.cpp index cdbb8e45..f1042ef6 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -14,7 +14,7 @@ int utf8len(unsigned char c, bool suppress){ } #define PK_STR_ALLOCATE() \ - if(this->size < sizeof(this->_inlined)){ \ + if(this->size < (int)sizeof(this->_inlined)){ \ this->data = this->_inlined; \ }else{ \ this->data = (char*)pool64_alloc(this->size+1); \ @@ -345,8 +345,8 @@ int utf8len(unsigned char c, bool suppress){ return _byte_index_to_unicode(size); } - std::vector Str::split(const Str& sep) const{ - std::vector result; + pod_vector Str::split(const Str& sep) const{ + pod_vector result; std::string_view tmp; int start = 0; while(true){ @@ -361,8 +361,8 @@ int utf8len(unsigned char c, bool suppress){ return result; } - std::vector Str::split(char sep) const{ - std::vector result; + pod_vector Str::split(char sep) const{ + pod_vector result; int i = 0; for(int j = 0; j < size; j++){ if(data[j] == sep){ diff --git a/src/vm.cpp b/src/vm.cpp index 9c2f4274..83f07682 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -278,7 +278,7 @@ namespace pkpy{ PyObject* VM::py_import(Str path, bool throw_err){ if(path.empty()) vm->ValueError("empty module name"); - static auto f_join = [](const std::vector& cpnts){ + static auto f_join = [](const pod_vector& cpnts){ SStream ss; for(int i=0; i cpnts = curr_path.split('.'); + pod_vector cpnts = curr_path.split('.'); int prefix = 0; // how many dots in the prefix for(int i=0; i path_cpnts = path.split('.'); + pod_vector path_cpnts = path.split('.'); // check circular import if(_import_context.pending.size() > 128){ ImportError("maximum recursion depth exceeded while importing"); @@ -600,7 +600,7 @@ Str VM::disassemble(CodeObject_ co){ return s + std::string(n - s.length(), ' '); }; - std::vector jumpTargets; + pod_vector jumpTargets; for(auto byte : co->codes){ if(byte.op == OP_JUMP_ABSOLUTE || byte.op == OP_POP_JUMP_IF_FALSE || byte.op == OP_SHORTCUT_IF_FALSE_OR_POP || byte.op == OP_FOR_ITER){ jumpTargets.push_back(byte.arg);