3 ms·
Live design review: never use type “string” for keys and values in protocol buffers. Always use “bytes”.
by jeffbee 7y ago
Live design review: never use type “string” for keys and values in protocol buffers. Always use “bytes”.
- praneshp 7y agohey can you add why?
- mdouglass 7y agoWhy is that? My google-fu only turns up references to invalid utf-8 data, but that didn’t sound like what you were referring to.
- jeffbee 7y agostring and bytes are the same on the wire but generate different application code. string is defined to be utf-8, so if you are planning to put arbitrary bytes in there, you can't use that type. in some languages string is checked for utf8 conformance, which is expensive. In some languages allocating strings is more expensive than allocating bytes. Only use string when you really mean utf8.
- deleted 7y ago[deleted]
- rakoo 7y agoIsn't that still ok for keys ? You typically want keys to be human-readable and -writable strings anyway
- jeffbee 7y agoI recently had this debate in a professional context. My correspondent argued that string was good for filenames, because it's a human-readable string. I pointed out all the different ways that a filename (on a variety of platforms!) can be constructed such that it will not conform to utf8. In a new system I would want to see an iron-clad reason for string in this case, because it's not obviously correct and the efficiency story isn't on the side of string, either. Luckily you can change from string to bytes at any time, since the wire format is the same, so this isn't the kind of mistake that gets cast in stone.
- jpalomaki 7y agoMaybe your key is hash of something. Converting it to string is extra effort and it takes more space.
- kodablah 7y ago> never use type “string” for keys and values in protocol buffers. Always use “bytes”. What are "keys and values" with regard to protobuf? Surely you don't mean maps as bytes can't be "keys" anyways. And why shouldn't you use string as a field type?
- atombender 7y agoI think they're referring to this app, which uses Protobuf strings to represent its keys and values.