Cysharp / MemoryPack

Zero encoding extreme performance binary serializer for C# and Unity.
MIT License
3.29k stars 193 forks source link

Unexpected sequence reached end exception #213

Closed Meivyn closed 7 months ago

Meivyn commented 9 months ago

I am trying to implement MemoryPack into my existing serialization logic, but I'm getting this exception while I try to do so. I am really not sure if this is usage issue or lib issue, I based my code on what you did with your tests. It works when serializing only string[] or ExtraSongData[].

MemoryPack.MemoryPackSerializationException: Sequence reached end, reader can not provide more buffer.
   at MemoryPack.MemoryPackSerializationException.ThrowSequenceReachedEnd()
   at MemoryPack.MemoryPackReader.GetNextSpan(Int32 sizeHint)
   at MemoryPack.MemoryPackReader.ReadUtf8(Int32 utf8Length)
   at ConsoleApp.DifficultyData.MemoryPack.IMemoryPackable<ConsoleApp.DifficultyData>.Deserialize(MemoryPackReader& reader, DifficultyData& value) in C:\Users\Meivyn\RiderProjects\ConsoleApp8\ConsoleApp8\MemoryPack.Generator\MemoryPack.Generator.MemoryPackGenerator\ConsoleApp.DifficultyData.MemoryPackFormatter.g.cs:line 158
   at ConsoleApp.ExtraSongData.MemoryPack.IMemoryPackable<ConsoleApp.ExtraSongData>.Deserialize(MemoryPackReader& reader, ExtraSongData& value) in C:\Users\Meivyn\RiderProjects\ConsoleApp8\ConsoleApp8\MemoryPack.Generator\MemoryPack.Generator.MemoryPackGenerator\ConsoleApp.ExtraSongData.MemoryPackFormatter.g.cs:line 140
   at ConsoleApp.ExtraSongSerializedData.MemoryPack.IMemoryPackable<ConsoleApp.ExtraSongSerializedData>.Deserialize(MemoryPackReader& reader, ExtraSongSerializedData& value) in C:\Users\Meivyn\RiderProjects\ConsoleApp8\ConsoleApp8\MemoryPack.Generator\MemoryPack.Generator.MemoryPackGenerator\ConsoleApp.ExtraSongSerializedData.MemoryPackFormatter.g.cs:line 96
   at MemoryPack.Formatters.MemoryPackableFormatter`1.Deserialize(MemoryPackReader& reader, T& value)
   at MemoryPack.Streaming.MemoryPackStreamingSerializer.<DeserializeAsync>g__Deserialize|2_1[T](ReadOnlySequence`1& buffer, Int32 bufferAtLeast, List`1 itemBuffer, StrongBox`1 remain, Boolean bufferIsFull, MemoryPackReaderOptionalState state)
   at MemoryPack.Streaming.MemoryPackStreamingSerializer.DeserializeAsync[T](PipeReader pipeReader, Int32 bufferAtLeast, Int32 readMinimumSize, MemoryPackSerializerOptions options, CancellationToken cancellationToken)+MoveNext()

With this code (but do note that this also happens with Dictionary<string, ExtraSongData>):

private static async Task Serialize()
{
    await using var fileStream = File.Open(DataPath, FileMode.Create);
    var array = CustomSongsDataList.ToArray();
    await MemoryPackStreamingSerializer.SerializeAsync(fileStream, array.Length, array);
}

private static async Task Deserialize()
{
    if (!File.Exists(DataPath))
    {
        return;
    }

    await using var fileStream = File.Open(DataPath, FileMode.Open);
    await foreach (var pair in MemoryPackStreamingSerializer.DeserializeAsync<ExtraSongSerializedData>(fileStream))
    {
        CustomSongsData.TryAdd(pair.levelId, pair.data);
    }
}

[MemoryPackable]
public partial class ExtraSongSerializedData
{
    public string levelId;
    public ExtraSongData data;
}

[MemoryPackable]
public partial class ExtraSongData
{
    public string[] _genreTags;
    public Contributor[] contributors; //convert legacy mappers/lighters fields into contributors
    public string _customEnvironmentName;
    public string _customEnvironmentHash;
    public DifficultyData[] _difficulties;
    public string _defaultCharacteristic = null;

    public ColorScheme[] _colorSchemes; //beatmap 2.1.0, community decided to song-core ify colour stuff
    public string[] _environmentNames; //these have underscores but the actual format doesnt, I genuinely dont know what to go by so I went consistent with songcore

    //PinkCore Port
    public CharacteristicDetails[] _characteristicDetails;

    public ExtraSongData()
    {
    }

    [MemoryPackConstructor]
    public ExtraSongData(Contributor[] contributors, string customEnvironmentName, string customEnvironmentHash, DifficultyData[] difficulties)
    {
        this.contributors = contributors;
        _customEnvironmentName = customEnvironmentName;
        _customEnvironmentHash = customEnvironmentHash;
        _difficulties = difficulties;
    }
}

[MemoryPackable]
public partial class MapColor
{
    public float r;
    public float g;
    public float b;
    public float a = 1f;

    public MapColor(float r, float g, float b, float a = 1f)
    {
        this.r = r;
        this.g = g;
        this.b = b;
        this.a = a;
    }
}

[MemoryPackable]
public partial class Contributor
{
    public string _role;
    public string _name;
    public string _iconPath;

    [MemoryPackIgnore]
    public Sprite? icon = null;
}

[MemoryPackable]
public partial class ColorScheme //stuck to the same naming convention as the json itself
{
    public bool useOverride;
    public string colorSchemeId;
    public MapColor? saberAColor;
    public MapColor? saberBColor;
    public MapColor? environmentColor0;
    public MapColor? environmentColor1;
    public MapColor? obstaclesColor;
    public MapColor? environmentColor0Boost;
    public MapColor? environmentColor1Boost;
    //Not officially within the default scheme, added for consistency
    public MapColor? environmentColorW;
    public MapColor? environmentColorWBoost;
}

[MemoryPackable]
public partial class RequirementData
{
    public string[] _requirements;
    public string[] _suggestions;
    public string[] _warnings;
    public string[] _information;
}

[MemoryPackable]
public partial class CharacteristicDetails
{
    public string _beatmapCharacteristicName;
    public string? _characteristicLabel;
    public string? _characteristicIconFilePath = null;
}

[MemoryPackable]
public partial class DifficultyData
{
    public string _beatmapCharacteristicName;
    public BeatmapDifficulty _difficulty;
    public string _difficultyLabel;
    public RequirementData additionalDifficultyData;
    public MapColor? _colorLeft;
    public MapColor? _colorRight;
    public MapColor? _envColorLeft;
    public MapColor? _envColorRight;
    public MapColor? _envColorWhite;
    public MapColor? _envColorLeftBoost;
    public MapColor? _envColorRightBoost;
    public MapColor? _envColorWhiteBoost;
    public MapColor? _obstacleColor;
    public int? _beatmapColorSchemeIdx;
    public int? _environmentNameIdx;

    //PinkCore Port
    public bool? _oneSaber;
    public bool? _showRotationNoteSpawnLines;
    //Tags
    public string[] _styleTags;
}
hadashiA commented 7 months ago

I would only comment on the code you posted,

var array = CustomSongsDataList.ToArray();

Make sure this is an array of ExtraSongSerializedData.

If possible, could you provide a minimal sample that works? (The above code is not executable, and the type of CustomSongsDataList is unknown. That way we may be able to investigate a bit further.

Meivyn commented 7 months ago

What I currently use is this, which also produces the same error

using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using MemoryPack;
using MemoryPack.Streaming;
using Newtonsoft.Json;

namespace ConsoleApp
{
    internal class Program
    {
        private static ConcurrentDictionary<string, ExtraSongData> CustomSongsData = new();
        private static string DataPath = "SongCoreExtraData.json";

        private static async Task Main(string[] args)
        {
            var json = await File.ReadAllTextAsync(DataPath);
            CustomSongsData = JsonConvert.DeserializeObject<ConcurrentDictionary<string, ExtraSongData>>(json)!;

            await MemoryPack();
        }

        private static async Task MemoryPack()
        {
            var startTime = Stopwatch.GetTimestamp();
            await using var fileStream = File.Open("data.json", FileMode.Create);

            await MemoryPackStreamingSerializer.SerializeAsync(fileStream, CustomSongsData.Count, CustomSongsData);
            Console.WriteLine($"Serialization: {CustomSongsData.Count} entries in {Stopwatch.GetElapsedTime(startTime)}");

            fileStream.Position = 0;
            startTime = Stopwatch.GetTimestamp();

            CustomSongsData.Clear();
            await foreach (var pair in MemoryPackStreamingSerializer.DeserializeAsync<KeyValuePair<string, ExtraSongData>>(fileStream))
            {
                CustomSongsData.TryAdd(pair.Key, pair.Value);
            }
            Console.WriteLine($"Deserialization: {CustomSongsData.Count} entries in {Stopwatch.GetElapsedTime(startTime)}");
        }
    }

    [MemoryPackable]
    public partial class ExtraSongData
    {
        public string[] _genreTags;
        public Contributor[] contributors;
        public string _customEnvironmentName;
        public string _customEnvironmentHash;
        public DifficultyData[] _difficulties;
        public string _defaultCharacteristic = null;

        public ColorScheme[] _colorSchemes;
        public string[] _environmentNames;

        public CharacteristicDetails[] _characteristicDetails;
    }

    [MemoryPackable]
    public partial class MapColor
    {
        public float r;
        public float g;
        public float b;

        [DefaultValue(1)]
        public float a = 1f;
    }

    [MemoryPackable]
    public partial class Contributor
    {
        public string _role;
        public string _name;
        public string _iconPath;
    }

    [MemoryPackable]
    public partial class ColorScheme
    {
        public bool useOverride;
        public string colorSchemeId;
        public MapColor? saberAColor;
        public MapColor? saberBColor;
        public MapColor? environmentColor0;
        public MapColor? environmentColor1;
        public MapColor? obstaclesColor;
        public MapColor? environmentColor0Boost;
        public MapColor? environmentColor1Boost;
        public MapColor? environmentColorW;
        public MapColor? environmentColorWBoost;
    }

    [MemoryPackable]
    public partial class RequirementData
    {
        public string[] _requirements;
        public string[] _suggestions;
        public string[] _warnings;
        public string[] _information;
    }

    [MemoryPackable]
    public partial class CharacteristicDetails
    {
        public string _beatmapCharacteristicName;
        public string? _characteristicLabel;
        public string? _characteristicIconFilePath = null;
    }

    [MemoryPackable]
    public partial class DifficultyData
    {
        public string _beatmapCharacteristicName;
        public BeatmapDifficulty _difficulty;
        public string _difficultyLabel;
        public RequirementData additionalDifficultyData;
        public MapColor? _colorLeft;
        public MapColor? _colorRight;
        public MapColor? _envColorLeft;
        public MapColor? _envColorRight;
        public MapColor? _envColorWhite;
        public MapColor? _envColorLeftBoost;
        public MapColor? _envColorRightBoost;
        public MapColor? _envColorWhiteBoost;
        public MapColor? _obstacleColor;
        public int? _beatmapColorSchemeIdx;
        public int? _environmentNameIdx;

        //PinkCore Port
        public bool? _oneSaber;
        public bool? _showRotationNoteSpawnLines;
        //Tags
        public string[] _styleTags;
    }
}

SongCoreExtraData.json

hadashiA commented 7 months ago

Thank you for the reproduction code.

I have looked into it and it seems that your data has very long strings, over about 15KB. (e.g. _difficultyLabel.)

Since the default buffer size is 8KB, the buffer would overflow.

Check the argument for MemoryPackStreamingSerializer.DeserializeAsync.

The following seems to work:

MemoryPackStreamingSerializer.DeserializeAsync<ExtraSongData>(fileStream, bufferAtLeast: 65536, readMinimumSize: 65536))

bufferAtLeast is the minimum buffer required. It must be longer than the maximum length of the utf8 encoding of your string. Try specifying a larger value.

Meivyn commented 7 months ago

I did try to specify a larger buffer already... but I guess it wasn't big enough. Thanks for taking the time to look into it, I appreciate it.