GNOME Bugzilla – Bug 755509
object: Allow allocation-free and callback-free iteration over object members
Last modified: 2015-10-06 07:32:10 UTC
New API to avoid some list copies. See the commit message for details.
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.
I'm not overly fond of this kind of API. If you want to save allocations, why not use json_object_foreach_member()?
(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?
(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.
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 on attachment 312004 [details] [review] object: Add json_object_peek_members() to avoid allocations (peek_members() approach is obsolete.)
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…
Merged with 2 padding pointers rather than 4. Attachment 312032 [details] pushed as d231976 - object: Add JsonObjectIter to ease iteration over JsonObject members