Skip to content

Commit cefa377

Browse files
author
dse
committed
Merge
2 parents 090eb7e + e4764f2 commit cefa377

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
88
## [unreleased][]
99

1010
### Added
11+
- Optimized implicit assembly loading on module import, PythonEngine.ImplicitAssemblyLoading event added.
1112
- Improved performance. String marshaling between python and clr now cached.
1213
Cache reduces GC pressure and saves from extensive memory copying.
1314
- Added tool for debugging floating bugs. Stable tests are executed in the loop. ~100 cycles is enough to pop up any bugs.

src/runtime/assemblymanager.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,17 @@ public static Assembly LoadAssembly(string name)
194194
Assembly assembly = null;
195195
try
196196
{
197-
assembly = Assembly.Load(name);
197+
var importEvent = new ImplicitAssemblyLoadingEventArgs(name);
198+
if (importEvent.SkipAssemblyLoad)
199+
{
200+
return null;
201+
}
202+
203+
PythonEngine.RaiseAssemblyAsModuleImportingEvent(importEvent);
204+
if (!importEvent.SkipAssemblyLoad)
205+
{
206+
assembly = Assembly.Load(name);
207+
}
198208
}
199209
catch (Exception)
200210
{
@@ -341,8 +351,17 @@ internal static void ScanAssembly(Assembly assembly)
341351
// A couple of things we want to do here: first, we want to
342352
// gather a list of all of the namespaces contributed to by
343353
// the assembly.
354+
Type[] types = new Type[0];
355+
try
356+
{
357+
types = assembly.IsDynamic ? assembly.GetTypes():assembly.GetExportedTypes();
358+
}
359+
catch(TypeLoadException)
360+
{
361+
// Do nothing.
362+
// This problem usually occurs when transitive dependencies have references to older packages than main application.
363+
}
344364

345-
Type[] types = assembly.GetTypes();
346365
foreach (Type t in types)
347366
{
348367
string ns = t.Namespace ?? "";
@@ -417,12 +436,15 @@ public static List<string> GetNames(string nsname)
417436
{
418437
foreach (Assembly a in namespaces[nsname].Keys)
419438
{
420-
Type[] types = a.GetTypes();
439+
Type[] types = a.IsDynamic ? a.GetTypes(): a.GetExportedTypes();
421440
foreach (Type t in types)
422441
{
423442
if ((t.Namespace ?? "") == nsname)
424443
{
425-
names.Add(t.Name);
444+
if (!t.IsNested)
445+
{
446+
names.Add(t.Name);
447+
}
426448
}
427449
}
428450
}

src/runtime/pythonengine.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ public static string Compiler
139139

140140
internal static PyReferenceDecrementer CurrentRefDecrementer { get; private set; }
141141

142+
/// <summary>
143+
/// Fires when python engines importing module and probably tries to load an assembly.
144+
/// </summary>
145+
public static event EventHandler<ImplicitAssemblyLoadingEventArgs> ImplicitAssemblyLoading;
146+
142147
public static int RunSimpleString(string code)
143148
{
144149
return Runtime.PyRun_SimpleString(code);
@@ -560,6 +565,29 @@ internal static PyObject RunString(string code, IntPtr? globals, IntPtr? locals,
560565
}
561566
}
562567
}
568+
569+
internal static void RaiseAssemblyAsModuleImportingEvent(ImplicitAssemblyLoadingEventArgs e)
570+
{
571+
ImplicitAssemblyLoading?.Invoke(null, e);
572+
}
573+
}
574+
575+
public class ImplicitAssemblyLoadingEventArgs: EventArgs
576+
{
577+
public ImplicitAssemblyLoadingEventArgs(string moduleName)
578+
{
579+
ModuleName = moduleName;
580+
}
581+
582+
/// <summary>
583+
/// The name of the module to import that is probably assembly name.
584+
/// </summary>
585+
public string ModuleName { get; }
586+
587+
/// <summary>
588+
/// Set it to true, if you know that <see cref="ModuleName"/> is not an assembly to import.
589+
/// </summary>
590+
public bool SkipAssemblyLoad { get; set; }
563591
}
564592

565593
public enum RunFlagType

0 commit comments

Comments
 (0)