The Protobuf Sint32 Is a Zigzag (and a Negative One Is One Byte, Not Ten)
The one number that makes the difference is negative one. I declared the same field two ways and encoded the same value. As an int32, minus one goes out as 08ffffffffffffffffff01, the tag plus a full ten-byte varint, because a negative 32-bit integer signs into the full 64-bit range the moment you write it. As a sint32, the very same value is 0801, tag and a single byte. That is not a rounding or a compression trick. The sint32 runs the value through a zigzag before it hits the varint, and the zigzag folds the sign into the low bit so that small negatives sit next to small positives instead of at the far end of the number line. Minus one maps to one, plus one to two, minus two to three, plus two to four, minus five to nine, plus five to ten. The low bit is the sign, and the rest is the magnitude, so the varint has almost nothing to write for a small number in either direction.
The price of that trick is that the decoder has to know to undo it, and when it does not, the value is wrong in a way that does not raise an error. I took the ten bytes that a real int32 writes for minus one and decoded them under a schema that declares a sint32 in the same position. The library handed back -2147483648, the minimum 32-bit integer, clean, no complaint. The zigzag was never unrolled, the raw varint just got re-read as if it had come from a signed source that never folded its sign. Turn it around and it is just as bad: the two bytes a sint32 writes for minus one, read as an int32, come back as plus one. A bool set to true writes 0801 on the wire, the same two bytes, and under a sint32 schema that reads as minus one. The bytes are all plain varints, and which value you get out is decided by the schema label, not by anything in the stream.
On the JSON side the split follows the 64-bit rule from the rest of the format. A sint32 comes back as an ordinary number, and a negative stays a number, minus five is minus five, no string, no problem, because 32-bit values fit a JavaScript number with room to spare. A sint64 behaves like every other 64-bit integer and serializes as a decimal string, minus five goes out as the four characters dash five, the same rule that makes an int64 order id a string. The input side is lenient the same way both libraries are: fromObject takes a plain negative number for a sint32 field without fuss, it does not make you spell the sign or wrap the value. There is nothing to escape or quote, the zigzag is a wire detail and the JSON never sees it.
Where the savings actually land is in a list, not a single field, because a single small value is one byte either way. I encoded the same three numbers as a packed repeated field in both types. As repeated int32 the payload is twelve bytes, 0a0c01ffffffffffffffffff0102, and the twelve is almost entirely the one negative, the minus one that took ten of the twelve. As repeated sint32 the same three numbers are 0a03020104, three bytes of payload, 02 01 04, the three zigzagged magnitudes back to back. A column of small positive-and-negative deltas, a counter that goes up and down, a signed offset, is exactly where the format earns its name. A field that only ever holds non-negative values gets nothing out of the switch, because the zigzag only helps the negative half, and for a value that is never negative it just adds a multiply and a shift to every encode.
That is why the diff tab treats an int32 to sint32 change as a silent reinterpret and not a loud break. Both ride wire type zero, so the tag does not move and every old byte still decodes, but the value a decoder reads for each byte is different, and the report says exactly that, old bytes still decode, the values are reinterpreted. It is the same class of change as an int32 to uint32 or to a bool, a same-wire swap where the reader is the only thing that changed. The tool handles all of these with the zigzag built in, the generated code keeps a sint32 as a signed 32-bit integer in every target, and the raw decode tab is where you catch a wrong label, because without the schema the bytes are just varints and only the tag tells you what family they are in. The converter this post measured is at proto.datajutsu.com, the tag and varint math underneath it is in the wire format post, and the same-wire reinterpret case is walked through in the diff report post.