This commit is contained in:
blueloveTH 2024-06-05 22:47:36 +08:00
parent c0a855ab7a
commit 52a3c74e43
6 changed files with 24 additions and 23 deletions

View File

@ -5,7 +5,6 @@
#include "pocketpy/common/vector.hpp" #include "pocketpy/common/vector.hpp"
#include <string_view> #include <string_view>
#include <map>
namespace pkpy { namespace pkpy {
@ -113,9 +112,8 @@ struct StrName {
StrName(const Str& s) : index(get(s.sv()).index) {} StrName(const Str& s) : index(get(s.sv()).index) {}
std::string_view sv() const { return _r_interned()[index]; } std::string_view sv() const;
const char* c_str() const;
const char* c_str() const { return _r_interned()[index].c_str(); }
bool empty() const { return index == 0; } bool empty() const { return index == 0; }
@ -130,8 +128,6 @@ struct StrName {
bool operator> (const StrName& other) const noexcept { return sv() > other.sv(); } bool operator> (const StrName& other) const noexcept { return sv() > other.sv(); }
static StrName get(std::string_view s); static StrName get(std::string_view s);
static std::map<std::string_view, uint16_t>& _interned();
static std::map<uint16_t, std::string>& _r_interned();
static uint32_t _pesudo_random_index; static uint32_t _pesudo_random_index;
}; };

View File

@ -119,8 +119,8 @@ struct CodeEmitContext {
int curr_iblock = 0; int curr_iblock = 0;
bool is_compiling_class = false; bool is_compiling_class = false;
std::map<PyVar, int> _co_consts_nonstring_dedup_map; small_map<PyVar, int> _co_consts_nonstring_dedup_map;
std::map<std::string_view, int> _co_consts_string_dedup_map; small_map<std::string_view, int> _co_consts_string_dedup_map;
int get_loop() const; int get_loop() const;
CodeBlock* enter_block(CodeBlockType type); CodeBlock* enter_block(CodeBlockType type);

View File

@ -102,7 +102,7 @@ struct PyVar final {
template <typename T> template <typename T>
obj_get_t<T> obj_get(); obj_get_t<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; } bool operator< (const PyVar& other) const { return memcmp(this, &other, sizeof(PyVar)) < 0; }
}; };

View File

@ -1,10 +1,12 @@
#include "pocketpy/common/str.hpp" #include "pocketpy/common/str.hpp"
#include "pocketpy/common/gil.hpp"
#include <stdexcept> #include <stdexcept>
#include <cassert> #include <cassert>
#include <ostream> #include <ostream>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <map>
namespace pkpy { namespace pkpy {
@ -356,19 +358,23 @@ int Str::count(const Str& sub) const {
return cnt; return cnt;
} }
std::map<std::string_view, uint16_t>& StrName::_interned() { std::map<std::string_view, uint16_t>& _interned() {
static std::map<std::string_view, uint16_t> interned; static std::map<std::string_view, uint16_t> interned;
return interned; return interned;
} }
std::map<uint16_t, std::string>& StrName::_r_interned() { std::map<uint16_t, std::string>& _r_interned() {
static std::map<uint16_t, std::string> r_interned; static std::map<uint16_t, std::string> r_interned;
return 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; uint32_t StrName::_pesudo_random_index = 0;
StrName StrName::get(std::string_view s) { StrName StrName::get(std::string_view s) {
PK_GLOBAL_SCOPE_LOCK()
auto it = _interned().find(s); auto it = _interned().find(s);
if(it != _interned().end()) return StrName(it->second); if(it != _interned().end()) return StrName(it->second);
// generate new index // generate new index

View File

@ -1191,12 +1191,11 @@ Str Compiler::precompile() {
ss << "pkpy:" PK_VERSION << '\n'; // L1: version string ss << "pkpy:" PK_VERSION << '\n'; // L1: version string
ss << (int)mode() << '\n'; // L2: mode ss << (int)mode() << '\n'; // L2: mode
std::map<std::string_view, int> token_indices; small_map<std::string_view, int> token_indices;
for(auto token: tokens) { for(auto token: tokens) {
if(is_raw_string_used(token.type)) { if(is_raw_string_used(token.type)) {
auto it = token_indices.find(token.sv()); if(!token_indices.contains(token.sv())) {
if(it == token_indices.end()) { token_indices.insert(token.sv(), 0);
token_indices[token.sv()] = 0;
// assert no '\n' in token.sv() // assert no '\n' in token.sv()
for(char c: token.sv()) for(char c: token.sv())
assert(c != '\n'); assert(c != '\n');

View File

@ -115,14 +115,14 @@ int CodeEmitContext::add_varname(StrName name) {
} }
int CodeEmitContext::add_const_string(std::string_view key) { int CodeEmitContext::add_const_string(std::string_view key) {
auto it = _co_consts_string_dedup_map.find(key); int* val = _co_consts_string_dedup_map.try_get(key);
if(it != _co_consts_string_dedup_map.end()) { if(val) {
return it->second; return *val;
} else { } else {
co->consts.push_back(VAR(key)); co->consts.push_back(VAR(key));
int index = co->consts.size() - 1; int index = co->consts.size() - 1;
key = co->consts.back().obj_get<Str>().sv(); key = co->consts.back().obj_get<Str>().sv();
_co_consts_string_dedup_map[key] = index; _co_consts_string_dedup_map.insert(key, index);
return index; return index;
} }
} }
@ -130,13 +130,13 @@ int CodeEmitContext::add_const_string(std::string_view key) {
int CodeEmitContext::add_const(PyVar v) { int CodeEmitContext::add_const(PyVar v) {
assert(!is_type(v, VM::tp_str)); assert(!is_type(v, VM::tp_str));
// non-string deduplication // non-string deduplication
auto it = _co_consts_nonstring_dedup_map.find(v); int* val = _co_consts_nonstring_dedup_map.try_get(v);
if(it != _co_consts_nonstring_dedup_map.end()) { if(val) {
return it->second; return *val;
} else { } else {
co->consts.push_back(v); co->consts.push_back(v);
int index = co->consts.size() - 1; int index = co->consts.size() - 1;
_co_consts_nonstring_dedup_map[v] = index; _co_consts_nonstring_dedup_map.insert(v, index);
return index; return index;
} }
} }