summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBin Guo <guobin@linux.alibaba.com>2026-06-03 10:25:38 +0800
committerMarkus Armbruster <armbru@redhat.com>2026-07-02 13:45:45 +0200
commita21232a2558e947e5f9d46de9b9590d8e006050b (patch)
tree6c093d2e89231a71dd06bb53d40176b76650394e
parenta59157f98f0b69b0bbdb26bc15fbc4d6c8060799 (diff)
downloadqemu-a21232a2558e947e5f9d46de9b9590d8e006050b.tar.gz
qemu-a21232a2558e947e5f9d46de9b9590d8e006050b.zip
qobject/json-writer: preallocate output buffer
json_writer_new() creates the output GString with g_string_new(NULL), which starts at the GLib default of 64 bytes. Serializing typical QMP responses then requires multiple reallocations as the buffer grows -- for query-qmp-schema the GString is reallocated 12+ times. Preallocate JSON_WRITER_INITIAL_SIZE (4096) bytes. This covers most QMP responses without any reallocation. The JSONWriter is a short-lived object so the preallocation does not accumulate. Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Bin Guo <guobin@linux.alibaba.com> Message-ID: <20260603022538.92780-1-guobin@linux.alibaba.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
-rw-r--r--qobject/json-writer.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/qobject/json-writer.c b/qobject/json-writer.c
index aac2c6ab71..c23c81709b 100644
--- a/qobject/json-writer.c
+++ b/qobject/json-writer.c
@@ -24,13 +24,16 @@ struct JSONWriter {
GByteArray *container_is_array;
};
+/* Should cover most QMP responses without reallocation */
+#define JSON_WRITER_INITIAL_SIZE 4096
+
JSONWriter *json_writer_new(bool pretty)
{
JSONWriter *writer = g_new(JSONWriter, 1);
writer->pretty = pretty;
writer->need_comma = false;
- writer->contents = g_string_new(NULL);
+ writer->contents = g_string_sized_new(JSON_WRITER_INITIAL_SIZE);
writer->container_is_array = g_byte_array_new();
return writer;
}