perf(save): encode detached snapshots and expose phase timings
Publish UPM package / publish (push) Successful in 2s
Publish NuGet packages / publish (push) Successful in 2m31s

This commit is contained in:
2026-09-29 10:14:43 +08:00
parent d26f1a94ef
commit cdb682f484
6 changed files with 65 additions and 19 deletions
+38 -15
View File
@@ -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();