[glib: 1/2] guri: Preallocate a buffer for building URIs
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib: 1/2] guri: Preallocate a buffer for building URIs
- Date: Thu, 5 May 2022 13:30:33 +0000 (UTC)
commit 17f608e382aedb892239fa381dc13fe2b614f74a
Author: Philip Withnall <pwithnall endlessos org>
Date: Thu May 5 13:38:44 2022 +0100
guri: Preallocate a buffer for building URIs
Rather than reallocating the string buffer a few times as more
components are added, use a default buffer size which should hopefully
accommodate most average URIs.
The buffer size is a guess and can be tweaked in future.
This has the advantage of no longer passing a potentially-`NULL`
`scheme` to `g_string_new()`, which should placate the static analysers,
which think that `g_string_new()` shouldn’t accept `NULL`.
Signed-off-by: Philip Withnall <pwithnall endlessos org>
Coverity CID: #1474691
glib/guri.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
---
diff --git a/glib/guri.c b/glib/guri.c
index 069060809a..a698b07ed5 100644
--- a/glib/guri.c
+++ b/glib/guri.c
@@ -1633,9 +1633,17 @@ g_uri_join_internal (GUriFlags flags,
g_return_val_if_fail (host == NULL || (path[0] == '\0' || path[0] == '/'), NULL);
g_return_val_if_fail (host != NULL || (path[0] != '/' || path[1] != '/'), NULL);
- str = g_string_new (scheme);
+ /* Arbitrarily chosen default size which should handle most average length
+ * URIs. This should avoid a few reallocations of the buffer in most cases.
+ * It’s 1B shorter than a power of two, since GString will add a
+ * nul-terminator byte. */
+ str = g_string_sized_new (127);
+
if (scheme)
- g_string_append_c (str, ':');
+ {
+ g_string_append (str, scheme);
+ g_string_append_c (str, ':');
+ }
if (flags & G_URI_FLAGS_SCHEME_NORMALIZE && scheme && ((host && port != -1) || path[0] == '\0'))
normalized_scheme = g_ascii_strdown (scheme, -1);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]