Skip to content

Commit

Permalink
Add ModuleDefinition.ImmediateRead
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 committed Jan 26, 2021
1 parent 24c6ce4 commit 1da28d1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions Mono.Cecil/AssemblyReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions Mono.Cecil/ModuleDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
11 changes: 11 additions & 0 deletions Test/Mono.Cecil.Tests/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
{
Expand Down

0 comments on commit 1da28d1

Please sign in to comment.