Skip to content

Commit

Permalink
Add ModuleDefinition.ImmediateRead (#713)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mrvoorhe authored Feb 12, 2021
1 parent 25f85c9 commit 3c4ea3f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Mono.Cecil/ModuleDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,15 @@ public IMetadataTokenProvider LookupToken (MetadataToken token)
{
return Read (token, (t, reader) => reader.LookupToken (t));
}

public void ImmediateRead ()
{
if (!HasImage)
return;
ReadingMode = ReadingMode.Immediate;
var moduleReader = new ImmediateModuleReader (Image);
moduleReader.ReadModule (this, resolve_attributes: true);
}

readonly object module_lock = new object();

Expand Down
21 changes: 21 additions & 0 deletions Test/Mono.Cecil.Tests/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,27 @@ 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 ImmediateReadDoesNothingForModuleWithNoImage ()
{
using (var module = new ModuleDefinition ()) {
var initialReadingMode = module.ReadingMode;
module.ImmediateRead ();
Assert.AreEqual (initialReadingMode, module.ReadingMode);
}
}

[Test]
public void OwnedStreamModuleFileName ()
{
Expand Down

0 comments on commit 3c4ea3f

Please sign in to comment.