[gnome-software] fedora-pkgdb-collections: Memory map JSON files for parsing



commit 8c3e3fcdf986a7610fa02050e2e8c5533c2284e5
Author: Philip Withnall <withnall endlessm com>
Date:   Wed Jun 17 20:15:02 2020 +0100

    fedora-pkgdb-collections: Memory map JSON files for parsing
    
    Where possible, memory map any JSON files which are going to be parsed,
    rather than loading the file into a new heap allocation, parsing it and
    then freeing it. This saves a few large (temporary) allocations.
    
    It uses a new API in the as-yet-unreleased json-glib 1.6, but doesn’t
    have a hard dependency on it.
    
    See https://gitlab.gnome.org/GNOME/json-glib/-/merge_requests/27
    
    Signed-off-by: Philip Withnall <withnall endlessm com>

 .../fedora-pkgdb-collections/gs-plugin-fedora-pkgdb-collections.c | 8 ++++++++
 1 file changed, 8 insertions(+)
---
diff --git a/plugins/fedora-pkgdb-collections/gs-plugin-fedora-pkgdb-collections.c 
b/plugins/fedora-pkgdb-collections/gs-plugin-fedora-pkgdb-collections.c
index 2e6486c4..5499340c 100644
--- a/plugins/fedora-pkgdb-collections/gs-plugin-fedora-pkgdb-collections.c
+++ b/plugins/fedora-pkgdb-collections/gs-plugin-fedora-pkgdb-collections.c
@@ -342,8 +342,10 @@ _ensure_cache (GsPlugin *plugin, GCancellable *cancellable, GError **error)
        GsPluginData *priv = gs_plugin_get_data (plugin);
        JsonArray *collections;
        JsonObject *root;
+#if !JSON_CHECK_VERSION(1, 6, 0)
        gsize len;
        g_autofree gchar *data = NULL;
+#endif  /* json-glib < 1.6.0 */
        g_autoptr(JsonParser) parser = NULL;
 
        /* already done */
@@ -354,6 +356,11 @@ _ensure_cache (GsPlugin *plugin, GCancellable *cancellable, GError **error)
        if (!_refresh_cache (plugin, G_MAXUINT, cancellable, error))
                return FALSE;
 
+#if JSON_CHECK_VERSION(1, 6, 0)
+       parser = json_parser_new_immutable ();
+       if (!json_parser_load_from_mapped_file (parser, priv->cachefn, error))
+               return FALSE;
+#else  /* if json-glib < 1.6.0 */
        /* get cached file */
        if (!g_file_get_contents (priv->cachefn, &data, &len, error)) {
                gs_utils_error_convert_gio (error);
@@ -364,6 +371,7 @@ _ensure_cache (GsPlugin *plugin, GCancellable *cancellable, GError **error)
        parser = json_parser_new ();
        if (!json_parser_load_from_data (parser, data, len, error))
                return FALSE;
+#endif  /* json-glib < 1.6.0 */
 
        root = json_node_get_object (json_parser_get_root (parser));
        if (root == NULL) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]