From 1da28d1b2a7bf7ee24c042b34ef40eb8ab6dfbd4 Mon Sep 17 00:00:00 2001 From: Michael Voorhees Date: Tue, 26 Jan 2021 11:53:18 -0500 Subject: [PATCH] Add ModuleDefinition.ImmediateRead We are going to use this to do a multi stage load in parallel. We will be able to greatly reduce our load times using this new API. The way it's going to work is Stage 1 - In parallel, load the assemblies with ReadingMode.Deferred. Stage 2 - Populate our AssemblyResolver with a cache of all read assemblies. Stage 3 - In parallel, call ImmediateRead. What I really want is an API to load everything in stage 3. I found that ImmediateRead does not load method bodies. I don't know if/how you want want something like this exposed via the public API. For now I'm iterating the data model and forcing things to load that ImmediateRead did not cover. --- Mono.Cecil/AssemblyReader.cs | 1 + Mono.Cecil/ModuleDefinition.cs | 6 ++++++ Test/Mono.Cecil.Tests/ModuleTests.cs | 11 +++++++++++ 3 files changed, 18 insertions(+) diff --git a/Mono.Cecil/AssemblyReader.cs b/Mono.Cecil/AssemblyReader.cs index d18a51ed0..552e49102 100644 --- a/Mono.Cecil/AssemblyReader.cs +++ b/Mono.Cecil/AssemblyReader.cs @@ -166,6 +166,7 @@ protected override void ReadModule () public void ReadModule (ModuleDefinition module, bool resolve_attributes) { + module.ReadingMode = ReadingMode.Immediate; this.resolve_attributes = resolve_attributes; if (module.HasAssemblyReferences) diff --git a/Mono.Cecil/ModuleDefinition.cs b/Mono.Cecil/ModuleDefinition.cs index 22daecb9b..a254ae278 100644 --- a/Mono.Cecil/ModuleDefinition.cs +++ b/Mono.Cecil/ModuleDefinition.cs @@ -921,6 +921,12 @@ public IMetadataTokenProvider LookupToken (MetadataToken token) { return Read (token, (t, reader) => reader.LookupToken (t)); } + + public void ImmediateRead() + { + var moduleReader = new ImmediateModuleReader(Image); + moduleReader.ReadModule (this, resolve_attributes: true); + } readonly object module_lock = new object(); diff --git a/Test/Mono.Cecil.Tests/ModuleTests.cs b/Test/Mono.Cecil.Tests/ModuleTests.cs index 8e73750bf..a78ca4d7e 100644 --- a/Test/Mono.Cecil.Tests/ModuleTests.cs +++ b/Test/Mono.Cecil.Tests/ModuleTests.cs @@ -277,6 +277,17 @@ public void OpenModuleDeferred () } } + + [Test] + public void OpenModuleDeferredAndThenPerformImmediateRead () + { + using (var module = GetResourceModule ("hello.exe", ReadingMode.Deferred)) { + Assert.AreEqual (ReadingMode.Deferred, module.ReadingMode); + module.ImmediateRead(); + Assert.AreEqual (ReadingMode.Immediate, module.ReadingMode); + } + } + [Test] public void OwnedStreamModuleFileName () {