diff options
author | Philip Withnall <withnall@endlessm.com> | 2020-06-09 15:41:45 +0100 |
---|---|---|
committer | Philip Withnall <withnall@endlessm.com> | 2020-06-09 15:43:04 +0100 |
commit | dd7a711244e3d33e3e960cd990e9afccc6877820 (patch) | |
tree | 6cdd86fb2578dee5f5626091cdcbd78a6c45df0a /json-glib/json-parser.c | |
parent | 13142637c8132f49e1bd012786eb5f7f7c6dc986 (diff) | |
download | json-glib-dd7a711244e3d33e3e960cd990e9afccc6877820.tar.gz |
json-parser: Support loading files via memory mapping
Add a new `json_parser_load_from_mapped_file()` to load JSON from
files via memory mapping. It’s otherwise similar to
`json_parser_load_from_file()`. It’s in the right position to be able
to memory map the file it’s reading from: it reads the input once
before building a `JsonNode` structure to represent it, doesn’t write
to the file, and often deals with large input files.
This should speed things up slightly due to reducing time spent
allocating a large chunk of heap memory to load the file into, if a
caller can support that.
Signed-off-by: Philip Withnall <withnall@endlessm.com>
Diffstat (limited to 'json-glib/json-parser.c')
-rw-r--r-- | json-glib/json-parser.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c index a90bbd8..3d77975 100644 --- a/json-glib/json-parser.c +++ b/json-glib/json-parser.c @@ -1089,6 +1089,9 @@ json_parser_load (JsonParser *parser, * Loads a JSON stream from the content of @filename and parses it. See * json_parser_load_from_data(). * + * If the file is large or shared between processes, + * json_parser_load_from_mapped_file() may be a more efficient way to load it. + * * Return value: %TRUE if the file was successfully loaded and parsed. * In case of error, @error is set accordingly and %FALSE is returned */ @@ -1132,6 +1135,61 @@ json_parser_load_from_file (JsonParser *parser, } /** + * json_parser_load_from_mapped_file: + * @parser: a #JsonParser + * @filename: the path for the file to parse + * @error: return location for a #GError, or %NULL + * + * Loads a JSON stream from the content of @filename and parses it. Unlike + * json_parser_load_from_file(), @filename will be memory mapped as read-only + * and parsed. @filename will be unmapped before this function returns. + * + * If mapping or reading the file fails, a %G_FILE_ERROR will be returned. + * + * Return value: %TRUE if the file was successfully loaded and parsed. + * In case of error, @error is set accordingly and %FALSE is returned + * Since: 1.6 + */ +gboolean +json_parser_load_from_mapped_file (JsonParser *parser, + const gchar *filename, + GError **error) +{ + JsonParserPrivate *priv; + GError *internal_error = NULL; + gboolean retval = TRUE; + GMappedFile *mapped_file = NULL; + + g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE); + g_return_val_if_fail (filename != NULL, FALSE); + + priv = parser->priv; + + mapped_file = g_mapped_file_new (filename, FALSE, &internal_error); + if (mapped_file == NULL) + { + g_propagate_error (error, internal_error); + return FALSE; + } + + g_free (priv->filename); + + priv->is_filename = TRUE; + priv->filename = g_strdup (filename); + + if (!json_parser_load (parser, g_mapped_file_get_contents (mapped_file), + g_mapped_file_get_length (mapped_file), &internal_error)) + { + g_propagate_error (error, internal_error); + retval = FALSE; + } + + g_clear_pointer (&mapped_file, g_mapped_file_unref); + + return retval; +} + +/** * json_parser_load_from_data: * @parser: a #JsonParser * @data: the buffer to parse |