Speed up renaming by storing less names in the typeNames dict.

merge() was pretty slow but is much faster now.
This commit is contained in:
de4dot 2012-01-27 22:01:20 +01:00
parent e9f6e2930d
commit 257456fd8b
2 changed files with 70 additions and 45 deletions

View File

@ -25,6 +25,8 @@ namespace de4dot.code.renamer {
abstract class TypeNames {
protected Dictionary<string, NameCreator> typeNames = new Dictionary<string, NameCreator>(StringComparer.Ordinal);
protected NameCreator genericParamNameCreator = new NameCreator("gparam_");
protected Dictionary<string, string> fullNameToShortName;
protected Dictionary<string, string> fullNameToShortNamePrefix;
public string create(TypeReference typeRef) {
if (typeRef.IsGenericInstance) {
@ -46,21 +48,28 @@ namespace de4dot.code.renamer {
if (typeNames.TryGetValue(typeFullName, out nc))
return nc.create();
var name = elementType.FullName;
var parts = name.Replace('/', '.').Split(new char[] { '.' });
var newName = parts[parts.Length - 1];
int tickIndex = newName.LastIndexOf('`');
if (tickIndex > 0)
newName = newName.Substring(0, tickIndex);
var fullName = elementType.FullName;
string shortName;
var dict = prefix == "" ? fullNameToShortName : fullNameToShortNamePrefix;
if (!dict.TryGetValue(fullName, out shortName)) {
fullName = fullName.Replace('/', '.');
int index = fullName.LastIndexOf('.');
shortName = index > 0 ? fullName.Substring(index + 1) : fullName;
return addTypeName(typeFullName, newName, prefix).create();
index = shortName.LastIndexOf('`');
if (index > 0)
shortName = shortName.Substring(0, index);
}
return addTypeName(typeFullName, shortName, prefix).create();
}
static string getPrefix(TypeReference typeRef) {
string prefix = "";
while (typeRef is PointerType) {
typeRef = ((PointerType)typeRef).ElementType;
while (typeRef is TypeSpecification) {
if (typeRef.IsPointer)
prefix += "p";
typeRef = ((TypeSpecification)typeRef).ElementType;
}
return prefix;
}
@ -97,42 +106,50 @@ namespace de4dot.code.renamer {
}
class VariableNameCreator : TypeNames {
public VariableNameCreator(bool init = true) {
if (!init)
return;
initTypeName("System.Boolean", "bool");
initTypeName("System.Byte", "byte");
initTypeName("System.Char", "char");
initTypeName("System.Double", "double");
initTypeName("System.Int16", "short");
initTypeName("System.Int32", "int");
initTypeName("System.Int64", "long");
initTypeName("System.IntPtr", "intptr", "IntPtr");
initTypeName("System.SByte", "sbyte", "SByte");
initTypeName("System.Single", "float");
initTypeName("System.String", "string");
initTypeName("System.UInt16", "ushort", "UShort");
initTypeName("System.UInt32", "uint", "UInt");
initTypeName("System.UInt64", "ulong", "ULong");
initTypeName("System.UIntPtr", "uintptr", "UIntPtr");
initTypeName("System.Decimal", "decimal");
static Dictionary<string, string> ourFullNameToShortName;
static Dictionary<string, string> ourFullNameToShortNamePrefix;
static VariableNameCreator() {
ourFullNameToShortName = new Dictionary<string, string>(StringComparer.Ordinal) {
{ "System.Boolean", "bool" },
{ "System.Byte", "byte" },
{ "System.Char", "char" },
{ "System.Double", "double" },
{ "System.Int16", "short" },
{ "System.Int32", "int" },
{ "System.Int64", "long" },
{ "System.IntPtr", "intptr" },
{ "System.SByte", "sbyte" },
{ "System.Single", "float" },
{ "System.String", "string" },
{ "System.UInt16", "ushort" },
{ "System.UInt32", "uint" },
{ "System.UInt64", "ulong" },
{ "System.UIntPtr", "uintptr" },
{ "System.Decimal", "decimal" },
};
ourFullNameToShortNamePrefix = new Dictionary<string, string>(StringComparer.Ordinal) {
{ "System.Boolean", "Bool" },
{ "System.Byte", "Byte" },
{ "System.Char", "Char" },
{ "System.Double", "Double" },
{ "System.Int16", "Short" },
{ "System.Int32", "Int" },
{ "System.Int64", "Long" },
{ "System.IntPtr", "IntPtr" },
{ "System.SByte", "SByte" },
{ "System.Single", "Float" },
{ "System.String", "String" },
{ "System.UInt16", "UShort" },
{ "System.UInt32", "UInt" },
{ "System.UInt64", "ULong" },
{ "System.UIntPtr", "UIntPtr" },
{ "System.Decimal", "Decimal" },
};
}
void initTypeName(string fullName, string newName, string ptrName = null) {
if (ptrName == null)
ptrName = upperFirst(newName);
initTypeName2(fullName, "", newName);
initTypeName2(fullName + "[]", "", newName);
initTypeName2(fullName + "[][]", "", newName);
initTypeName2(fullName + "[][][]", "", newName);
initTypeName2(fullName + "[0...,0...]", "", newName);
initTypeName2(fullName + "*", "p", ptrName);
initTypeName2(fullName + "**", "pp", ptrName);
}
void initTypeName2(string fullName, string prefix, string newName) {
addTypeName(fullName, newName, prefix);
addTypeName(fullName + "&", newName, prefix);
public VariableNameCreator() {
fullNameToShortName = ourFullNameToShortName;
fullNameToShortNamePrefix = ourFullNameToShortNamePrefix;
}
static string lowerLeadingChars(string name) {
@ -155,6 +172,14 @@ namespace de4dot.code.renamer {
}
class PropertyNameCreator : TypeNames {
static Dictionary<string, string> ourFullNameToShortName = new Dictionary<string, string>(StringComparer.Ordinal);
static Dictionary<string, string> ourFullNameToShortNamePrefix = new Dictionary<string, string>(StringComparer.Ordinal);
public PropertyNameCreator() {
fullNameToShortName = ourFullNameToShortName;
fullNameToShortNamePrefix = ourFullNameToShortNamePrefix;
}
protected override string fixName(string prefix, string name) {
return prefix.ToUpperInvariant() + upperFirst(name);
}

View File

@ -54,7 +54,7 @@ namespace de4dot.code.renamer {
public VariableNameState cloneParamsOnly() {
var vns = new VariableNameState();
vns.existingVariableNames = new ExistingNames();
vns.variableNameCreator = new VariableNameCreator(false);
vns.variableNameCreator = new VariableNameCreator();
vns.existingVariableNames.merge(existingVariableNames);
vns.variableNameCreator.merge(variableNameCreator);
return vns;