speedup delta stating with disks with simple track tracking, should be within acceptable performance bounds (more or less)

This commit is contained in:
CasualPokePlayer 2023-04-02 03:45:34 -07:00
parent 3ffb6c3b52
commit df71de64c8
3 changed files with 18 additions and 4 deletions

View File

@ -9,7 +9,7 @@ namespace BizHawk.Common
/// </summary>
public static class DeltaSerializer
{
public static byte[] GetDelta<T>(ReadOnlySpan<T> original, ReadOnlySpan<T> data)
public static ReadOnlySpan<byte> GetDelta<T>(ReadOnlySpan<T> original, ReadOnlySpan<T> data)
where T : unmanaged
{
var orignalAsBytes = MemoryMarshal.AsBytes(original);
@ -94,7 +94,7 @@ namespace BizHawk.Common
}
}
return ret.Slice(0, retSize).ToArray();
return ret.Slice(0, retSize);
}
public static void ApplyDelta<T>(ReadOnlySpan<T> original, Span<T> data, ReadOnlySpan<byte> delta)

View File

@ -759,7 +759,7 @@ namespace BizHawk.Common
}
else
{
var delta = DeltaSerializer.GetDelta<T>(original, data);
var delta = DeltaSerializer.GetDelta<T>(original, data).ToArray(); // TODO: don't create array here (need .net update to write span to binary writer)
Sync(name, ref delta, useNull: false);
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using BizHawk.Common;
@ -12,6 +13,7 @@ namespace BizHawk.Emulation.Cores.Computers.Commodore64.Media
public const int FluxEntriesPerTrack = FluxBitsPerTrack / FluxBitsPerEntry;
private readonly int[][] _tracks;
private readonly int[][] _originalMedia;
private bool[] _usedTracks;
public bool Valid;
public bool WriteProtected;
@ -24,6 +26,7 @@ namespace BizHawk.Emulation.Cores.Computers.Commodore64.Media
_tracks = new int[trackCapacity][];
FillMissingTracks();
_originalMedia = _tracks.Select(t => (int[])t.Clone()).ToArray();
_usedTracks = new bool[trackCapacity];
Valid = true;
}
@ -46,6 +49,7 @@ namespace BizHawk.Emulation.Cores.Computers.Commodore64.Media
FillMissingTracks();
Valid = true;
_originalMedia = _tracks.Select(t => (int[])t.Clone()).ToArray();
_usedTracks = new bool[trackCapacity];
}
private int[] ConvertToFluxTransitions(int density, byte[] bytes, int fluxBitOffset)
@ -133,16 +137,26 @@ namespace BizHawk.Emulation.Cores.Computers.Commodore64.Media
public int[] GetDataForTrack(int halftrack)
{
_usedTracks[halftrack] = true; // TODO: probably can be smarter about this with the WriteProtected flag
return _tracks[halftrack];
}
public void SyncState(Serializer ser)
{
ser.Sync(nameof(WriteProtected), ref WriteProtected);
var oldUsedTracks = _usedTracks; // Sync changes reference if loading state (we don't care in the saving state case)
ser.Sync(nameof(_usedTracks), ref _usedTracks, useNull: false);
for (var i = 0; i < _tracks.Length; i++)
{
ser.SyncDelta($"MediaState{i}", _originalMedia[i], _tracks[i]);
if (_usedTracks[i])
{
ser.SyncDelta($"MediaState{i}", _originalMedia[i], _tracks[i]);
}
else if (ser.IsReader && oldUsedTracks[i]) // _tracks[i] might be different, but in the state it wasn't, so just copy _originalMedia[i]
{
_originalMedia[i].AsSpan().CopyTo(_tracks[i]);
}
}
}
}