Search code examples
c#jsongo.net-coresystem.text.json

Why is JSON deserialisation with System.Text.Json so slow?


I have the same minimal project that deserializes a json 100,000 times written in C# and in Go. The performance varies greatly. While it is nice to know that performance goals can be achieved by using Go, I would much prefer to achieve comparable results in C#. Given that C# is 193x slower, I assume the mistake is on my side, but I cannot figure out why.

Performance

$ dotnet run .
real    1m37.555s
user    1m39.552s
sys     0m0.729s

$ ./jsonperf
real    0m0.478s
user    0m0.500s
sys     0m0.011s

Source code C#

using System;

namespace jsonperf
{
    class Program
    {
        static void Main(string[] args)
        {
            var json = "{\"e\":\"trade\",\"E\":1633046399882,\"s\":\"BTCBUSD\",\"t\":243216662,\"p\":\"43818.22000000\",\"q\":\"0.00452000\",\"b\":3422298876,\"a\":3422298789,\"T\":1633046399882,\"m\":false,\"M\":true}";

            for (int i = 0; i < 100000; i++)
            {
                if (0 == i % 1000)
                {
                    Console.WriteLine($"Completed: {i}");
                }

                var obj = BinanceTradeUpdate.FromJson(json);
            }

            Console.WriteLine("Done");
        }
    }
}

and

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace jsonperf
{
    public class BinanceTradeUpdate
    {
        [JsonPropertyName("e")]
        public string EventType
        {
            get;
            set;
        }

        [JsonPropertyName("E")]
        public long EventUnixTimestamp
        {
            get;
            set;
        }

        [JsonIgnore]
        public DateTime EventTime
        {
            get
            {
                return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(EventUnixTimestamp);
            }
        }

        [JsonPropertyName("s")]
        public string MarketSymbol
        {
            get;
            set;
        }

        [JsonPropertyName("t")]
        public long TradeId
        {
            get;
            set;
        }

        [JsonPropertyName("p")]
        public double Price
        {
            get;
            set;
        }

        [JsonPropertyName("q")]

        public double Quantity
        {
            get;
            set;
        }

        [JsonPropertyName("b")]
        public long BuyerOrderId
        {
            get;
            set;
        }

        [JsonPropertyName("a")]
        public long SellerOrderId
        {
            get;
            set;
        }

        [JsonPropertyName("T")]
        public long TradeUnixTimestamp
        {
            get;
            set;
        }

        [JsonIgnore]
        public DateTime TradeTime
        {
            get
            {
                return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(TradeUnixTimestamp);
            }
        }

        [JsonPropertyName("m")]
        public bool BuyerIsMarketMaker
        {
            get;
            set;
        }

        [JsonPropertyName("M")]
        public bool UndocumentedFlag
        {
            get;
            set;
        }

        public static BinanceTradeUpdate FromJson(string json)
        {
            return JsonSerializer.Deserialize<BinanceTradeUpdate>(
                json,
                new JsonSerializerOptions()
                {
                    NumberHandling = JsonNumberHandling.AllowReadingFromString
                });
        }
    }
}

Source code Go

package main

import (
  "encoding/csv"
  "encoding/json"
  "fmt"
  "os"
  "strconv"
)

type Float64Str float64

func (f *Float64Str) UnmarshalJSON(b []byte) error {
  var s string

  // Try to unmarshal string first
  if err := json.Unmarshal(b, &s); err == nil {

    value, err := strconv.ParseFloat(s, 64)

    if err != nil {
      return err
    }

    *f = Float64Str(value)
    return nil
  }

  // If unsuccessful, unmarshal as float64
  return json.Unmarshal(b, (*float64)(f))
}

// Trade represents an exchange of assets in a given market
type Trade struct {
  EventType     string     json:"e"
  EventTime     int64      json:"E"
  MarketSymbol  string     json:"s"
  TradeID       int64      json:"t"
  Price         Float64Str json:"p"
  Quantity      Float64Str json:"q"
  BuyerOrderID  int64      json:"b"
  SellerOrderID int64      json:"a"
  TradeTime     int64      json:"T"
  IsBuyerMaker  bool       json:"m"
  Flag          bool       json:"M"
}

func main() {

  jsonString := "{\"e\":\"trade\",\"E\":1633046399882,\"s\":\"BTCBUSD\",\"t\":243216662,\"p\":\"43818.22000000\",\"q\":\"0.00452000\",\"b\":3422298876,\"a\":3422298789,\"T\":1633046399882,\"m\":false,\"M\":true}"

  // open stdout
  var stdwrite = csv.NewWriter(os.Stdout)

  // convert string several times into obj
  var trade = Trade{}
  counter := 0

  for i := 0; i < 100000; i++ {
    if err := json.Unmarshal([]byte(jsonString), &trade); err != nil {
      stdwrite.Flush()
      panic(err)
    } else {
      counter++

      if counter%1000 == 0 {
        fmt.Printf("%d elements read\n", counter)
      }
    }
  }
}

Solution

  • The reason this takes so long is that you’re initialising a new JsonSerializerOptions object everytime.

    Initialise the serialiser once & you’ll see huge performance improvements (70%+ for me).