-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Perf] Add IID Lookup Optimization (#849)
- Loading branch information
Showing
29 changed files
with
1,871 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
NOTICES AND INFORMATION | ||
Do Not Translate or Localize | ||
|
||
This software incorporates material from third parties. | ||
Microsoft makes certain open source code available at https://3rdpartysource.microsoft.com, | ||
or you may send a check or money order for US $5.00, including the product name, | ||
the open source component name, platform, and version number, to: | ||
|
||
Source Code Compliance Team | ||
Microsoft Corporation | ||
One Microsoft Way | ||
Redmond, WA 98052 | ||
USA | ||
|
||
Notwithstanding any other terms, you may reverse engineer this software to the extent | ||
required to debug changes to any libraries licensed under the GNU Lesser General Public License. | ||
|
||
--------------------------------------------------------- | ||
|
||
Mono.Cecil 0.11.4 - MIT | ||
|
||
Copyright (c) 2008 - 2015 Jb Evain | ||
Copyright (c) 2008 - 2011 Novell, Inc. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using Mono.Cecil; | ||
using Mono.Cecil.Cil; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace GuidPatch | ||
{ | ||
static class CecilExtensions | ||
{ | ||
internal static Guid? ReadGuidFromAttribute(this TypeReference type, TypeReference guidAttributeType, AssemblyDefinition winrtRuntimeAssembly) | ||
{ | ||
TypeDefinition def = type.Resolve(); | ||
var guidAttr = def.CustomAttributes.FirstOrDefault(attr => attr.AttributeType.Resolve() == guidAttributeType); | ||
if (guidAttr is null) | ||
{ | ||
TypeDefinition abiType = def.GetCswinrtAbiTypeDefinition(winrtRuntimeAssembly); | ||
if (abiType is not null) | ||
{ | ||
return abiType.ReadGuidFromAttribute(guidAttributeType, winrtRuntimeAssembly); | ||
} | ||
return null; | ||
} | ||
return new Guid((string)guidAttr.ConstructorArguments[0].Value); | ||
} | ||
|
||
internal static TypeDefinition GetCswinrtAbiTypeDefinition(this TypeReference type, AssemblyDefinition winrtRuntimeAssembly) | ||
{ | ||
var resolvedType = type.Resolve(); | ||
|
||
return resolvedType.Module.GetType($"ABI.{resolvedType.FullName}") ?? | ||
winrtRuntimeAssembly.MainModule.GetType($"ABI.{resolvedType.FullName}"); | ||
} | ||
|
||
internal static MethodDefinition CreateIIDDataGetter(TypeReference type, Guid iidValue, TypeDefinition dataBlockType, TypeDefinition parentType, TypeReference readOnlySpanOfByte, MethodReference readOnlySpanOfByteCtor) | ||
{ | ||
var guidDataMethod = new MethodDefinition($"<IIDData>{type.FullName}", MethodAttributes.Assembly | MethodAttributes.Static, readOnlySpanOfByte); | ||
|
||
WriteIIDDataGetterBody(guidDataMethod, type, iidValue, dataBlockType, parentType, readOnlySpanOfByteCtor); | ||
return guidDataMethod; | ||
} | ||
|
||
internal static void WriteIIDDataGetterBody(MethodDefinition method, TypeReference type, Guid iidValue, TypeDefinition dataBlockType, TypeDefinition parentType, MethodReference readOnlySpanOfByteCtor) | ||
{ | ||
var guidDataField = new FieldDefinition($"<IIDDataField>{type.FullName}", FieldAttributes.Private | FieldAttributes.Static | FieldAttributes.InitOnly | FieldAttributes.HasFieldRVA, dataBlockType) | ||
{ | ||
InitialValue = iidValue.ToByteArray() | ||
}; | ||
parentType.Fields.Add(guidDataField); | ||
|
||
var ilProcessor = method.Body.GetILProcessor(); | ||
ilProcessor.Clear(); | ||
ilProcessor.Append(Instruction.Create(OpCodes.Ldsflda, guidDataField)); | ||
ilProcessor.Append(Instruction.Create(OpCodes.Ldc_I4, 16)); | ||
ilProcessor.Append(Instruction.Create(OpCodes.Newobj, readOnlySpanOfByteCtor)); | ||
ilProcessor.Append(Instruction.Create(OpCodes.Ret)); | ||
} | ||
|
||
internal static TypeDefinition GetOrCreateDataBlockType(TypeDefinition parentType, int size) | ||
{ | ||
if (size < 0 || size > ushort.MaxValue) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(size)); | ||
} | ||
|
||
string typeName = $"__StaticDataBlock<>Size={size}"; | ||
|
||
var typeRef = new TypeReference(null, typeName, parentType.Module, parentType.Module) | ||
{ | ||
DeclaringType = parentType | ||
}; | ||
|
||
if (typeRef.Resolve() is TypeDefinition td) | ||
{ | ||
return td; | ||
} | ||
|
||
td = new TypeDefinition(null, typeName, TypeAttributes.AutoClass | TypeAttributes.Sealed | TypeAttributes.NestedAssembly | TypeAttributes.SequentialLayout | TypeAttributes.AnsiClass, new TypeReference("System", "ValueType", parentType.Module, parentType.Module.TypeSystem.CoreLibrary)) | ||
{ | ||
PackingSize = 1, | ||
ClassSize = size | ||
}; | ||
|
||
parentType.NestedTypes.Add(td); | ||
|
||
return td; | ||
} | ||
|
||
internal static TypeReference? FindTypeReference(ModuleDefinition module, string ns, string name, string basicAssemblyName, bool isValueType) | ||
{ | ||
foreach (var asm in module.AssemblyReferences) | ||
{ | ||
if (asm.Name == basicAssemblyName || asm.Name.StartsWith($"{basicAssemblyName},")) | ||
{ | ||
TypeReference typeRef = new TypeReference(ns, name, module, asm, isValueType); | ||
if (typeRef.Resolve() != null) | ||
{ | ||
return module.ImportReference(typeRef); | ||
} | ||
break; | ||
} | ||
} | ||
var resolved = module.AssemblyResolver.Resolve(new AssemblyNameReference(basicAssemblyName, default)); | ||
if (resolved is null) | ||
{ | ||
return null; | ||
} | ||
return resolved.MainModule.Types.FirstOrDefault(t => t.Namespace == ns && t.Name == name); | ||
} | ||
} | ||
} |
Oops, something went wrong.