perf(save): encode detached snapshots and expose phase timings
This commit is contained in:
@@ -108,8 +108,8 @@ namespace ShrinkDataSaver
|
||||
}
|
||||
}
|
||||
|
||||
public UniTask<bool> ExistsAsync(string path, CancellationToken ct = default) =>
|
||||
Task.Run(() => File.Exists(Resolve(path)), ct).AsUniTask();
|
||||
public async UniTask<bool> ExistsAsync(string path, CancellationToken ct = default) =>
|
||||
await Task.Run(() => File.Exists(Resolve(path)), ct);
|
||||
|
||||
public async UniTask DeleteAsync(string path, CancellationToken ct = default)
|
||||
{
|
||||
|
||||
@@ -59,8 +59,21 @@ namespace ShrinkDataSaver
|
||||
}
|
||||
|
||||
|
||||
public sealed class SavePhaseTimings
|
||||
{
|
||||
public double CaptureMilliseconds { get; internal set; }
|
||||
public double EncodeMilliseconds { get; internal set; }
|
||||
public double EncryptMilliseconds { get; internal set; }
|
||||
public double WriteMilliseconds { get; internal set; }
|
||||
public long EncodedBytes { get; internal set; }
|
||||
}
|
||||
|
||||
public class SaveOptions
|
||||
{
|
||||
/// <summary>Only the detached JToken snapshot is encoded off-thread; module capture stays on the caller thread.</summary>
|
||||
public bool EncodeInBackground = false;
|
||||
public bool CollectTimings = false;
|
||||
public SavePhaseTimings LastTimings { get; internal set; }
|
||||
public string SlotName = "New Save";
|
||||
public bool CaptureScreenshot = false;
|
||||
#if UNITY_5_3_OR_NEWER
|
||||
|
||||
+38
-15
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Newtonsoft.Json.Linq;
|
||||
@@ -183,6 +185,14 @@ namespace ShrinkDataSaver
|
||||
{
|
||||
options ??= new SaveOptions();
|
||||
ValidateSlotIndex(slotIndex);
|
||||
ct.ThrowIfCancellationRequested();
|
||||
var timings = options.CollectTimings ? new SavePhaseTimings() : null;
|
||||
options.LastTimings = timings;
|
||||
var captureStarted = Stopwatch.GetTimestamp();
|
||||
var encrypt = options.Encrypt;
|
||||
var encryptionKey = options.EncryptionKey;
|
||||
var encodeInBackground = options.EncodeInBackground;
|
||||
var storage = _storage;
|
||||
|
||||
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||
OnSaveStarted?.Invoke(new SaveStartedEventArgs { SlotIndex = slotIndex, Timestamp = timestamp });
|
||||
@@ -198,7 +208,7 @@ namespace ShrinkDataSaver
|
||||
SaveVersion = _currentSaveVersion,
|
||||
LastModified = timestamp,
|
||||
PlaytimeSeconds = GetCurrentPlaytime(),
|
||||
IsEncrypted = options.Encrypt
|
||||
IsEncrypted = encrypt
|
||||
}
|
||||
};
|
||||
|
||||
@@ -233,24 +243,35 @@ namespace ShrinkDataSaver
|
||||
}
|
||||
}
|
||||
|
||||
if (options.Encrypt)
|
||||
if (timings != null) timings.CaptureMilliseconds = PhaseElapsed(captureStarted);
|
||||
ct.ThrowIfCancellationRequested();
|
||||
// Modules have been materialized as private JSON tokens; the worker never touches providers or engine objects.
|
||||
byte[] Encode()
|
||||
{
|
||||
if (string.IsNullOrEmpty(options.EncryptionKey))
|
||||
ct.ThrowIfCancellationRequested();
|
||||
var stage = Stopwatch.GetTimestamp();
|
||||
if (encrypt)
|
||||
{
|
||||
throw new ArgumentException("EncryptionKey missing.");
|
||||
if (string.IsNullOrEmpty(encryptionKey)) throw new ArgumentException("EncryptionKey missing.");
|
||||
var modulesBytes = DataSerializer.Serialize(modulesDict);
|
||||
if (timings != null) timings.EncodeMilliseconds += PhaseElapsed(stage);
|
||||
stage = Stopwatch.GetTimestamp();
|
||||
var encryptedBytes = SaveEncryptor.Encrypt(modulesBytes, encryptionKey);
|
||||
if (timings != null) timings.EncryptMilliseconds = PhaseElapsed(stage);
|
||||
packet.EncryptedModules = Convert.ToBase64String(encryptedBytes);
|
||||
}
|
||||
|
||||
var modulesBytes = DataSerializer.Serialize(modulesDict);
|
||||
var encryptedBytes = SaveEncryptor.Encrypt(modulesBytes, options.EncryptionKey);
|
||||
packet.EncryptedModules = Convert.ToBase64String(encryptedBytes);
|
||||
else packet.Modules = modulesDict;
|
||||
ct.ThrowIfCancellationRequested();
|
||||
stage = Stopwatch.GetTimestamp();
|
||||
var bytes = DataSerializer.Serialize(packet);
|
||||
if (timings != null) { timings.EncodeMilliseconds += PhaseElapsed(stage); timings.EncodedBytes = bytes.Length; }
|
||||
return bytes;
|
||||
}
|
||||
else
|
||||
{
|
||||
packet.Modules = modulesDict;
|
||||
}
|
||||
|
||||
var finalBytes = DataSerializer.Serialize(packet);
|
||||
await _storage.WriteAsync(SlotPath(slotIndex), finalBytes, ct);
|
||||
var finalBytes = encodeInBackground ? await Task.Run(Encode, ct) : Encode();
|
||||
ct.ThrowIfCancellationRequested();
|
||||
var writeStarted = Stopwatch.GetTimestamp();
|
||||
await storage.WriteAsync(SlotPath(slotIndex), finalBytes, ct);
|
||||
if (timings != null) timings.WriteMilliseconds = PhaseElapsed(writeStarted);
|
||||
|
||||
OnSaveCompleted?.Invoke(new SaveCompletedEventArgs
|
||||
{
|
||||
@@ -271,6 +292,8 @@ namespace ShrinkDataSaver
|
||||
}
|
||||
}
|
||||
|
||||
private static double PhaseElapsed(long started) => (Stopwatch.GetTimestamp() - started) * 1000d / Stopwatch.Frequency;
|
||||
|
||||
public static async UniTask LoadSlotAsync(int slotIndex, string decryptionKey = null, CancellationToken ct = default)
|
||||
{
|
||||
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
|
||||
|
||||
Reference in New Issue
Block a user