gresource: avoid allocations in enumerate_children()

In the vast majority of cases, we can avoid temporary
allocations for paths in g_resources_enumerate_children().

In the case we need to add a suffix "/", we can usually just
build the path on the stack. In other cases, we can completely
avoid the strdup, which appears to only have been added for
readability. If the path is really long, we fallback to doing
what we did before, and use g_strconcat().

In the case of Builder, this saved 5.3mb of temporary
allocations in the process of showing the first application
window.

https://bugzilla.gnome.org/show_bug.cgi?id=790275
This commit is contained in:
Christian Hergert
2017-11-12 23:04:12 -08:00
parent 38ffcd298c
commit 5464461e4c
2 changed files with 61 additions and 5 deletions

View File

@@ -134,6 +134,32 @@ test_resource (GResource *resource)
g_assert_no_error (error);
g_assert_cmpint (g_strv_length (children), ==, 2);
g_strfreev (children);
/* Test the preferred lookup where we have a trailing slash. */
children = g_resource_enumerate_children (resource,
"/a_prefix/",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children != NULL);
g_assert_no_error (error);
g_assert_cmpint (g_strv_length (children), ==, 2);
g_strfreev (children);
/* test with a path > 256 and no trailing slash to test the
* slow path of resources where we allocate a modified path.
*/
children = g_resource_enumerate_children (resource,
"/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
"/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
"/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
"/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
"/not/here/not/here/not/here/not/here/not/here/not/here/not/here"
"/with/no/trailing/slash",
G_RESOURCE_LOOKUP_FLAGS_NONE,
&error);
g_assert (children == NULL);
g_assert_error (error, G_RESOURCE_ERROR, G_RESOURCE_ERROR_NOT_FOUND);
g_clear_error (&error);
}
static void