go-json-experiment / json

Experimental implementation of a proposed v2 encoding/json package
BSD 3-Clause "New" or "Revised" License
341 stars 11 forks source link

Somehow expose safeASCII option to outer types? #44

Open karalabe opened 1 week ago

karalabe commented 1 week ago

Currently the library internally uses AppendRaw with safeASCII set to true for scenarios where it is sure that the text is correct, but for all external user types, the library forces ascii escaping.

That escape check is very expensive for large objects. E.g. for an 8MB hex string (where my type knows that it generated correct ASCII), the default encoder through MarshalText appends it to the buffer in 5.5ms, whereas setting safeASCII to true (hacking the lib) will cause the data to be appended to the buffer in 0.8ms. That's almost a whole order of magnitude lost time on uselessly iterating the string.

I'm not entirely sure what the solution could be, maybe a RawMessage type that does not get escaped? Or some way for a V2 marshaller to signal that the output is "final"?

dsnet commented 1 week ago

This is somewhat related to https://github.com/golang/go/issues/33422. At present, the module errs on the side of correctness, which means that it always checks for validity according to the JSON grammar even if the caller knows for certain that the input is valid. I think this is still the right default, but an API that opts into looser guarantees seems plausible.

karalabe commented 1 week ago

Ah, I may have answered in not the best place, linking it here too. It's relevant for both discussions.

https://github.com/go-json-experiment/json/issues/45#issuecomment-2194068919