Is it correct to use unchecked String constructor here? It seams to me, that it should be correct, because serde_json should produce a valid json -> valid UTF-8 string, but I'm still a bit sheepish.
Even if it's correct should I? I don't expect it to be a performance bottleneck, but if I see, that I can satisfy the invariants of more performant unsafe function, should I do that?
As always with performance, two main rules are: measure if in doubt (criterion might help here), err on the safe side by default (i.e. if the measurable difference is small enough).
I wonder why you're doing this in the first place. Why not collect the iterator into an appropriate map and let serde take care of its de-/serialization.
This way you also don't limit yourself to JSON, but you can use anyDe-/Serializer.
I didn't want to allocate the map here if I had the option
The original code is very specific to json, so I opted to using json-specific API.
But I meant the original question as more general. All of us, let's say, re-use a value computed from a vector instead of recomputing it. Not because we measured performance impact, but because it's basic compitence to not do something in O(n) if you can do it in O(1) with no penalty. So the question is, should I think about using unsafe functions as "no penalty", if their invariants are meat by nature of surrounding local code. If I got @Cerber-Ursi's answer, the consensus is no, unless I can prove real-world benefit of such a change.
My goal is either to use no unsafe code at all, or to isolate the unsafe code in small separate crates, or at least separate modules, that can be easily audited. In Cargo.toml I use:
[lints.rust]
unsafe_code = "deny"
and then in the unsafe modules I use:
#![allow(unsafe_code)]
This motivates me to say no to unsafe code in situations like this.
If I needed it for performance in this case, I would create a small utility module that does the json serialize and returns a String.
That will require more explanation than the unsafe one, but I can't help but love it
PS: playground link to a working version of your thing for future generations