changeset 1166:36e8431d8e19 trunk

[svn] - optimise for multiple lookups of a single INI file, by caching the contents of the INI file.
author nenolod
date Sun, 11 Jun 2006 12:10:53 -0700
parents 6fe822f5a3c0
children 2b8e5e0ef006
files audacious/util.c
diffstat 1 files changed, 32 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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;
 }