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 677062 - GVariant: Make g_variant_new_from_bytes() public, add more GBytes API
GVariant: Make g_variant_new_from_bytes() public, add more GBytes API
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: gvariant
unspecified
Other All
: Normal normal
: ---
Assigned To: Allison Karlitskaya (desrt)
gtkdev
Depends on:
Blocks:
 
 
Reported: 2012-05-29 21:27 UTC by Colin Walters
Modified: 2013-06-10 14:46 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GVariant: Make g_variant_new_from_bytes() public, add more GBytes API (7.71 KB, patch)
2012-05-29 21:27 UTC, Colin Walters
none Details | Review
GVariant: Make g_variant_new_from_bytes() public, add more GBytes API (7.67 KB, patch)
2012-05-29 23:20 UTC, Colin Walters
needs-work Details | Review
gvariant: Make g_variant_new_from_bytes() public (7.12 KB, patch)
2012-10-23 14:26 UTC, Allison Karlitskaya (desrt)
committed Details | Review
gvariant: add g_variant_new_byte_array() (3.55 KB, patch)
2012-10-23 14:26 UTC, Allison Karlitskaya (desrt)
rejected Details | Review

Description Colin Walters 2012-05-29 21:27:33 UTC
Now that GBytes has been made public, we should make
g_variant_new_from_bytes() public too.

While we have the patient open, add g_variant_new_byte_array() which
accepts GBytes.  The main advantage of this is that we don't have to
do a copy of the data.

NOTE: Missing gio.symbols and the docs update, will do before commit
Comment 1 Colin Walters 2012-05-29 21:27:36 UTC
Created attachment 215222 [details] [review]
GVariant: Make g_variant_new_from_bytes() public, add more GBytes API
Comment 2 Allison Karlitskaya (desrt) 2012-05-29 22:21:03 UTC
(In reply to comment #0)
> While we have the patient open, add g_variant_new_byte_array() which
> accepts GBytes.  The main advantage of this is that we don't have to
> do a copy of the data.

g_variant_new_from_bytes uses the GBytes as its storage -- there is already no copy made.
Comment 3 Colin Walters 2012-05-29 22:34:27 UTC
(In reply to comment #2)
> (In reply to comment #0)
> > While we have the patient open, add g_variant_new_byte_array() which
> > accepts GBytes.  The main advantage of this is that we don't have to
> > do a copy of the data.
> 
> g_variant_new_from_bytes uses the GBytes as its storage -- there is already no
> copy made.

Yes, but g_variant_new_byte_array() is different; it is really a shorthand for:

g_variant_new_from_data (G_VARIANT_TYPE ("ay"), buf, bytes_read, FALSE, g_free, buf);

under the assumption that your "buf" was allocated with g_malloc().

I've seen some code that uses g_variant_new_fixed_array() for byte arrays, but this is less efficient as it incurs a copy, which is unnecessary if you already had the data as a GBytes.
Comment 4 Colin Walters 2012-05-29 22:38:45 UTC
I know this is confusing because there's two GBytes here:

1) "variant data": When we want to construct a GVariant of any format from a GBytes, or turn an existing variant into GBytes 
2) "variants of type ay": A special case of 1), but one I happen to often use in my code, and I've seen other projects using g_variant_new_fixed_array() for this, which as mentioned above is less efficient if one already has a GBytes.
Comment 5 Colin Walters 2012-05-29 23:20:44 UTC
Created attachment 215225 [details] [review]
GVariant: Make g_variant_new_from_bytes() public, add more GBytes API

Just return a ref to our internal GBytes in
g_variant_get_data_as_bytes(), rather than making a new GBytes.
Comment 6 Allison Karlitskaya (desrt) 2012-05-30 12:41:43 UTC
Review of attachment 215225 [details] [review]:

I don't like the new_byte_array() function.  It's just as easy to do this with a call to new_from_bytes and a type...
Comment 7 Colin Walters 2012-05-31 13:20:22 UTC
(In reply to comment #6)
> Review of attachment 215225 [details] [review]:
> 
> I don't like the new_byte_array() function.  It's just as easy to do this with
> a call to new_from_bytes and a type...

...and TRUE for trusted, as the implementation does.  However, many GVariant users may not be aware of the details of implementation, and don't know that an "ay" GVariant is literally just the data.  At least Will Thompson wasn't, and he's a very experienced hacker.

Having an API call to do this where one would expect to find it (next to the other fixed array functions) helps a lot.

I guess other factors in the cost/benefit analysis are:

1) How many programs make byte arrays (as distinct from bytestrings)
2) For those, how large are they?

Dunno.  I have two components (ostree, and one not public) which could use this.  See 

http://git.gnome.org/browse/ostree/tree/src/libotutil/ot-variant-utils.c?id=90aff4f2f6d604a5c3680a22df2e07c60d4a4d9d#n32
which is used in e.g.
http://git.gnome.org/browse/ostree/tree/src/libostree/ostree-core.c?id=90aff4f2f6d604a5c3680a22df2e07c60d4a4d9d#n866
Comment 8 Colin Walters 2012-07-07 21:25:58 UTC
I could split this in two parts if that helps.
Comment 9 Allison Karlitskaya (desrt) 2012-10-23 14:26:17 UTC
Created attachment 227064 [details] [review]
gvariant: Make g_variant_new_from_bytes() public

Now that GBytes has been made public, we should make
g_variant_new_from_bytes() public too.

Add g_variant_get_data_as_bytes() to match.
Comment 10 Allison Karlitskaya (desrt) 2012-10-23 14:26:24 UTC
Created attachment 227065 [details] [review]
gvariant: add g_variant_new_byte_array()

This is a convenience call around g_variant_new_from_bytes() for the
common case of wanting to convert a GBytes to a GVariant of type 'ay'.
Comment 11 Allison Karlitskaya (desrt) 2012-10-23 14:27:45 UTC
Comment on attachment 227064 [details] [review]
gvariant: Make g_variant_new_from_bytes() public

Attachment 227064 [details] pushed as 4fb2d73 - gvariant: Make g_variant_new_from_bytes() public

I've split the patches, updated the since tags and pushed the first one (since
this half was quite obviously useful).

I'm still not convinced on the second one...
Comment 12 Allison Karlitskaya (desrt) 2013-06-10 14:46:09 UTC
With having both _new_fixed_array() and _new_from_bytes(), I think that _new_byte_array() would be quite redundant (and also, I would not expect this to be a GBytes based function).  Let's call this bug finished with the one addition.