Serializing large multidimensional arrays with JSON – speed

I’ve been messing around in unity and have begun learning about data serialization. My first tries have been to save the splatmaps/alphamaps of my terrain tiles as they change overtime.

The splatmaps/alphamaps are of size 5125123 = 786432. Over half a million float datapoints.

When I call SerializeObjects on my struct, it takes 50 seconds to serialize 16 splatmaps/alphamaps. The overall size of the file is 246mb. As this is my first time doing anything like this, I’m unsure if the time it takes is off by an order of magnitude or if it is within reasonable expectations of the time it would take to perform this operation.

Can you please help me figure out if there is an issue with the methodology ?

I’m using Newtonsoft and custom converters.

I’m having trouble understanding what the general benchmarks are and how to know if there’s an issue with the code itself. I’ve checked the output and it seems fine.

Code snippet where the serializing takes place

    private void SaveTerrainCellData(string SaveName)
    {
        string saveName = $"{SaveName} - TerrainCellData";
        string filePath = (Application.persistentDataPath + "/" + saveName + ".json");
        string lines = null;
        //Debug.Log(Application.persistentDataPath);

        // Use custom converters
        var settings = new JsonSerializerSettings
        {
            Converters = new JsonConverter[] { new Vector3Converter(), new StringEnumConverter() },
            Formatting = Formatting.Indented
        };

        foreach (TerrainCellData data in this.terrainCellsData)
        {
            lines += JsonConvert.SerializeObject(data, settings);
        }

        File.WriteAllText(filePath, lines);
        //await Task.Yield();
    }

The data structure

public struct TerrainCellData
{
    [SerializeField] public string TerrainName; 

    [Header("--- Weather ---")]
    public Weather Weather;
    public float MinutesSinceWeatherChange;

    [Header("--- Simulation ---")]
    public Biome Biome;
    public float WaterLevel;
    public float SunLevel;
    public int DirtPatchCount;
    public int GrassPatchCount;

    public List<Vector3> DirtAlphaMapGridID;
    public List<Vector3> GrassAlphaMapGridID;
    public Vector3 TrailingGrassPatch;
    public Vector3 TrailingDirtPatch;

    public Dictionary<ObjectName, Vector3> TrailingPlants;
    public int TrailingGrassPlantIndex;

    public float[,,] OriginalSplatmapData;
    public Dictionary<Vector3, float[,,]> AlphaMapGrid;
    public float[,,] AlphaMap;

    public void WriteData(string TerrainName,
                           Weather Weather, 
                           float MinutesSinceWeatherChange, 
                           Biome Biome, 
                           float WaterLevel, 
                           float SunLevel, 
                           int DirtPatchCount, 
                           int GrassPatchCount, 
                           List<Vector3> DirtAlphaMapGridID, 
                           List<Vector3> GrassAlphaMapGridID,
                           Vector3 TrailingGrassPatch,
                           Vector3 TrailingDirtPatch,
                           Dictionary<ObjectName, Vector3> TrailingPlants,
                           int TrailingGrassPlantIndex,
                           float[,,] OriginalSplatmapData,
                           Dictionary<Vector3, float[,,]> AlphaMapGrid,
                           float[,,] AlphaMap)
    {
        this.TerrainName = TerrainName;
        this.Weather = Weather;
        this.MinutesSinceWeatherChange = MinutesSinceWeatherChange;
        this.Biome = Biome;
        this.WaterLevel = WaterLevel;
        this.SunLevel = SunLevel;
        this.DirtPatchCount = DirtPatchCount;
        this.GrassPatchCount = GrassPatchCount;
        this.DirtAlphaMapGridID = DirtAlphaMapGridID;
        this.GrassAlphaMapGridID = GrassAlphaMapGridID;
        this.TrailingGrassPatch = TrailingGrassPatch;
        this.TrailingDirtPatch = TrailingDirtPatch;
        this.TrailingPlants = TrailingPlants;
        this.TrailingGrassPlantIndex = TrailingGrassPlantIndex;
        this.OriginalSplatmapData = OriginalSplatmapData;
        this.AlphaMapGrid = AlphaMapGrid;
        this.AlphaMap = AlphaMap;
    }
}

Thank you all very much!

New contributor

MrSig is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Json is not great for serializing large amounts of binary data, like large arrays. It will have significantly lower performance than just writing the data directly to a file.

A fairly common approach is to have one json file with all metadata, and a separate file with the actual raw binary data. You seem to have multiple arrays, so would need multiple files. A slightly more advanced design is to put all the files in a zip-archive with some custom extension, so you still have a single file to manage. You can use BinaryWriter.Write(ReadOnlySpan<byte>) to write an array directly to a stream, after casting the span for the array to the correct type.

A note about multi dimensional arrays. These can be a bit more difficult to use when serializing, since some methods only work with 1D arrays. There can also be issues with very large volumes, since there is a max size per array. Some of these issues can be worked around with unsafe code, but not all.

In my experience a better approach is to split your volume into separate 2D-slices, where each slice uses a plain 1D array as storage, and calculate the 2D-index yourself ([y * width + x]). This provides a decent tradeoff between allocation sizes, number of indirection, etc. But this will require a fair bit of more code, and is probably not necessary for smaller volumes. If your arrays are not actually volumetric data, and instead contains some kind of structure data, like RGB or vectors, you probably want to use an Array of structs to ensure type safety.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật