性能优化(缓存优化, ddt优化, 有效)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user