Skip to content

In Unity, the first call to MessagePackSerializer.SerializeToJson is very slow. #2239

@baiyufei

Description

@baiyufei

Bug description

In Unity, the first call to MessagePackSerializer.SerializeToJson is very slow, taking more than 500ms. I’m not sure whether this is a bug or expected behavior.

Repro steps

[MessagePackObject]
public class TestData {
    [Key(1)] public int TypeId;
}

private void Test() {
        var data = new TestData();

        var sw = new Stopwatch();
        sw.Start();
        MessagePackSerializer.SerializeToJson(data);
        sw.Stop();
        Debug.LogFormat("First MessagePackSerializer.SerializeToJson took {0} ms", sw.ElapsedMilliseconds);
        
        sw.Restart();
        MessagePackSerializer.SerializeToJson(data);
        sw.Stop();
        Debug.LogFormat("Second MessagePackSerializer.SerializeToJson took {0} ms", sw.ElapsedMilliseconds);
}

log:

First MessagePackSerializer.SerializeToJson took 559 ms
Second MessagePackSerializer.SerializeToJson took 0 ms

Expected behavior

The first call to MessagePackSerializer.SerializeToJson won't be so slow

Actual behavior

The first call to MessagePackSerializer.SerializeToJson is very slow, taking more than 500ms.

  • Version used: 3.1.4
  • Runtime: (Unity, mono, etc.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions