Use base::StringPiece in Mojo calls |
|||||
Issue descriptionThere is a feature request from Reilly: "Have Mojo use base::StringPiece more often as that efficiently handles writing functions that are often called with a string literal but may also be called with a full std::string." That sounds reasonable. But I realize that it probably won't work with lazy initialization if the underlying buffer of a base::StringPiece dies before serialization happens. Right? I don't think our lazy-initialization code special-cases base::StringPiece to "deep copy". If my understanding is correct, I think we probably should warn people about using non-owning arguments when making mojo calls (such as type-mapping a struct to base::StringPiece). Or we should have some struct traits support for doing "deep copy". However, that completely removes the benefit of using base::StringPiece. WDYT?
,
Jul 14 2017
Thanks Ken. Agreed with #1. A few thoughts: - If we would like to go down this direction, there seems no reason why this should be limited to base::StringPiece. Some custom types may have the same ownership problem and require different param/storage types as well. But that requires two StructTraits for serializing both the param/storage types. - Mapping mojom string to base::StringPiece is convenient if passing a single string. But it is less so if the user has a std::vector<std::string> and wanty to pass into a mojo call accepting std::vector<base::StringPiece>.
,
Jul 14 2017
Supporting separate param and storage types is also required for issue 700180.
,
Nov 18 2017
In addition to base::StringPiece we also have base::span<T> which is a good replacement for std::vector<T> when T is a type where we can simply map the storage out of the message, such as uint8_t. base::span<T> is a good adapter between various buffer types when ownership of the data is not needed. For example in the USB interface if Mojo methods took base::span<uint8_t> instead of std::vector<uint8_t> then in the renderer the ArrayBufferOrArrayBufferView could be passed directly to the transfer functions. On the browser process side the callbacks could be called with a net::IOBuffer (or base::RefCountedMemory). In both cases copying the buffer into an intermediate std::vector before message serialization could be avoided. Only on deserialization is a copy actually necessary.
,
Nov 18 2017
Yeah, base::span<T> is useful sometimes. I created ReadOnlyBuffer mojom type and typemapped it to base::span<const uint8_t>: https://cs.chromium.org/chromium/src/mojo/common/read_only_buffer.mojom?rcl=c58b56d6408886513e9f87a2bff2ea276d0c058d&l=10 I have used it in ipc.mojom to avoid copying at either side. And there is array_traits_span.h that you could use when writing traits specializations: https://cs.chromium.org/chromium/src/mojo/public/cpp/bindings/array_traits_span.h
,
Nov 20 2017
,
Feb 21 2018
ReadOnlyBuffer satisfies my desire for base::span. We could probably close this issue if we added ReadOnlyString and ReadOnlyString16 types as well. My only concern is that it is a bit odd to recommend against using built-in types.
,
Feb 21 2018
It's also weird to reference a "ReadOnlyFoo" in mojom, in my opinion, just to get subtly different typemapping behavior. I would rather we just implement more granular typemapping controls so you can provide context-sensitive type configurations.
,
Feb 21 2018
i.e. so you can say mojom.Foo has different type representation for storage vs transient passing.
,
Oct 17
|
|||||
►
Sign in to add a comment |
|||||
Comment 1 by roc...@chromium.org
, Jul 14 2017