# HG changeset patch # User nenolod # Date 1150053053 25200 # Node ID 36e8431d8e199a0be986787dbc36bc03807e056e # Parent 6fe822f5a3c07cf226d0d381bc1be0486dbc4392 [svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file. diff -r 6fe822f5a3c0 -r 36e8431d8e19 audacious/util.c --- a/audacious/util.c Sat Jun 10 21:44:36 2006 -0700 +++ b/audacious/util.c Sun Jun 11 12:10:53 2006 -0700 @@ -399,7 +399,9 @@ read_ini_string(const gchar * filename, const gchar * section, const gchar * key) { - gchar *buffer, *ret_buffer = NULL; + static gchar *buffer = NULL; + static gchar *open_buffer = NULL; + gchar *ret_buffer = NULL; gint found_section = 0, len = 0; gsize filesize, off = 0; gchar *outbuf; @@ -409,8 +411,35 @@ if (!filename) return NULL; - if (!g_file_get_contents(filename, &buffer, &filesize, NULL)) - return NULL; + /* + * We optimise for the condition that we may be reading from the + * same ini-file multiple times. This is fairly common; it happens + * on .pls playlist loads. To do otherwise would take entirely too + * long, as fstat() can be very slow when done 21,000 times too many. + * + * Therefore, we optimise by keeping the last ini file in memory. + * Yes, this is a memory leak, but it is not too bad, hopefully. + * - nenolod + */ + if (open_buffer == NULL || strcasecmp(filename, open_buffer)) + { + if (buffer != NULL) + { + g_free(buffer); + buffer = NULL; + } + + if (open_buffer != NULL) + { + g_free(open_buffer); + open_buffer = NULL; + } + + if (!g_file_get_contents(filename, &buffer, &filesize, NULL)) + return NULL; + + open_buffer = g_strdup(filename); + } /* * Convert UTF-16 into something useful. Original implementation @@ -484,7 +513,6 @@ off++; } - g_free(buffer); return ret_buffer; }