Skip to content

Commit 2ad9ff8

Browse files
committed
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.
1 parent 24c6ce4 commit 2ad9ff8

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Mono.Cecil/ModuleDefinition.cs

+9
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,15 @@ public IMetadataTokenProvider LookupToken (MetadataToken token)
921921
{
922922
return Read (token, (t, reader) => reader.LookupToken (t));
923923
}
924+
925+
public void ImmediateRead ()
926+
{
927+
if (!HasImage)
928+
return;
929+
ReadingMode = ReadingMode.Immediate;
930+
var moduleReader = new ImmediateModuleReader (Image);
931+
moduleReader.ReadModule (this, resolve_attributes: true);
932+
}
924933

925934
readonly object module_lock = new object();
926935

Test/Mono.Cecil.Tests/ModuleTests.cs

+21
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,27 @@ public void OpenModuleDeferred ()
277277
}
278278
}
279279

280+
281+
[Test]
282+
public void OpenModuleDeferredAndThenPerformImmediateRead ()
283+
{
284+
using (var module = GetResourceModule ("hello.exe", ReadingMode.Deferred)) {
285+
Assert.AreEqual (ReadingMode.Deferred, module.ReadingMode);
286+
module.ImmediateRead ();
287+
Assert.AreEqual (ReadingMode.Immediate, module.ReadingMode);
288+
}
289+
}
290+
291+
[Test]
292+
public void ImmediateReadDoesNothingForModuleWithNoImage ()
293+
{
294+
using (var module = new ModuleDefinition ()) {
295+
var initialReadingMode = module.ReadingMode;
296+
module.ImmediateRead ();
297+
Assert.AreEqual (initialReadingMode, module.ReadingMode);
298+
}
299+
}
300+
280301
[Test]
281302
public void OwnedStreamModuleFileName ()
282303
{

0 commit comments

Comments
 (0)