Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question JSON-parsing and Steve?

Discussion in 'Entity Component System' started by nyanpath, Dec 20, 2020.

  1. nyanpath

    nyanpath

    Joined:
    Feb 9, 2018
    Posts:
    77
    Greetings,
    I am doing a lot of the JSON-parsing in my 9-5, and am looking into ways of being able to efficiently use the JSON-data with the powerful parallel processing that comes from the Jobs.

    Has anyone here done anything similar, tried, tested, decided for or against, either fully ECS, or quasi, or not? I would be very interested in hearing your findings on this. Thank you very much in advance for helping out.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    I can imagine you can run multiple JSON parsing in parallel. Haven't tested.
    But each thread would be responsible, for each individual JSON data.
    You parse back to class right?

    But if you have big JSON file, and you want split process between threads, I don't think there is easy way to do so.
    For the first, you would need split data in right point. You don't want to split data in mid of word.
    Then writing to a class, would required writing from multiple threads.

    Probably converting class to JSON string in parallel, is more feasible, as you could probably simply join job results strings, into one DATA, afer jobs are done.

    You would need to know about the jobs order however.
     
    Nyanpas likes this.
  3. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    Are you considering rolling your own serializer? That's almost never worth it. Parsing JSON sounds difficult to implement using Burst, especially if you need to support managed objects. And if you're dropping Burst, you might as well use one of the existing solutions. I think most serializers should work fine in a parallel job as long as you don't touch any (non-threadsafe) Unity APIs. Here's a summary:

    - JsonUtility - fast, thread-safe, limited feature set (works just like the built-in Unity serialization, which is a plus if you're using that extensively)
    - Newtonsoft.Json - versatile, popular, customizable, thread-safe, doesn't seem to like certain Unity types but it's mostly fixable
    - com.unity.serialization - pretty slow, internally uses burst+jobs for tokenization so I don't think you can even use it in a job, but maybe it's good for something I dunno

    Performance-wise, here's a random badly-written benchmark.
    upload_2020-12-20_10-56-34.png

    And another for large objects. Ouch, that took a while to run.
    upload_2020-12-20_11-11-5.png
     
    Antypodish and Nyanpas like this.
  4. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Not doing my own serialiser (I really hope, I hsd to make my own triangulator which took half a year), just wondering what is out there.

    I am currently using the JSONUtility. I have looked into the code and wondered how it would look in burst, but since it's all strings ("ew, gross") it's unlike the datatypes (bools/bytes/ints/floats) that I have so far used.

    I tend to have extremely large objects to parse, so the example above is a realistic encounter for me. However I don't intend parsing JSON at runtime, only in between play sessions or whenever there is no direct gameplay activity.

    Also, you can use structs with JSON apparently.
     
  5. BrendonSmuts

    BrendonSmuts

    Joined:
    Jun 12, 2017
    Posts:
    86
    Look at Project Tiny (com.unity.tiny.all). I just checked there now and found an implementation of an unmanaged JSON parser that looks like it is also Burst friendly. You should be able to simply copy that portion of the package out into whatever project you want to use it in without taking on all the other Tiny bagage.
     
    apkdev and Nyanpas like this.
  6. MNNoxMortem

    MNNoxMortem

    Joined:
    Sep 11, 2016
    Posts:
    723
    Regarding the serializer take a look at https://github.com/neuecc/Utf8Json and messagepack as alternative to json https://github.com/neuecc/MessagePack-CSharp (they both share quite some features and have quite some problems but if you want to understand how to be really fast they are a good start).

    Also checkout these two benchmarks:
    * https://theburningmonk.com/2014/08/json-serializers-benchmarks-updated-2/
    * https://aloiskraus.wordpress.com/2019/09/29/net-serialization-benchmark-2019-roundup/

    What do you want to achieve with Jobs in particular, that can't be done with regular threads better?
     
    nyanpath likes this.