Compare commits

...

7 Commits

10 changed files with 308 additions and 70 deletions
+3 -3
View File
@@ -2,17 +2,17 @@
"versions": [
{
"unity_version":"2020",
"hybridclr" : { "branch":"v4.0.9"},
"hybridclr" : { "branch":"v4.0.10"},
"il2cpp_plus": { "branch":"v2020-4.0.8"}
},
{
"unity_version":"2021",
"hybridclr" : { "branch":"v4.0.9"},
"hybridclr" : { "branch":"v4.0.10"},
"il2cpp_plus": { "branch":"v2021-4.0.8"}
},
{
"unity_version":"2022",
"hybridclr" : { "branch":"v4.0.9"},
"hybridclr" : { "branch":"v4.0.10"},
"il2cpp_plus": { "branch":"v2022-4.0.8"}
}
]
+8 -2
View File
@@ -22,7 +22,7 @@ namespace HybridCLR.Editor.Commands
static BuildOptions GetBuildPlayerOptions(BuildTarget buildTarget)
{
BuildOptions options = BuildOptions.BuildScriptsOnly;
BuildOptions options = BuildOptions.None;
bool development = EditorUserBuildSettings.development;
if (development)
{
@@ -84,6 +84,10 @@ namespace HybridCLR.Editor.Commands
bool oldBuildScriptsOnly = EditorUserBuildSettings.buildScriptsOnly;
EditorUserBuildSettings.buildScriptsOnly = true;
string location = GetLocationPathName(outputPath, target);
string oldBuildLocation = EditorUserBuildSettings.GetBuildLocation(target);
EditorUserBuildSettings.SetBuildLocation(target, location);
switch (target)
{
case BuildTarget.StandaloneWindows:
@@ -113,7 +117,7 @@ namespace HybridCLR.Editor.Commands
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions()
{
scenes = EditorBuildSettings.scenes.Where(s => s.enabled).Select(s => s.path).ToArray(),
locationPathName = GetLocationPathName(outputPath, target),
locationPathName = location,
options = buildOptions,
target = target,
targetGroup = BuildPipeline.GetBuildTargetGroup(target),
@@ -122,6 +126,8 @@ namespace HybridCLR.Editor.Commands
var report = BuildPipeline.BuildPlayer(buildPlayerOptions);
EditorUserBuildSettings.buildScriptsOnly = oldBuildScriptsOnly;
EditorUserBuildSettings.SetBuildLocation(target, oldBuildLocation);
switch (target)
{
case BuildTarget.StandaloneWindows:
+1 -1
View File
@@ -46,7 +46,7 @@ namespace HybridCLR.Editor.Installer
EditorGUILayout.LabelField($"Package Version: v{_controller.PackageVersion}");
GUILayout.Space(5f);
EditorGUILayout.LabelField($"Installed libil2cpp Version: {_controller.InstalledLibil2cppVersion ?? "Unknown"}");
EditorGUILayout.LabelField($"Installed Version: {_controller.InstalledLibil2cppVersion ?? "Unknown"}");
GUILayout.Space(5f);
GUILayout.Space(10f);
+218 -33
View File
@@ -187,32 +187,210 @@ namespace HybridCLR.Editor.MethodBridge
}
}
public void Generate()
private List<MethodDesc> _managed2NativeMethodList0;
private List<MethodDesc> _native2ManagedMethodList0;
private List<MethodDesc> _adjustThunkMethodList0;
private List<TypeInfo> _structTypes0;
private void CollectTypesAndMethods()
{
_managed2NativeMethodList0 = _managed2nativeMethodSet.ToList();
_managed2NativeMethodList0.Sort((a, b) => string.CompareOrdinal(a.Sig, b.Sig));
_native2ManagedMethodList0 = _native2managedMethodSet.ToList();
_native2ManagedMethodList0.Sort((a, b) => string.CompareOrdinal(a.Sig, b.Sig));
_adjustThunkMethodList0 = _adjustThunkMethodSet.ToList();
_adjustThunkMethodList0.Sort((a, b) => string.CompareOrdinal(a.Sig, b.Sig));
var structTypeSet = new HashSet<TypeInfo>();
CollectStructDefs(_managed2NativeMethodList0, structTypeSet);
CollectStructDefs(_native2ManagedMethodList0, structTypeSet);
CollectStructDefs(_adjustThunkMethodList0, structTypeSet);
_structTypes0 = structTypeSet.ToList();
_structTypes0.Sort((a, b) => a.TypeId - b.TypeId);
CheckUnique(_structTypes0.Select(t => ToFullName(t.Klass)));
CheckUnique(_structTypes0.Select(t => t.CreateSigName()));
Debug.LogFormat("== before optimization struct:{3} managed2native:{0} native2managed:{1} adjustThunk:{2}",
_managed2NativeMethodList0.Count, _native2ManagedMethodList0.Count, _adjustThunkMethodList0.Count, _structTypes0.Count);
}
private class AnalyzeTypeInfo
{
public TypeInfo toSharedType;
public List<TypeInfo> fields;
public string signature;
}
private readonly Dictionary<TypeInfo, AnalyzeTypeInfo> _analyzeTypeInfos = new Dictionary<TypeInfo, AnalyzeTypeInfo>();
private readonly Dictionary<string, TypeInfo> _signature2Type = new Dictionary<string, TypeInfo>();
private AnalyzeTypeInfo CalculateAnalyzeTypeInfoBasic(TypeInfo typeInfo)
{
TypeSig type = typeInfo.Klass;
TypeDef typeDef = type.ToTypeDefOrRef().ResolveTypeDefThrow();
List<TypeSig> klassInst = type.ToGenericInstSig()?.GenericArguments?.ToList();
GenericArgumentContext ctx = klassInst != null ? new GenericArgumentContext(klassInst, null) : null;
ClassLayout sa = typeDef.ClassLayout;
var analyzeTypeInfo = new AnalyzeTypeInfo();
// don't share type with explicit layout
if (sa != null)
{
analyzeTypeInfo.toSharedType = typeInfo;
analyzeTypeInfo.signature = typeInfo.CreateSigName();
_signature2Type.Add(analyzeTypeInfo.signature, typeInfo);
return analyzeTypeInfo;
}
var fields = analyzeTypeInfo.fields = new List<TypeInfo>();
foreach (FieldDef field in typeDef.Fields)
{
if (field.IsStatic)
{
continue;
}
TypeSig fieldType = ctx != null ? MetaUtil.Inflate(field.FieldType, ctx) : field.FieldType;
fields.Add(GetSharedTypeInfo(fieldType));
}
return analyzeTypeInfo;
}
private string GetOrCalculateTypeInfoSignature(TypeInfo typeInfo)
{
if (!typeInfo.IsStruct)
{
return typeInfo.CreateSigName();
}
var ati = _analyzeTypeInfos[typeInfo];
//if (_analyzeTypeInfos.TryGetValue(typeInfo, out var ati))
//{
// return ati.signature;
//}
//ati = CalculateAnalyzeTypeInfoBasic(typeInfo);
//_analyzeTypeInfos.Add(typeInfo, ati);
if (ati.signature != null)
{
return ati.signature;
}
var sigBuf = new StringBuilder();
foreach (var field in ati.fields)
{
sigBuf.Append(GetOrCalculateTypeInfoSignature(ToIsomorphicType(field)));
}
return ati.signature = sigBuf.ToString();
}
private TypeInfo ToIsomorphicType(TypeInfo type)
{
if (!type.IsStruct)
{
return type;
}
if (!_analyzeTypeInfos.TryGetValue(type, out var ati))
{
ati = CalculateAnalyzeTypeInfoBasic(type);
_analyzeTypeInfos.Add(type, ati);
}
if (ati.toSharedType == null)
{
string signature = GetOrCalculateTypeInfoSignature(type);
Debug.Assert(signature == ati.signature);
if (_signature2Type.TryGetValue(signature, out var sharedType))
{
// Debug.Log($"[ToIsomorphicType] type:{type.Klass} ==> sharedType:{sharedType.Klass} signature:{signature} ");
ati.toSharedType = sharedType;
}
else
{
ati.toSharedType = type;
_signature2Type.Add(signature, type);
}
}
return ati.toSharedType;
}
private MethodDesc ToIsomorphicMethod(MethodDesc method)
{
var paramInfos = new List<ParamInfo>();
foreach (var paramInfo in method.ParamInfos)
{
paramInfos.Add(new ParamInfo() { Type = ToIsomorphicType(paramInfo.Type) });
}
var mbs = new MethodDesc()
{
MethodDef = method.MethodDef,
ReturnInfo = new ReturnInfo() { Type = ToIsomorphicType(method.ReturnInfo.Type) },
ParamInfos = paramInfos,
};
mbs.Init();
return mbs;
}
private List<MethodDesc> _managed2NativeMethodList;
private List<MethodDesc> _native2ManagedMethodList;
private List<MethodDesc> _adjustThunkMethodList;
private List<TypeInfo> structTypes;
private void BuildAnalyzeTypeInfos()
{
foreach (var type in _structTypes0)
{
ToIsomorphicType(type);
}
structTypes = _signature2Type.Values.ToList();
structTypes.Sort((a, b) => a.TypeId - b.TypeId);
}
private List<MethodDesc> ToUniqueOrderedList(List<MethodDesc> methods)
{
var methodMap = new SortedDictionary<string, MethodDesc>();
foreach (var method in methods)
{
var sharedMethod = ToIsomorphicMethod(method);
var sig = sharedMethod.Sig;
if (!methodMap.TryGetValue(sig, out var _))
{
methodMap.Add(sig, sharedMethod);
}
}
return methodMap.Values.ToList();
}
private void BuildOptimizedMethods()
{
_managed2NativeMethodList = ToUniqueOrderedList(_managed2NativeMethodList0);
_native2ManagedMethodList = ToUniqueOrderedList(_native2ManagedMethodList0);
_adjustThunkMethodList = ToUniqueOrderedList(_adjustThunkMethodList0);
}
private void OptimizationTypesAndMethods()
{
BuildAnalyzeTypeInfos();
BuildOptimizedMethods();
Debug.LogFormat("== after optimization struct:{3} managed2native:{0} native2managed:{1} adjustThunk:{2}",
_managed2NativeMethodList.Count, _native2ManagedMethodList.Count, _adjustThunkMethodList.Count, structTypes.Count);
}
private void GenerateCode()
{
var frr = new FileRegionReplace(_templateCode);
List<string> lines = new List<string>(20_0000);
List<MethodDesc> managed2NativeMethodList = _managed2nativeMethodSet.ToList();
managed2NativeMethodList.Sort((a, b) => string.CompareOrdinal(a.Sig, b.Sig));
List<MethodDesc> native2ManagedMethodList = _native2managedMethodSet.ToList();
native2ManagedMethodList.Sort((a, b) => string.CompareOrdinal(a.Sig, b.Sig));
List<MethodDesc> adjustThunkMethodList = _adjustThunkMethodSet.ToList();
adjustThunkMethodList.Sort((a, b) => string.CompareOrdinal(a.Sig, b.Sig));
Debug.LogFormat("== managed2native:{0} native2managed:{1} adjustThunk:{2}",
managed2NativeMethodList.Count, native2ManagedMethodList.Count, adjustThunkMethodList.Count);
var structTypeSet = new HashSet<TypeInfo>();
CollectStructDefs(managed2NativeMethodList, structTypeSet);
CollectStructDefs(native2ManagedMethodList, structTypeSet);
CollectStructDefs(adjustThunkMethodList, structTypeSet);
List<TypeInfo> structTypes = structTypeSet.ToList();
structTypes.Sort((a, b) => a.TypeId - b.TypeId);
var classInfos = new List<ClassInfo>();
var classTypeSet = new HashSet<TypeInfo>();
foreach (var type in structTypes)
@@ -220,32 +398,31 @@ namespace HybridCLR.Editor.MethodBridge
GenerateClassInfo(type, classTypeSet, classInfos);
}
CheckUnique(structTypes.Select(t => ToFullName(t.Klass)));
CheckUnique(structTypes.Select(t => t.CreateSigName()));
GenerateStructDefines(classInfos, lines);
GenerateStructureSignatureStub(structTypes, lines);
foreach(var method in managed2NativeMethodList)
// use structTypes0 to generate signature
GenerateStructureSignatureStub(_structTypes0, lines);
foreach (var method in _managed2NativeMethodList)
{
GenerateManaged2NativeMethod(method, lines);
}
GenerateManaged2NativeStub(managed2NativeMethodList, lines);
GenerateManaged2NativeStub(_managed2NativeMethodList, lines);
foreach (var method in native2ManagedMethodList)
foreach (var method in _native2ManagedMethodList)
{
GenerateNative2ManagedMethod(method, lines);
}
GenerateNative2ManagedStub(native2ManagedMethodList, lines);
GenerateNative2ManagedStub(_native2ManagedMethodList, lines);
foreach (var method in adjustThunkMethodList)
foreach (var method in _adjustThunkMethodList)
{
GenerateAdjustThunkMethod(method, lines);
}
GenerateAdjustThunkStub(adjustThunkMethodList, lines);
GenerateAdjustThunkStub(_adjustThunkMethodList, lines);
frr.Replace("CODE", string.Join("\n", lines));
@@ -254,6 +431,13 @@ namespace HybridCLR.Editor.MethodBridge
frr.Commit(_outputFile);
}
public void Generate()
{
CollectTypesAndMethods();
OptimizationTypesAndMethods();
GenerateCode();
}
private void CollectStructDefs(List<MethodDesc> methods, HashSet<TypeInfo> structTypes)
{
foreach (var method in methods)
@@ -324,7 +508,7 @@ namespace HybridCLR.Editor.MethodBridge
}
TypeSig fieldType = ctx != null ? MetaUtil.Inflate(field.FieldType, ctx) : field.FieldType;
fieldType = MetaUtil.ToShareTypeSig(corLibTypes, fieldType);
var fieldTypeInfo = _typeCreator.CreateTypeInfo(fieldType);
var fieldTypeInfo = ToIsomorphicType(_typeCreator.CreateTypeInfo(fieldType));
if (fieldTypeInfo.IsStruct)
{
GenerateClassInfo(fieldTypeInfo, typeSet, classInfos);
@@ -497,7 +681,8 @@ namespace HybridCLR.Editor.MethodBridge
lines.Add("FullName2Signature hybridclr::interpreter::g_fullName2SignatureStub[] = {");
foreach (var type in types)
{
lines.Add($"\t{{\"{ToFullName(type.Klass)}\", \"{type.CreateSigName()}\"}},");
TypeInfo isoType = ToIsomorphicType(type);
lines.Add($"\t{{\"{ToFullName(type.Klass)}\", \"{isoType.CreateSigName()}\"}},");
}
lines.Add("\t{ nullptr, nullptr},");
lines.Add("};");
+4 -3
View File
@@ -59,9 +59,10 @@ namespace HybridCLR.Editor.Settings
protected static string GetFilePath()
{
return typeof(T).GetCustomAttributes(inherit: true)
.Cast<FilePathAttribute>()
.FirstOrDefault(v => v != null)
?.filepath;
.Where(v => v is FilePathAttribute)
.Cast<FilePathAttribute>()
.FirstOrDefault()
?.filepath;
}
}
[AttributeUsage(AttributeTargets.Class)]
+21
View File
@@ -1,5 +1,26 @@
# 发布日志
## 4.0.15
发布日期 2024年1月2日.
### Runtime
- [fix] 修复计算未完全实例化的泛型类时将VAR和MVAR类型参数大小计算成sizeof(void*),导致计算出无效且过大的instance,在执行LayoutFieldsLocked过程中调用UpdateInstanceSizeForGenericClass错误地使用泛型基类instance覆盖设置了实例类型的instance值的严重bug
- [change] 支持打印热更新栈,虽然顺序不太正确
- [change] 使用HYBRIDCLR_MALLOC之类分配函数替换IL2CPP_MALLOC
- [refactor] 重构Config接口,统一通过GetRuntimeOption和SetRuntimeOption获取和设置选项
- [opt] 删除NewValueTypeVar和NewValueTypeInterpVar指令不必要的对结构memset操作
### Editor
- [fix] 修复Additional Compiler Arguments中输入 -nullable:enable 之后,Editor抛出InvalidCastException的bug。来自报告 https://github.com/focus-creative-games/hybridclr/issues/116
- [fix] 修复某些情况下报错:BuildFailedException: Build path contains a project previously built without the "Create Visual Studio Solution"
- [opt] 优化桥接函数生成,将同构的struct映射到同一个结构,减少了30-35%的桥接函数数量
- [change] StripAOTDllCommand导出时不再设置BuildScriptsOnly选项
- [change] 调整Installer窗口的显示内容
- [refactor] RuntimeApi中设置hybridclr参数的功能统一通过GetRuntimeOption和SetRuntimeOption函数
## 4.0.14
发布日期 2023.12.11.
+32 -27
View File
@@ -11,11 +11,6 @@ namespace HybridCLR
{
public static class RuntimeApi
{
#if UNITY_EDITOR
private static int s_interpreterThreadObjectStackSize = 128 * 1024;
private static int s_interpreterThreadFrameStackSize = 2 * 1024;
#endif
/// <summary>
/// 加载补充元数据assembly
/// </summary>
@@ -36,55 +31,65 @@ namespace HybridCLR
/// 获取解释器线程栈的最大StackObject个数(size*8 为最终占用的内存大小)
/// </summary>
/// <returns></returns>
#if UNITY_EDITOR
public static int GetInterpreterThreadObjectStackSize()
{
return s_interpreterThreadObjectStackSize;
return GetRuntimeOption(RuntimeOptionId.InterpreterThreadObjectStackSize);
}
#else
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern int GetInterpreterThreadObjectStackSize();
#endif
/// <summary>
/// 设置解释器线程栈的最大StackObject个数(size*8 为最终占用的内存大小)
/// </summary>
/// <param name="size"></param>
#if UNITY_EDITOR
public static void SetInterpreterThreadObjectStackSize(int size)
{
s_interpreterThreadObjectStackSize = size;
SetRuntimeOption(RuntimeOptionId.InterpreterThreadObjectStackSize, size);
}
#else
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void SetInterpreterThreadObjectStackSize(int size);
#endif
/// <summary>
/// 获取解释器线程函数帧数量(sizeof(InterpreterFrame)*size 为最终占用的内存大小)
/// </summary>
/// <returns></returns>
#if UNITY_EDITOR
public static int GetInterpreterThreadFrameStackSize()
{
return s_interpreterThreadFrameStackSize;
return GetRuntimeOption(RuntimeOptionId.InterpreterThreadFrameStackSize);
}
#else
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern int GetInterpreterThreadFrameStackSize();
#endif
/// <summary>
/// 设置解释器线程函数帧数量(sizeof(InterpreterFrame)*size 为最终占用的内存大小)
/// </summary>
/// <param name="size"></param>
#if UNITY_EDITOR
public static void SetInterpreterThreadFrameStackSize(int size)
{
s_interpreterThreadFrameStackSize = size;
SetRuntimeOption(RuntimeOptionId.InterpreterThreadFrameStackSize, size);
}
#if UNITY_EDITOR
private static readonly Dictionary<RuntimeOptionId, int> s_runtimeOptions = new Dictionary<RuntimeOptionId, int>();
public static void SetRuntimeOption(RuntimeOptionId optionId, int value)
{
s_runtimeOptions[optionId] = value;
}
#else
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void SetInterpreterThreadFrameStackSize(int size);
public static extern void SetRuntimeOption(RuntimeOptionId optionId, int value);
#endif
#if UNITY_EDITOR
public static int GetRuntimeOption(RuntimeOptionId optionId)
{
if (s_runtimeOptions.TryGetValue(optionId, out var value))
{
return value;
}
return 0;
}
#else
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern int GetRuntimeOption(RuntimeOptionId optionId);
#endif
}
}
+9
View File
@@ -0,0 +1,9 @@
namespace HybridCLR
{
public enum RuntimeOptionId
{
InterpreterThreadObjectStackSize = 1,
InterpreterThreadFrameStackSize = 2,
ThreadExceptionFlowSize = 3,
}
}
+11
View File
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 64be598b47302644a96013c74d945653
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "com.code-philosophy.hybridclr",
"version": "4.0.14",
"version": "4.0.15",
"displayName": "HybridCLR",
"description": "HybridCLR is a fully featured, zero-cost, high-performance, low-memory solution for Unity's all-platform native c# hotupdate.",
"category": "Runtime",