Skip to content

Commit

Permalink
Settings: Cache calls to SETTINGS_read_bool / SETTINGS_write_bool (us…
Browse files Browse the repository at this point in the history
…ing general template class, so easy to add caching for the other types later, if needed)
  • Loading branch information
kmatheussen committed Feb 6, 2024
1 parent 6afcbf8 commit 5cef21c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
4 changes: 4 additions & 0 deletions common/hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ static unsigned int oat_hash(const char *key, int i_i)
return h;
}

unsigned int get_hash_hash(const char *key)
{
return oat_hash(key, 0);
}

typedef struct _hash_element_t{
const char *key;
Expand Down
2 changes: 2 additions & 0 deletions common/hashmap_proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

// A key can not contain lineshift since lineshifts are used as delimiter character when loading/saving.

extern LANGSPEC unsigned int get_hash_hash(const char *key);

extern LANGSPEC void HASH_clear(hash_t *hash);
extern LANGSPEC int HASH_get_version(const hash_t *hash); // TODO: remove. Make HASH_is_compatible function instead.

Expand Down
78 changes: 73 additions & 5 deletions common/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ static void SETTINGS_put(const char* key, QString val){

// Warning, called before GC_init, so it cannot allocate with talloc or talloc_atomic.
static QString SETTINGS_get(const char* key){
printf("SEtTING_get called for \"%s\"\n", key);

QVector<QString> lines = get_lines(key);
if(lines.size()==0)
return not_found;
Expand Down Expand Up @@ -358,8 +360,78 @@ void SETTINGS_unset_custom_configfile(void){
custom_configuration_filename="";
}


namespace{
template <typename T>
struct Cache{
QHash<unsigned int, QPair<T, bool>> _cache;

T get(const char *key, T def, std::function<T(const char*)> get_val){
R_ASSERT(THREADING_is_main_thread());

const unsigned int hash = get_hash_hash(key);

auto it = _cache.find(hash);

if (it == _cache.end()) {

const char* string_val = SETTINGS_get_chars(key);

if (string_val == NULL) {

_cache.insert(hash, {def, false});

return def;

} else {

T val = get_val(string_val);

_cache.insert(hash, {val, true});

return val;
}

}else if (!it.value().second) {

return def;

} else {

return it.value().first;

}
}

void put(const char *key, T val, const QString &stringval) {
R_ASSERT(THREADING_is_main_thread());

const unsigned int hash = get_hash_hash(key);

auto it = _cache.find(hash);

if (it==_cache.end())
_cache.insert(hash, {val, true});
else
it.value() = {val, true};

SETTINGS_put(key, stringval);
}
};
}

static Cache<bool> g_bools;

bool SETTINGS_read_bool(const char* key, bool def){
return SETTINGS_read_string(key, def==true?"true":"false")[0] == 't';
return g_bools.get(key, def, [](const char *string_val)
{
R_ASSERT(!strcmp(string_val, "true") || !strcmp(string_val, "false"));
return string_val[0] == 't';
});
}

void SETTINGS_write_bool(const char* key, bool val){
g_bools.put(key, val, val==true?"true":"false");
}

int64_t SETTINGS_read_int(const char* key, int64_t def){
Expand Down Expand Up @@ -433,10 +505,6 @@ vector_t *SETTINGS_get_all_lines_starting_with(const char *prefix){
return ret;
}

void SETTINGS_write_bool(const char* key, bool val){
SETTINGS_write_string(key, val==true?"true":"false");
}

void SETTINGS_write_int(const char* key, int64_t val){
qlonglong val2 = val;
SETTINGS_put(key, QString::number(val2));
Expand Down
10 changes: 5 additions & 5 deletions common/settings_proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ namespace radium{
extern LANGSPEC bool SETTINGS_has_key(const char *key);

extern LANGSPEC bool SETTINGS_read_bool(const char *key, bool def);
extern LANGSPEC int64_t SETTINGS_read_int(const char *key, int64_t def);
extern LANGSPEC int SETTINGS_read_int32(const char *key, int def);
extern LANGSPEC double SETTINGS_read_double(const char *key, double def);
extern LANGSPEC const char *SETTINGS_read_string(const char *key, const char *def);
extern LANGSPEC int64_t SETTINGS_read_int(const char *key, int64_t def); // Warning, not cached yet!
extern LANGSPEC int SETTINGS_read_int32(const char *key, int def); // Warning, not cached yet!
extern LANGSPEC double SETTINGS_read_double(const char *key, double def); // Warning, not cached yet!
extern LANGSPEC const char *SETTINGS_read_string(const char *key, const char *def); // Warning, not cached yet!

extern LANGSPEC const wchar_t* SETTINGS_read_wchars(const char* key, const wchar_t* def);
extern LANGSPEC const wchar_t* SETTINGS_read_wchars(const char* key, const wchar_t* def); // Warning, not cached yet!
extern LANGSPEC void SETTINGS_write_wchars(const char* key, const wchar_t *wchars);

extern LANGSPEC vector_t *SETTINGS_get_all_lines_starting_with(const char *prefix);
Expand Down

0 comments on commit 5cef21c

Please sign in to comment.