From 52a3c74e431221585ccd451baba31a77fefb6dfa Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 5 Jun 2024 22:47:36 +0800 Subject: [PATCH] some fix --- include/pocketpy/common/str.hpp | 8 ++------ include/pocketpy/compiler/expr.hpp | 4 ++-- include/pocketpy/objects/base.hpp | 2 +- src/common/str.cpp | 10 ++++++++-- src/compiler/compiler.cpp | 7 +++---- src/compiler/expr.cpp | 16 ++++++++-------- 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/include/pocketpy/common/str.hpp b/include/pocketpy/common/str.hpp index 7b16fe83..232a7e4e 100644 --- a/include/pocketpy/common/str.hpp +++ b/include/pocketpy/common/str.hpp @@ -5,7 +5,6 @@ #include "pocketpy/common/vector.hpp" #include -#include namespace pkpy { @@ -113,9 +112,8 @@ struct StrName { StrName(const Str& s) : index(get(s.sv()).index) {} - std::string_view sv() const { return _r_interned()[index]; } - - const char* c_str() const { return _r_interned()[index].c_str(); } + std::string_view sv() const; + const char* c_str() const; bool empty() const { return index == 0; } @@ -130,8 +128,6 @@ struct StrName { bool operator> (const StrName& other) const noexcept { return sv() > other.sv(); } static StrName get(std::string_view s); - static std::map& _interned(); - static std::map& _r_interned(); static uint32_t _pesudo_random_index; }; diff --git a/include/pocketpy/compiler/expr.hpp b/include/pocketpy/compiler/expr.hpp index 1e27d02a..bd481eb0 100644 --- a/include/pocketpy/compiler/expr.hpp +++ b/include/pocketpy/compiler/expr.hpp @@ -119,8 +119,8 @@ struct CodeEmitContext { int curr_iblock = 0; bool is_compiling_class = false; - std::map _co_consts_nonstring_dedup_map; - std::map _co_consts_string_dedup_map; + small_map _co_consts_nonstring_dedup_map; + small_map _co_consts_string_dedup_map; int get_loop() const; CodeBlock* enter_block(CodeBlockType type); diff --git a/include/pocketpy/objects/base.hpp b/include/pocketpy/objects/base.hpp index 063d9e8c..875bcd80 100644 --- a/include/pocketpy/objects/base.hpp +++ b/include/pocketpy/objects/base.hpp @@ -102,7 +102,7 @@ struct PyVar final { template obj_get_t obj_get(); - // for std::map<> + // std::less<> for map-like containers bool operator< (const PyVar& other) const { return memcmp(this, &other, sizeof(PyVar)) < 0; } }; diff --git a/src/common/str.cpp b/src/common/str.cpp index ea7b71df..5a205ab2 100644 --- a/src/common/str.cpp +++ b/src/common/str.cpp @@ -1,10 +1,12 @@ #include "pocketpy/common/str.hpp" +#include "pocketpy/common/gil.hpp" #include #include #include #include #include +#include namespace pkpy { @@ -356,19 +358,23 @@ int Str::count(const Str& sub) const { return cnt; } -std::map& StrName::_interned() { +std::map& _interned() { static std::map interned; return interned; } -std::map& StrName::_r_interned() { +std::map& _r_interned() { static std::map r_interned; return r_interned; } +std::string_view StrName::sv() const { return _r_interned()[index]; } +const char* StrName::c_str() const { return _r_interned()[index].c_str(); } + uint32_t StrName::_pesudo_random_index = 0; StrName StrName::get(std::string_view s) { + PK_GLOBAL_SCOPE_LOCK() auto it = _interned().find(s); if(it != _interned().end()) return StrName(it->second); // generate new index diff --git a/src/compiler/compiler.cpp b/src/compiler/compiler.cpp index 9c015976..0b692b8c 100644 --- a/src/compiler/compiler.cpp +++ b/src/compiler/compiler.cpp @@ -1191,12 +1191,11 @@ Str Compiler::precompile() { ss << "pkpy:" PK_VERSION << '\n'; // L1: version string ss << (int)mode() << '\n'; // L2: mode - std::map token_indices; + small_map token_indices; for(auto token: tokens) { if(is_raw_string_used(token.type)) { - auto it = token_indices.find(token.sv()); - if(it == token_indices.end()) { - token_indices[token.sv()] = 0; + if(!token_indices.contains(token.sv())) { + token_indices.insert(token.sv(), 0); // assert no '\n' in token.sv() for(char c: token.sv()) assert(c != '\n'); diff --git a/src/compiler/expr.cpp b/src/compiler/expr.cpp index 22f85bc2..99d2f31b 100644 --- a/src/compiler/expr.cpp +++ b/src/compiler/expr.cpp @@ -115,14 +115,14 @@ int CodeEmitContext::add_varname(StrName name) { } int CodeEmitContext::add_const_string(std::string_view key) { - auto it = _co_consts_string_dedup_map.find(key); - if(it != _co_consts_string_dedup_map.end()) { - return it->second; + int* val = _co_consts_string_dedup_map.try_get(key); + if(val) { + return *val; } else { co->consts.push_back(VAR(key)); int index = co->consts.size() - 1; key = co->consts.back().obj_get().sv(); - _co_consts_string_dedup_map[key] = index; + _co_consts_string_dedup_map.insert(key, index); return index; } } @@ -130,13 +130,13 @@ int CodeEmitContext::add_const_string(std::string_view key) { int CodeEmitContext::add_const(PyVar v) { assert(!is_type(v, VM::tp_str)); // non-string deduplication - auto it = _co_consts_nonstring_dedup_map.find(v); - if(it != _co_consts_nonstring_dedup_map.end()) { - return it->second; + int* val = _co_consts_nonstring_dedup_map.try_get(v); + if(val) { + return *val; } else { co->consts.push_back(v); int index = co->consts.size() - 1; - _co_consts_nonstring_dedup_map[v] = index; + _co_consts_nonstring_dedup_map.insert(v, index); return index; } }