bool CheckAssemblySize(OptimizerContext context, Assembly assembly, int size) { int tolerance; string toleranceValue = assembly.Tolerance ?? Options.SizeCheckTolerance ?? "0.05%"; if (toleranceValue.EndsWith("%", StringComparison.Ordinal)) { var percent = float.Parse(toleranceValue.Substring(0, toleranceValue.Length - 1)); tolerance = (int)(assembly.Size * percent / 100.0f); } else { tolerance = int.Parse(toleranceValue); } context.LogDebug($"Size check: {assembly.Name}, actual={size}, expected={assembly.Size} (tolerance {toleranceValue})"); if (size < assembly.Size - tolerance) { context.LogWarning($"Assembly `{assembly.Name}` size below minimum: expected {assembly.Size} (tolerance {toleranceValue}), got {size}."); return(false); } if (size > assembly.Size + tolerance) { context.LogWarning($"Assembly `{assembly.Name}` size above maximum: expected {assembly.Size} (tolerance {toleranceValue}), got {size}."); return(false); } return(true); }
public bool CheckAndReportAssemblySize(OptimizerContext context, AssemblyDefinition assembly, int size) { if (IsEnabled(ReportMode.Detailed)) { var reportEntry = SizeReport.Assemblies.GetAssembly(assembly, true); reportEntry.Size = size; GenerateDetailedReport(assembly, reportEntry); CleanupSizeList(reportEntry.Namespaces); } if (!Options.CheckSize) { return(true); } var profile = SizeCheck.GetProfile(Options.ReportConfiguration, Options.ReportProfile, false); if (profile == null) { context.LogMessage(MessageImportance.High, $"Cannot find size check profile for configuration `{((Options.ReportConfiguration ?? "<null>"))}`, profile `{((Options.ReportProfile ?? "<null>"))}`."); return(true); } var configEntry = profile.Assemblies.GetAssembly(assembly, true); return(configEntry.Size == null || CheckAssemblySize(context, configEntry, size)); }
public static BasicBlockScanner Scan(OptimizerContext context, MethodDefinition method, int?debug = null) { var scanner = new BasicBlockScanner(context, method, debug); if (!scanner.Scan()) { return(null); } return(scanner); }
BasicBlockScanner(OptimizerContext context, MethodDefinition method, int?debug = null) { Context = context; Method = method; BlockList = new BasicBlockList(this, method.Body); Rewriter = new CodeRewriter(this); DebugLevel = debug ?? context.GetDebugLevel(method); }
/* * Replace the entire method body with `throw new PlatformNotSupportedException ()`. */ public static void ReplaceWithPlatformNotSupportedException(OptimizerContext context, MethodDefinition method) { if (method.IsAbstract || method.IsVirtual || method.IsConstructor || !method.IsIL) { throw ThrowUnsupported(method, "Cannot rewrite method of this type into throwing an exception."); } method.Body.Instructions.Clear(); method.Body.Instructions.Add(context.CreateNewPlatformNotSupportedException(method)); method.Body.Instructions.Add(Instruction.Create(OpCodes.Throw)); context.MarkAsConstantMethod(method, ConstantValue.Throw); }
private void Visit( OptimizerContext context, ISelection selection, FetchTask?parent) { if (selection.SelectionSet is not null) { foreach (IObjectType objectType in context.Operation.GetPossibleTypes(selection.SelectionSet)) { ISelectionSet selectionSet = context.Operation.GetSelectionSet(selection.SelectionSet, objectType); Visit(context, selectionSet, objectType, parent); } } }
/* * Replace the entire method body with `return true`. */ public static void ReplaceWithReturnTrue(OptimizerContext context, MethodDefinition method) { if (method.IsAbstract || method.IsVirtual || method.IsConstructor || !method.IsIL) { throw ThrowUnsupported(method, "Cannot rewrite method of this type into returning true."); } if (method.HasParameters) { throw ThrowUnsupported(method, "Can only rewrite parameterless methods into returning true."); } if (method.ReturnType.MetadataType != MetadataType.Boolean) { throw ThrowUnsupported(method, "Can only rewrite methods returning bool into returning true."); } method.Body.Instructions.Clear(); method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldc_I4_1)); method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); context.MarkAsConstantMethod(method, ConstantValue.True); }
public FinalCheckStep(OptimizerContext context) : base(context) { }
private void Visit( OptimizerContext context, ISelectionSet selectionSet, IObjectType objectType, FetchTask?parent) { if (!context.Processed.IsSupersetOf(selectionSet.Selections)) { IReadOnlyList <IFetchConfiguration> configurations = GetFetchConfigurations(objectType); var tasks = new List <FetchTask>(); FetchTask?task = null; foreach (IFetchConfiguration configuration in configurations) { if (configuration.CanHandleSelections( context.Operation, selectionSet, objectType, out IReadOnlyList <ISelection> handledSelections)) { var current = new FetchTask( new HashSet <ISelection>(handledSelections), configuration, parent?.Path.Push(parent) ?? ImmutableStack <FetchTask> .Empty, tasks); if (task is null || task.Selections.Count < handledSelections.Count) { task = current; } tasks.Add(current); } } if (task is not null) { tasks.Remove(task); foreach (ISelection handledSelection in task.Selections) { context.Processed.Add(handledSelection); } if (parent is null) { context.QueryPlan.Add(task); } else { parent.Children.Add(task); } parent = task; } } foreach (ISelection selection in selectionSet.Selections) { Visit(context, selection, parent); } }