Skip to content

Commit

Permalink
optimize symbol table for single-threaded mode
Browse files Browse the repository at this point in the history
remotes a bunch of mem allocs + unnecessary computations on every string lookup
  • Loading branch information
nunoplopes committed May 23, 2021
1 parent aef3809 commit f1545b0
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/util/symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Revision History:
#include "util/region.h"
#include "util/string_buffer.h"
#include <cstring>
#include <optional>
#ifndef SINGLE_THREAD
#include <thread>
#endif
Expand All @@ -35,6 +36,7 @@ const symbol symbol::null;
/**
\brief Symbol table manager. It stores the symbol strings created at runtime.
*/
namespace {
class internal_symbol_table {
region m_region; //!< Region used to store symbol strings.
str_hashtable m_table; //!< Table of created symbol strings.
Expand Down Expand Up @@ -73,6 +75,22 @@ class internal_symbol_table {
return result;
}
};
}

#ifdef SINGLE_THREAD
static std::optional<internal_symbol_table> g_symbol_tables;

void initialize_symbols() {
if (!g_symbol_tables) {
g_symbol_tables.emplace();
}
}

void finalize_symbols() {
g_symbol_tables.reset();
}

#else

struct internal_symbol_tables {
unsigned sz;
Expand Down Expand Up @@ -101,11 +119,7 @@ static internal_symbol_tables* g_symbol_tables = nullptr;

void initialize_symbols() {
if (!g_symbol_tables) {
#ifdef SINGLE_THREAD
unsigned num_tables = 1;
#else
unsigned num_tables = 2 * std::min((unsigned) std::thread::hardware_concurrency(), 64u);
#endif
g_symbol_tables = alloc(internal_symbol_tables, num_tables);

}
Expand All @@ -115,6 +129,7 @@ void finalize_symbols() {
dealloc(g_symbol_tables);
g_symbol_tables = nullptr;
}
#endif

symbol::symbol(char const * d) {
if (d == nullptr)
Expand All @@ -130,11 +145,8 @@ symbol & symbol::operator=(char const * d) {

std::string symbol::str() const {
SASSERT(!is_marked());
if (GET_TAG(m_data) == 0) {
if (m_data)
return m_data;
else
return "<null>";
if (GET_TAG(m_data) == 0) {
return m_data ? m_data : "<null>";
}
else {
string_buffer<128> buffer;
Expand Down

0 comments on commit f1545b0

Please sign in to comment.