Skip to contentSkip to conversation

qobject/json-writer: preallocate output buffer

PATCH Superseded Bin Guo 4 participants 5 messages May 29 – Jun 3

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

PATCH 1/1 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. 4096 is one page on most systems, which is efficient for the allocator. The JSONWriter is a short-lived object so the preallocation does not accumulate.

---
 qobject/json-writer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
qobject/json-writer.c +4 -1
index aac2c6ab71..fb3f3f3e3c 100644
@@ -24,13 +24,16 @@ struct JSONWriter {
     GByteArray *container_is_array;
 };
 
+/* Covers most QMP responses without reallocation (one page) */
+#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;
 }
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Bin Guo

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. 4096 is one page on most systems, which is efficient for the allocator.

Why not use qemu_real_host_page_size()?

Anyhow,

Quoted 30 lines

The JSONWriter
is a short-lived object so the preallocation does not accumulate.

---
  qobject/json-writer.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
qobject/json-writer.c +4 -1
index aac2c6ab71..fb3f3f3e3c 100644
@@ -24,13 +24,16 @@ struct JSONWriter {
      GByteArray *container_is_array;
  };
 
+/* Covers most QMP responses without reallocation (one page) */
+#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;
  }
Reviewed-by on v1 1/1
#2 lore raw
In reply to Bin Guo
Quoted 4 lines · Bin Guo

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.

That's an extreme case. Most responses are *much* smaller. Still, starting with a larger buffer makes sense.

Quoted 3 lines

Preallocate JSON_WRITER_INITIAL_SIZE (4096) bytes. This covers most QMP responses without any reallocation. 4096 is one page on most systems, which is efficient for the allocator.

I doubt "one page" matters. How many QMP commands get executed in practice? A couple of hundred during startup, then tens per second? Probably less than that.

Quoted 13 lines

The JSONWriter
is a short-lived object so the preallocation does not accumulate.

---
 qobject/json-writer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
qobject/json-writer.c +0 -0
index aac2c6ab71..fb3f3f3e3c 100644

qobject/json-writer.c struct JSONWriter { · line 27

Quoted 4 lines
qobject/json-writer.c +1 -0
@@ -24,13 +24,16 @@ struct JSONWriter {
     GByteArray *container_is_array;
 };
 
+/* Covers most QMP responses without reallocation (one page) */

Covering most responses matters, one page does not. Suggest

/* Should cover most QMP responses without reallocation */

qobject/json-writer.c struct JSONWriter { · line 39

Quoted 13 lines
qobject/json-writer.c +3 -1
+#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;
 }

Consider tweaking the commit message and the comment to address my remarks.

#3 lore raw
Quoted 10 lines · Markus Armbruster
Quoted 4 lines · Bin Guo

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.

That's an extreme case. Most responses are *much* smaller. Still, starting with a larger buffer makes sense.

Quoted 7 lines
Quoted 3 lines

Preallocate JSON_WRITER_INITIAL_SIZE (4096) bytes. This covers most QMP responses without any reallocation. 4096 is one page on most systems, which is efficient for the allocator.

I doubt "one page" matters. How many QMP commands get executed in practice? A couple of hundred during startup, then tens per second? Probably less than that.

NB tens per second, repeated across possibly 100's or even 1000's of VMs on the single host though.

If we want an arbitrary moderately size buffer, one page feels like a reasonable place to aim for

Quoted 40 lines
Quoted 14 lines

The JSONWriter
is a short-lived object so the preallocation does not accumulate.

---
 qobject/json-writer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
qobject/json-writer.c +0 -0
index aac2c6ab71..fb3f3f3e3c 100644

qobject/json-writer.c struct JSONWriter { · line 27

Quoted 4 lines
qobject/json-writer.c +1 -0
@@ -24,13 +24,16 @@ struct JSONWriter {
     GByteArray *container_is_array;
 };
 
+/* Covers most QMP responses without reallocation (one page) */

Covering most responses matters, one page does not. Suggest

   /* Should cover most QMP responses without reallocation */

qobject/json-writer.c struct JSONWriter { · line 39

Quoted 13 lines
qobject/json-writer.c +3 -1
+#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;
 }

Consider tweaking the commit message and the comment to address my remarks.

Quoted 3 lines

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#4 lore raw
Quoted 19 lines · Daniel P. Berrangé
Quoted 10 lines · Markus Armbruster
Quoted 4 lines · Bin Guo

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.

That's an extreme case. Most responses are *much* smaller. Still, starting with a larger buffer makes sense.

Quoted 7 lines
Quoted 3 lines

Preallocate JSON_WRITER_INITIAL_SIZE (4096) bytes. This covers most QMP responses without any reallocation. 4096 is one page on most systems, which is efficient for the allocator.

I doubt "one page" matters. How many QMP commands get executed in practice? A couple of hundred during startup, then tens per second? Probably less than that.

Quoted 5 lines

NB tens per second, repeated across possibly 100's or even 1000's of VMs on the single host though.

If we want an arbitrary moderately size buffer, one page feels like a reasonable place to aim for

I think 4KiB is a reasonable initial size regardless of the host's page size. That was my argument.

If we care, we can instrument QEMU to gather response size statistics.

[...]