优化UnityRenamePolicy,缓存计算结果,将整体混淆时间减少了一半左右

before-split
walon 2025-05-28 10:11:48 +08:00
parent 620d695880
commit b85f3f54a0
3 changed files with 58 additions and 24 deletions

View File

@ -126,9 +126,18 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
"OnCancel",
};
private readonly Dictionary<TypeDef, bool> _unitySourceGeneratedComputeCache = new Dictionary<TypeDef, bool>();
private readonly CachedDictionary<TypeDef, bool> _computeDeclaringTypeDisableAllMemberRenamingCache;
private readonly CachedDictionary<TypeDef, bool> _isInheritScriptCache;
private readonly CachedDictionary<TypeDef, bool> _isInheritFromMonoBehaviourCache;
private bool ComputeIsUnitySourceGeneratedAssemblyType(TypeDef typeDef)
public UnityRenamePolicy()
{
_computeDeclaringTypeDisableAllMemberRenamingCache = new CachedDictionary<TypeDef, bool>(ComputeDeclaringTypeDisableAllMemberRenaming);
_isInheritScriptCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsScriptType);
_isInheritFromMonoBehaviourCache = new CachedDictionary<TypeDef, bool>(MetaUtil.IsInheritFromMonoBehaviour);
}
private bool IsUnitySourceGeneratedAssemblyType(TypeDef typeDef)
{
if (typeDef.Name.StartsWith("UnitySourceGeneratedAssemblyMonoScriptTypes_"))
{
@ -157,18 +166,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
return false;
}
private bool IsUnitySourceGeneratedAssemblyType(TypeDef typeDef)
{
if (_unitySourceGeneratedComputeCache.TryGetValue(typeDef, out var result))
{
return result;
}
result = ComputeIsUnitySourceGeneratedAssemblyType(typeDef);
_unitySourceGeneratedComputeCache.Add(typeDef, result);
return result;
}
private bool DoesDeclaringTypeDisableAllMemberRenaming(TypeDef typeDef)
private bool ComputeDeclaringTypeDisableAllMemberRenaming(TypeDef typeDef)
{
if (typeDef.IsEnum && MetaUtil.HasBlackboardEnumAttribute(typeDef))
{
@ -182,20 +180,16 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{
return true;
}
if (IsUnitySourceGeneratedAssemblyType(typeDef))
{
return true;
}
return false;
}
public override bool NeedRename(TypeDef typeDef)
{
if (MetaUtil.IsScriptType(typeDef))
if (_isInheritScriptCache.GetValue(typeDef))
{
return false;
}
if (DoesDeclaringTypeDisableAllMemberRenaming(typeDef))
if (_computeDeclaringTypeDisableAllMemberRenamingCache.GetValue(typeDef))
{
return false;
}
@ -209,11 +203,11 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
public override bool NeedRename(MethodDef methodDef)
{
TypeDef typeDef = methodDef.DeclaringType;
if (MetaUtil.IsInheritFromMonoBehaviour(typeDef) && s_monoBehaviourEvents.Contains(methodDef.Name))
if (s_monoBehaviourEvents.Contains(methodDef.Name) && _isInheritFromMonoBehaviourCache.GetValue(typeDef))
{
return false;
}
if (DoesDeclaringTypeDisableAllMemberRenaming(typeDef))
if (_computeDeclaringTypeDisableAllMemberRenamingCache.GetValue(typeDef))
{
return false;
}
@ -235,11 +229,11 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{
return !MetaUtil.IsSerializableField(fieldDef);
}
if (DoesDeclaringTypeDisableAllMemberRenaming(typeDef))
if (_computeDeclaringTypeDisableAllMemberRenamingCache.GetValue(typeDef))
{
return false;
}
if (MetaUtil.HasBurstCompileAttribute(fieldDef) || MetaUtil.HasDOTSCompilerGeneratedAttribute(fieldDef))
if (MetaUtil.HasBurstCompileAttribute(fieldDef))
{
return false;
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.Utils
{
public class CachedDictionary<K, V>
{
private readonly Func<K, V> _valueFactory;
private readonly Dictionary<K, V> _cache = new Dictionary<K, V>();
public CachedDictionary(Func<K, V> valueFactory)
{
_valueFactory = valueFactory;
}
public V GetValue(K key)
{
if (!_cache.TryGetValue(key, out var value))
{
value = _valueFactory(key);
_cache[key] = value;
}
return value;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 66494da674feeb741b889590cb663d4e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: