性能优化(缓存优化, ddt优化, 有效)

This commit is contained in:
2025-12-09 11:18:11 +08:00
parent 85b505fe7e
commit 5ab19e39f2
8 changed files with 111 additions and 688 deletions

View File

@@ -5,6 +5,7 @@ using UnityEngine;
using System;
using Unity.Collections;
using UnityEngine.Rendering;
using System.IO;
namespace Demo.Game
{
@@ -59,5 +60,34 @@ namespace Demo.Game
if (Datas.IsCreated)
Datas.Dispose();
}
#region MyRegion
protected override bool IsImptSerialize => true;
protected override IEnumerator CreateAndLoadingImptCacheFile(ToolFile scriptFile, ToolFile cacheFile)
{
yield return this.ParseScript2Expr(scriptFile.LoadAsText());
using var stream = File.OpenWrite(cacheFile.GetFullPath());
using var writer = new BinaryWriter(stream);
writer.Write(Datas.Length);
foreach (var i in Datas)
{
writer.Write(i);
}
}
protected override IEnumerator LoadFromImptCacheFile(ToolFile cacheFile)
{
using var stream = File.OpenWrite(cacheFile.GetFullPath());
using var reader = new BinaryReader(stream);
int length = reader.ReadInt32();
Datas = new NativeArray<float>(length, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
for (int i = 0; i < length; i++)
{
Datas[i] = reader.ReadSingle();
}
yield break;
}
#endregion
}
}

View File

@@ -45,6 +45,7 @@ namespace Demo.Game
public IEnumerator GameInit()
{
float gameInitStartTime = Time.realtimeSinceStartup;
try
{
GameContent content = GameContent.instance;
@@ -186,6 +187,7 @@ namespace Demo.Game
yield break;
}
}
float loadRootObjectStartTime = Time.realtimeSinceStartup;
var rootFileName = (string)MainConfig.FindItem("root");
var rootObject = new ToolFile(Path.Combine(content.RootSourceDir, rootFileName));
rootObject.MustExistsPath();
@@ -216,11 +218,17 @@ namespace Demo.Game
}
//yield return
ConventionUtility.StartCoroutine(NDFS(rootGameObject));
float loadRootObjectEndTime = Time.realtimeSinceStartup;
float loadRootObjectElapsed = (loadRootObjectEndTime - loadRootObjectStartTime) * 1000f;
Debug.Log($"[GameInit] Load Root Object 耗时: {loadRootObjectElapsed:F2} ms", this);
}
}
finally
{
MainConfig.SaveProperties();
float gameInitEndTime = Time.realtimeSinceStartup;
float gameInitElapsed = (gameInitEndTime - gameInitStartTime) * 1000f;
Debug.Log($"[GameInit] 总耗时: {gameInitElapsed:F2} ms", this);
}
}

View File

@@ -275,7 +275,6 @@ namespace Demo
{
EnableScriptableObjectCounter++;
AllScriptableObjectCounterHierarchyItem.GetHierarchyItem().text = $"SOC: {ApplyScriptableObjectCounter}/{EnableScriptableObjectCounter}";
Debug.Log(nameof(EnableScript), this);
}
}
@@ -488,7 +487,6 @@ namespace Demo
{
ApplyScriptableObjectCounter++;
AllScriptableObjectCounterHierarchyItem.GetHierarchyItem().text = $"SOC: {ApplyScriptableObjectCounter}/{EnableScriptableObjectCounter}";
Debug.Log(nameof(ApplyScript), this);
}
// 统计更新能力
{

View File

@@ -207,31 +207,31 @@ namespace Demo
public partial class ScriptableObject
{
protected virtual bool IsImptSerialize => false;
protected virtual void LoadFromImptCacheFile(ToolFile file)
protected virtual IEnumerator LoadFromImptCacheFile(ToolFile cacheFile)
{
throw new NotImplementedException();
}
protected virtual void CreateAndLoadingImptCacheFile(ToolFile file)
protected virtual IEnumerator CreateAndLoadingImptCacheFile(ToolFile scriptFile, ToolFile cacheFile)
{
throw new NotImplementedException();
}
public RScriptImportClass GenerateImport()
{
RScriptImportClass importClass = new()
private readonly RScriptImportClass s_GenerateImport = new()
{
typeof(Mathf),
typeof(RandomTool),
};
return importClass;
public RScriptImportClass GenerateImport()
{
return s_GenerateImport;
}
public RScriptVariables GenerateVariables()
public RScriptVariables GenerateVariables(ScriptableObject self)
{
RScriptVariables variables = new()
{
{ "this", new() { data = this, type = this.GetType() } },
{ "self", new() { data = this, type = this.GetType() } },
{ "this", new() { data = self, type = self.GetType() } },
{ "self", new() { data = self, type = self.GetType() } },
{ "console", new() { data = new ConsoleTool(gameObject), type = typeof(ConsoleTool) } },
{ nameof(MathExtension.EaseCurveType), new() { data = EaseCurveTypeInstance.instance, type = typeof(EaseCurveTypeInstance) } },
{ $"Spline{nameof(SplineComputer.SampleMode)}",
@@ -247,9 +247,11 @@ namespace Demo
}
private static readonly Dictionary<string, object> s_FileLocker = new();
private static readonly Dictionary<string, RScriptEngine> s_RScriptEngineCache = new();
public IEnumerator ParseFromScriptFile2Expr(ToolFile file)
{
float parseStartTime = Time.realtimeSinceStartup;
IsParseScript2Expr = true;
try
{
@@ -262,57 +264,77 @@ namespace Demo
{
lastHashFile.MustExistsPath();
lastHashFile.SaveAsText(hash);
CreateAndLoadingImptCacheFile(bin);
yield return CreateAndLoadingImptCacheFile(file, bin);
}
else
{
LoadFromImptCacheFile(bin);
yield return LoadFromImptCacheFile(bin);
}
}
else
{
RScriptEngine engine = new();
RScriptImportClass importClass = GenerateImport();
RScriptVariables variables = GenerateVariables();
object locker;
bool is_engine_cached = false;
IEnumerator step = null;
if (lastHashFile.Exists() == false || lastHashFile.LoadAsText() != hash)
lock (s_RScriptEngineCache)
is_engine_cached = s_RScriptEngineCache.ContainsKey(file.GetFullPath());
if (is_engine_cached == false)
{
lastHashFile.MustExistsPath();
bin.MustExistsPath();
lastHashFile.SaveAsText(hash);
var script = file.LoadAsText();
var structBin = engine.Compile(script, importClass, variables);
lock (s_FileLocker)
lock (s_RScriptEngineCache)
{
if (s_FileLocker.TryGetValue(file.GetFullPath(), out locker) == false)
RScriptEngine engine = new();
RScriptImportClass importClass = GenerateImport();
RScriptVariables variables = GenerateVariables(this);
object locker;
if (lastHashFile.Exists() == false || lastHashFile.LoadAsText() != hash)
{
s_FileLocker.Add(file.GetFullPath(), locker = new object());
lastHashFile.MustExistsPath();
bin.MustExistsPath();
lastHashFile.SaveAsText(hash);
var script = file.LoadAsText();
var structBin = engine.Compile(script, importClass, variables);
lock (s_FileLocker)
{
if (s_FileLocker.TryGetValue(file.GetFullPath(), out locker) == false)
{
s_FileLocker.Add(file.GetFullPath(), locker = new object());
}
}
lock (locker)
{
bin.SaveAsBinary(RScriptSerializer.SerializeClass(structBin));
}
step = engine.RunAsync(script, importClass, variables);
}
else
{
RScriptContext.SerializableClass structBin;
lock (s_FileLocker)
{
if (s_FileLocker.TryGetValue(file.GetFullPath(), out locker) == false)
{
s_FileLocker.Add(file.GetFullPath(), locker = new object());
}
}
lock (locker)
{
structBin = RScriptSerializer.DeserializeClass(bin.LoadAsBinary());
}
step = engine.RunAsync(structBin, importClass, variables);
}
s_RScriptEngineCache.Add(file.GetFullPath(), engine);
}
lock (locker)
{
bin.SaveAsBinary(RScriptSerializer.SerializeClass(structBin));
}
step = engine.RunAsync(script, importClass, variables);
}
else
{
RScriptContext.SerializableClass structBin;
lock (s_FileLocker)
var engine = s_RScriptEngineCache[file.GetFullPath()];
lock (engine)
{
if (s_FileLocker.TryGetValue(file.GetFullPath(), out locker) == false)
{
s_FileLocker.Add(file.GetFullPath(), locker = new object());
}
engine.context.Variables["this"] = new() { data = this, type = this.GetType() };
engine.context.Variables["self"] = new() { data = this, type = this.GetType() };
step = engine.ReRunAsync();
}
lock (locker)
{
structBin = RScriptSerializer.DeserializeClass(bin.LoadAsBinary());
}
step = engine.RunAsync(structBin, importClass, variables);
}
yield return step;
}
@@ -320,17 +342,21 @@ namespace Demo
finally
{
IsParseScript2Expr = false;
float parseEndTime = Time.realtimeSinceStartup;
float parseElapsed = (parseEndTime - parseStartTime) * 1000f;
Debug.Log($"[{nameof(ParseFromScriptFile2Expr)}] {file.GetFullPath()} 耗时: {parseElapsed:F2} ms", this);
}
}
public IEnumerator ParseScript2Expr(string script)
{
float parseStartTime = Time.realtimeSinceStartup;
IsParseScript2Expr = true;
try
{
RScriptEngine engine = new();
RScriptImportClass importClass = GenerateImport();
RScriptVariables variables = GenerateVariables();
RScriptVariables variables = GenerateVariables(this);
var step = engine.RunAsync(script, importClass, variables);
yield return step;
@@ -338,6 +364,9 @@ namespace Demo
finally
{
IsParseScript2Expr = false;
float parseEndTime = Time.realtimeSinceStartup;
float parseElapsed = (parseEndTime - parseStartTime) * 1000f;
Debug.Log($"[{nameof(ParseScript2Expr)}] {this.ScriptName} 耗时: {parseElapsed:F2} ms", this);
}
}
}