Our dedicated server serialises JSON very frequently. To keep that cheap, we build the JSON once, cache the TSharedPtr<FJsonObject>, and write it out each time it’s needed. However, writing it still allocates memory for every string value, because there’s no way to read an FJsonValue’s string without copying it:
- FJsonValue::AsString() returns an FString by value, and TryGetString(FString&) copies into the output string.
- TJsonValueString<CharType>::Value is protected, and there’s no accessor that returns a reference or a view.
- Code that walks the values can’t cast to FJsonValueString safely either. The engine is built without RTTI, and TJsonValueString<UTF8CHAR> values also report EJson::String.
FJsonSerializer::Serialize does the same internally. SerializeIfString writes Element->Value->AsString(), so every string is copied into a temporary FString just to be written. A cached object therefore still costs one heap allocation per string value every time it’s serialised.
As a possible solution, maybe FJsonValue could return the string as an FStringView. For example, a virtual such as TryGetStringView(FStringView&) would return a view of the stored string when the value holds TCHAR, and false otherwise, so callers could fall back to the existing copy. The JSON writers, including FJsonSerializer::SerializeIfString, could then write the view directly without the temporary FString.
Does that sound about right, or is there an existing way to do this that we’ve missed?
I don’t think you’re missing anything. This API is pretty inefficient.
We’ve started to make use of FSharedString but at the moment that’s limited to field keys. And some of the implementations of TryGetString need to generate a string on demand and so cannot be shared.
The EJson field type isn’t really supposed to represent the hierarchy of the value classes, it’s supposed to represent an element type in the JSON DOM, so that’s not surprising there.
Any solution is going to have to push the problem to the user: either a new type field which *does* represent the class hierarchy for you to switch on and downcast as appropriately - this gives maximum flexibility at the cost of forcing a lot of the handling to you.
The other option you mention - TryGetStringView - implies a new TryGetStringUtf8View too and the user would be forced to do an if (TryGetStringView()) {} else if (TryGetStringUtf8View()) {} else if (TryGetString()) else {} dance in order to access the string in the most efficient way. Though you could then wrap that up in a bool TryWithStringView(Lambda), where your lambda could be instantiated with both string view types. This could instantiate your generic lambda with the right view type - even in cases where the string is generated on demand - or return false if it wasn’t stringable.
Anyway, that exhausts what I think is possible. I think a future direction for our JSON implementation would be to ditch TCHARs and use FUtf8SharedStrings for everything, which probably has the right balance of efficiency and convenience.
The Json objects already use this approach of using shared pointers everywhere, but the underlying strings they contain are FStrings which is what makes using the Json code slow.
We’ve been going down the path of converting FString to FName where possible, but that can only go so far, as you can only use it on common strings, like type names.
If strings, in general, used a shared pointer instead, then that would be huge win for us in all areas, not just Json. I don’t mind if that was just a shared pointer internal to the FString, so that minimal changes would be needed elsewhere or scrapping the FString in favour of the new data type you mentioned: FUtf8SharedStrings.
If shared strings were used throughout, and all plug-ins adopted them (e.g. PlayFab Marketplace Plugin), then I think we would see a large performance boost.
FString is not going to become shared under the hood. The likely direction of the engine is to migrate to FUtf8String (also not shared) and then maybe to FUtf8SharedString (shared but immutable). Some systems - like JSON DOM values - are immutable after construction, so if we were going to break the JSON API, we’d probably go straight to FUtf8SharedString. This would be mostly ok except for stringified bools and numbers, as these would be generated on demand rather than cached (though the boolean ones could reuse a shared global FUtf8SharedString(“true”) and FUtf8SharedString(“false”), I guess). The JSON library predates shared strings, and wasn’t designed with string efficiency in mind, so FString passed by value pervades throughout.
FNames have their own set of problems - mainly that you cannot reclaim the memory they allocate - so I wouldn’t go in that direction for generic JSON.