加载性能优化, 暂时放弃异步加载
This commit is contained in:
@@ -247,7 +247,6 @@ 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)
|
||||
{
|
||||
@@ -263,79 +262,59 @@ namespace Demo
|
||||
{
|
||||
lastHashFile.MustExistsPath();
|
||||
lastHashFile.SaveAsText(hash);
|
||||
yield return CreateAndLoadingImptCacheFile(file, bin);
|
||||
yield return ConventionUtility.AvoidFakeStop(CreateAndLoadingImptCacheFile(file, bin));
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return LoadFromImptCacheFile(bin);
|
||||
yield return ConventionUtility.AvoidFakeStop(LoadFromImptCacheFile(bin));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
bool is_engine_cached = false;
|
||||
IEnumerator step = null;
|
||||
lock (s_RScriptEngineCache)
|
||||
is_engine_cached = s_RScriptEngineCache.ContainsKey(file.GetFullPath());
|
||||
if (is_engine_cached == false)
|
||||
RScriptEngine engine = new();
|
||||
RScriptImportClass importClass = GenerateImport();
|
||||
RScriptVariables variables = GenerateVariables(this);
|
||||
object locker;
|
||||
if (lastHashFile.Exists() == false || lastHashFile.LoadAsText() != hash)
|
||||
{
|
||||
lock (s_RScriptEngineCache)
|
||||
lastHashFile.MustExistsPath();
|
||||
bin.MustExistsPath();
|
||||
lastHashFile.SaveAsText(hash);
|
||||
var script = file.LoadAsText();
|
||||
|
||||
var structBin = engine.Compile(script, importClass, variables);
|
||||
lock (s_FileLocker)
|
||||
{
|
||||
RScriptEngine engine = new();
|
||||
RScriptImportClass importClass = GenerateImport();
|
||||
RScriptVariables variables = GenerateVariables(this);
|
||||
object locker;
|
||||
if (lastHashFile.Exists() == false || lastHashFile.LoadAsText() != hash)
|
||||
if (s_FileLocker.TryGetValue(file.GetFullPath(), out locker) == false)
|
||||
{
|
||||
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);
|
||||
s_FileLocker.Add(file.GetFullPath(), locker = new object());
|
||||
}
|
||||
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
|
||||
{
|
||||
var engine = s_RScriptEngineCache[file.GetFullPath()];
|
||||
lock (engine)
|
||||
RScriptContext.SerializableClass structBin;
|
||||
lock (s_FileLocker)
|
||||
{
|
||||
engine.context.Variables["this"] = new() { data = this, type = this.GetType() };
|
||||
engine.context.Variables["self"] = new() { data = this, type = this.GetType() };
|
||||
step = engine.ReRunAsync();
|
||||
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);
|
||||
|
||||
}
|
||||
yield return step;
|
||||
yield return step;// ConventionUtility.AvoidFakeStop(step);
|
||||
}
|
||||
}
|
||||
finally
|
||||
@@ -354,7 +333,7 @@ namespace Demo
|
||||
RScriptVariables variables = GenerateVariables(this);
|
||||
|
||||
var step = engine.RunAsync(script, importClass, variables);
|
||||
yield return step;
|
||||
yield return step;//ConventionUtility.AvoidFakeStop(step);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user