1
1
mirror of https://github.com/tstellar/bygfoot.git synced 2024-12-15 17:59:42 +01:00

Speed up game simulations by using g_datalist_id_get_data()

g_datalist_id_get_data() performs much better than g_datalist_get_data()
because it uses a hash based lookup instead of string compare.
I tested this by starting a game with an English Premier league team
and then simulating to week 6.  According to perf's cycle counts, this
patch makes this test case about 10 times faster.

There is still room for improvement.  We currently re-compute the GQuark
value for every lookup, but we could pre-compute the GQuark once for each
option key and then use that value when looking up the data.
This commit is contained in:
Tom Stellard 2020-08-31 06:48:12 -07:00
parent 5a87b99e17
commit d195a59c80

View File

@ -44,7 +44,8 @@ option_string(const gchar *name, OptionList *optionlist)
printf("option_string\n"); printf("option_string\n");
#endif #endif
gpointer element = g_datalist_get_data(&optionlist->datalist, name); GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
if(element != NULL) if(element != NULL)
return ((Option*)element)->string_value; return ((Option*)element)->string_value;
@ -59,7 +60,8 @@ option_string(const gchar *name, OptionList *optionlist)
gchar** gchar**
option_string_pointer(const gchar *name, OptionList *optionlist) option_string_pointer(const gchar *name, OptionList *optionlist)
{ {
gpointer element = g_datalist_get_data(&optionlist->datalist, name); GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
if(element != NULL) if(element != NULL)
return &((Option*)element)->string_value; return &((Option*)element)->string_value;
@ -82,7 +84,8 @@ option_int(const gchar *name, OptionList *optionlist)
printf("option_int\n"); printf("option_int\n");
#endif #endif
gpointer element = g_datalist_get_data(&optionlist->datalist, name); GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
if(element != NULL) if(element != NULL)
return ((Option*)element)->value; return ((Option*)element)->value;
@ -101,7 +104,8 @@ option_int_pointer(const gchar *name, OptionList *optionlist)
printf("option_int_pointer\n"); printf("option_int_pointer\n");
#endif #endif
gpointer element = g_datalist_get_data(&optionlist->datalist, name); GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
if(element != NULL) if(element != NULL)
return &((Option*)element)->value; return &((Option*)element)->value;
@ -120,7 +124,8 @@ option_int_pointer(const gchar *name, OptionList *optionlist)
gfloat gfloat
option_float(const gchar *name, OptionList *optionlist) option_float(const gchar *name, OptionList *optionlist)
{ {
gpointer element = g_datalist_get_data(&optionlist->datalist, name); GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
if(element != NULL) if(element != NULL)
return (gfloat)((Option*)element)->value / OPTION_FLOAT_DIVISOR; return (gfloat)((Option*)element)->value / OPTION_FLOAT_DIVISOR;
@ -142,7 +147,8 @@ option_set_string(const gchar *name, OptionList *optionlist, const gchar *new_va
printf("option_set_string\n"); printf("option_set_string\n");
#endif #endif
gpointer element = g_datalist_get_data(&optionlist->datalist, name); GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
if(element == NULL) if(element == NULL)
debug_print_message("option_set_string: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name); debug_print_message("option_set_string: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name);
@ -161,7 +167,8 @@ option_set_int(const gchar *name, OptionList *optionlist, gint new_value)
printf("option_set_int\n"); printf("option_set_int\n");
#endif #endif
gpointer element = g_datalist_get_data(&optionlist->datalist, name); GQuark quark = g_quark_from_string(name);
gpointer element = g_datalist_id_get_data(&optionlist->datalist, quark);
if(element == NULL) if(element == NULL)
debug_print_message("option_set_int: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name); debug_print_message("option_set_int: option named %s not found\nMaybe you should delete the .bygfoot directory from your home dir", name);
@ -183,8 +190,10 @@ option_add(OptionList *optionlist, const gchar *name,
Option new; Option new;
gpointer element = NULL; gpointer element = NULL;
if(optionlist->list != NULL) if(optionlist->list != NULL) {
element = g_datalist_get_data(&optionlist->datalist, name); GQuark quark = g_quark_from_string(name);
element = g_datalist_id_get_data(&optionlist->datalist, name);
}
if(element != NULL) if(element != NULL)
{ {