After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 755509 - object: Allow allocation-free and callback-free iteration over object members
object: Allow allocation-free and callback-free iteration over object members
Status: RESOLVED FIXED
Product: json-glib
Classification: Core
Component: general
git master
Other All
: Normal normal
: ---
Assigned To: json-glib-maint
json-glib-maint
Depends on:
Blocks:
 
 
Reported: 2015-09-24 07:48 UTC by Philip Withnall
Modified: 2015-10-06 07:32 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
object: Add json_object_peek_members() to avoid allocations (3.83 KB, patch)
2015-09-24 07:48 UTC, Philip Withnall
none Details | Review
object: Add JsonObjectIter to ease iteration over JsonObject members (7.58 KB, patch)
2015-09-24 10:08 UTC, Philip Withnall
committed Details | Review

Description Philip Withnall 2015-09-24 07:48:52 UTC
New API to avoid some list copies. See the commit message for details.
Comment 1 Philip Withnall 2015-09-24 07:48:59 UTC
Created attachment 312004 [details] [review]
object: Add json_object_peek_members() to avoid allocations

This is a version of json_object_get_members() which avoids a list copy,
at the cost of its return value being invalidated if the JsonObject
changes.

It differs from json_object_get_members() in the order in which it
returns the member list — for json_object_peek_members() the order is
undefined.
Comment 2 Emmanuele Bassi (:ebassi) 2015-09-24 07:55:28 UTC
I'm not overly fond of this kind of API.

If you want to save allocations, why not use json_object_foreach_member()?
Comment 3 Philip Withnall 2015-09-24 09:06:06 UTC
(In reply to Emmanuele Bassi (:ebassi) from comment #2)
> I'm not overly fond of this kind of API.
> 
> If you want to save allocations, why not use json_object_foreach_member()?

a) It means moving part of my code out to a callback function which makes it harder to read and means bundling a load of state into a closure.
b) It means a lot of indirect function calls. (Lower cost than a list copy, but still costly.)

What would you think about an iterator API like GHashTableIter?
Comment 4 Emmanuele Bassi (:ebassi) 2015-09-24 09:13:16 UTC
(In reply to Philip Withnall from comment #3)
> (In reply to Emmanuele Bassi (:ebassi) from comment #2)
> > I'm not overly fond of this kind of API.
> > 
> > If you want to save allocations, why not use json_object_foreach_member()?
> 
> a) It means moving part of my code out to a callback function which makes it
> harder to read and means bundling a load of state into a closure.
> b) It means a lot of indirect function calls. (Lower cost than a list copy,
> but still costly.)
> 
> What would you think about an iterator API like GHashTableIter?

I'd be indeed more amenable to an iterator API, yes.

In theory, you could also use JsonBuilder and iterate over the object as if it were an array, but the set up costs are worse.
Comment 5 Philip Withnall 2015-09-24 10:08:28 UTC
Created attachment 312032 [details] [review]
object: Add JsonObjectIter to ease iteration over JsonObject members

This is a stack-allocated iterator object similar to GHashTableIter
which allows allocation-free iteration over the members in a JsonObject.

It differs from json_object_foreach_member() in the order in which it
iterates — for JsonObjectIter the order is undefined.
Comment 6 Philip Withnall 2015-10-05 17:19:09 UTC
Comment on attachment 312004 [details] [review]
object: Add json_object_peek_members() to avoid allocations

(peek_members() approach is obsolete.)
Comment 7 Emmanuele Bassi (:ebassi) 2015-10-05 17:26:50 UTC
Review of attachment 312032 [details] [review]:

Looks generally good to me.

::: json-glib/json-types-private.h
@@ +100,3 @@
+  JsonObject *object;  /* unowned */
+  GHashTableIter members_iter;  /* iterator over @members */
+  gpointer padding[4];  /* for future expansion */

Do we really think that we'll expand this with four pointers? GHashTableIter is already fairly sizeable in itself…
Comment 8 Philip Withnall 2015-10-06 07:32:07 UTC
Merged with 2 padding pointers rather than 4.

Attachment 312032 [details] pushed as d231976 - object: Add JsonObjectIter to ease iteration over JsonObject members