Skip to content

Commit 3c4ea3f

Browse files
authored
Add ModuleDefinition.ImmediateRead (#713)
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 25f85c9 commit 3c4ea3f

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
@@ -928,6 +928,15 @@ public IMetadataTokenProvider LookupToken (MetadataToken token)
928928
{
929929
return Read (token, (t, reader) => reader.LookupToken (t));
930930
}
931+
932+
public void ImmediateRead ()
933+
{
934+
if (!HasImage)
935+
return;
936+
ReadingMode = ReadingMode.Immediate;
937+
var moduleReader = new ImmediateModuleReader (Image);
938+
moduleReader.ReadModule (this, resolve_attributes: true);
939+
}
931940

932941
readonly object module_lock = new object();
933942

Test/Mono.Cecil.Tests/ModuleTests.cs

+21
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,27 @@ public void OpenModuleDeferred ()
283283
}
284284
}
285285

286+
287+
[Test]
288+
public void OpenModuleDeferredAndThenPerformImmediateRead ()
289+
{
290+
using (var module = GetResourceModule ("hello.exe", ReadingMode.Deferred)) {
291+
Assert.AreEqual (ReadingMode.Deferred, module.ReadingMode);
292+
module.ImmediateRead ();
293+
Assert.AreEqual (ReadingMode.Immediate, module.ReadingMode);
294+
}
295+
}
296+
297+
[Test]
298+
public void ImmediateReadDoesNothingForModuleWithNoImage ()
299+
{
300+
using (var module = new ModuleDefinition ()) {
301+
var initialReadingMode = module.ReadingMode;
302+
module.ImmediateRead ();
303+
Assert.AreEqual (initialReadingMode, module.ReadingMode);
304+
}
305+
}
306+
286307
[Test]
287308
public void OwnedStreamModuleFileName ()
288309
{

0 commit comments

Comments
 (0)