shared/json: optimize appending objects to arrays
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 17 Nov 2022 13:32:46 +0000 (14:32 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 1 Dec 2022 17:13:21 +0000 (18:13 +0100)
commit8525bb369a09f488ec77f94e1557ecc2343eb4ab
tree5026dcf84c61256248152122e5ac514b77650f6a
parentb0eeb945881b86f6740b84c055f0ad2be7a09ad1
shared/json: optimize appending objects to arrays

When repeatedly appending an object to a growing array, we would create a new
array larger by one slot, insert all the old entries and the new element with
ref count bumps into the new array, and then unref the old array.

This would cause problems when building an array with more than a few thousand
elements. If userdbctl is modified to construct an array,
'userdbctl --json=pretty group >/dev/null' with 31k groups:
0.74s   (existing code)
102.17s (returning an array)
0.79s   (with this patch)

We append arrays in various places, so it seems nice to make this generally
fast.
src/shared/json.c