mirror of
https://github.com/YosysHQ/yosys
synced 2025-10-10 09:48:06 +00:00
Add an IdString(std::string_view) constructor for efficiency when we already know the string length
This commit is contained in:
parent
6b43fca8df
commit
39c6e06e3d
1 changed files with 18 additions and 10 deletions
|
@ -209,10 +209,15 @@ struct RTLIL::IdString
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_reference(const char *p)
|
static int get_reference(const char *p)
|
||||||
|
{
|
||||||
|
return get_reference(std::string_view(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
static int get_reference(std::string_view p)
|
||||||
{
|
{
|
||||||
log_assert(destruct_guard_ok);
|
log_assert(destruct_guard_ok);
|
||||||
|
|
||||||
auto it = global_id_index_.find((char*)p);
|
auto it = global_id_index_.find(p);
|
||||||
if (it != global_id_index_.end()) {
|
if (it != global_id_index_.end()) {
|
||||||
#ifndef YOSYS_NO_IDS_REFCNT
|
#ifndef YOSYS_NO_IDS_REFCNT
|
||||||
global_refcount_storage_.at(it->second)++;
|
global_refcount_storage_.at(it->second)++;
|
||||||
|
@ -226,14 +231,13 @@ struct RTLIL::IdString
|
||||||
|
|
||||||
ensure_prepopulated();
|
ensure_prepopulated();
|
||||||
|
|
||||||
if (!p[0])
|
if (p.empty())
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
log_assert(p[0] == '$' || p[0] == '\\');
|
log_assert(p[0] == '$' || p[0] == '\\');
|
||||||
log_assert(p[1] != 0);
|
for (char ch : p)
|
||||||
for (const char *c = p; *c; c++)
|
if ((unsigned)ch <= (unsigned)' ')
|
||||||
if ((unsigned)*c <= (unsigned)' ')
|
log_error("Found control character or space (0x%02x) in string '%s' which is not allowed in RTLIL identifiers\n", ch, std::string(p).c_str());
|
||||||
log_error("Found control character or space (0x%02x) in string '%s' which is not allowed in RTLIL identifiers\n", *c, p);
|
|
||||||
|
|
||||||
#ifndef YOSYS_NO_IDS_REFCNT
|
#ifndef YOSYS_NO_IDS_REFCNT
|
||||||
if (global_free_idx_list_.empty()) {
|
if (global_free_idx_list_.empty()) {
|
||||||
|
@ -245,8 +249,11 @@ struct RTLIL::IdString
|
||||||
|
|
||||||
int idx = global_free_idx_list_.back();
|
int idx = global_free_idx_list_.back();
|
||||||
global_free_idx_list_.pop_back();
|
global_free_idx_list_.pop_back();
|
||||||
global_id_storage_.at(idx) = strdup(p);
|
char* buf = static_cast<char*>(malloc(p.size() + 1));
|
||||||
global_id_index_[global_id_storage_.at(idx)] = idx;
|
memcpy(buf, p.data(), p.size());
|
||||||
|
buf[p.size()] = 0;
|
||||||
|
global_id_storage_.at(idx) = buf;
|
||||||
|
global_id_index_.insert(it, {std::string_view(buf, p.size()), idx});
|
||||||
global_refcount_storage_.at(idx)++;
|
global_refcount_storage_.at(idx)++;
|
||||||
#else
|
#else
|
||||||
int idx = global_id_storage_.size();
|
int idx = global_id_storage_.size();
|
||||||
|
@ -255,7 +262,7 @@ struct RTLIL::IdString
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (yosys_xtrace) {
|
if (yosys_xtrace) {
|
||||||
log("#X# New IdString '%s' with index %d.\n", p, idx);
|
log("#X# New IdString '%s' with index %d.\n", global_id_storage_.at(idx), idx);
|
||||||
log_backtrace("-X- ", yosys_xtrace-1);
|
log_backtrace("-X- ", yosys_xtrace-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -322,7 +329,8 @@ struct RTLIL::IdString
|
||||||
inline IdString(const char *str) : index_(get_reference(str)) { }
|
inline IdString(const char *str) : index_(get_reference(str)) { }
|
||||||
inline IdString(const IdString &str) : index_(get_reference(str.index_)) { }
|
inline IdString(const IdString &str) : index_(get_reference(str.index_)) { }
|
||||||
inline IdString(IdString &&str) : index_(str.index_) { str.index_ = 0; }
|
inline IdString(IdString &&str) : index_(str.index_) { str.index_ = 0; }
|
||||||
inline IdString(const std::string &str) : index_(get_reference(str.c_str())) { }
|
inline IdString(const std::string &str) : index_(get_reference(std::string_view(str))) { }
|
||||||
|
inline IdString(std::string_view str) : index_(get_reference(str)) { }
|
||||||
inline IdString(StaticId id) : index_(static_cast<short>(id)) {}
|
inline IdString(StaticId id) : index_(static_cast<short>(id)) {}
|
||||||
inline ~IdString() { put_reference(index_); }
|
inline ~IdString() { put_reference(index_); }
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue