Skip to content

Optimizes implicit assembly loading. Helps to reduce amount of faulted LoadAssembly calls. #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

dmitriyse
Copy link
Contributor

What does this implement/fix? Explain your changes.

  1. Assembly.GetTypes replaced to Assembly.GetExportedTypes. So we are really skipping non-public types now.
  2. Type.IsNested types are excluded from the names collection that is used for implicit assembly loading.

Does this close any currently open issues?

no
...

Any other comments?

VS 2017 + Docker is insensitive to DebuggerNonUserCode, and LoadAssembly exceptions produces long trash in the debug log.

  • This fix speedups unit tests (up to 3x). And probably speedups initialization of production projects.
    ...

Checklist

Check all those that are applicable and complete.

  • Make sure to include one or more tests for your change
  • If an enhancement PR, please create docs and at best an example
  • Add yourself to AUTHORS
  • Updated the CHANGELOG

@mention-bot
Copy link

@dmitriyse, thanks! @vmuriart, @tonyroberts, @cgohlke, @tiran and @BartonCline, please review this.

@codecov
Copy link

codecov bot commented Aug 23, 2017

Codecov Report

Merging #528 into master will decrease coverage by 0.63%.
The diff coverage is 61.9%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #528      +/-   ##
==========================================
- Coverage    77.4%   76.76%   -0.64%     
==========================================
  Files          63       64       +1     
  Lines        5589     5630      +41     
  Branches      892      894       +2     
==========================================
- Hits         4326     4322       -4     
- Misses        970     1013      +43     
- Partials      293      295       +2
Flag Coverage Δ
#setup_linux 69.42% <ø> (ø) ⬆️
#setup_windows 75.95% <61.9%> (-0.63%) ⬇️
Impacted Files Coverage Δ
src/runtime/pythonengine.cs 77.68% <57.14%> (-0.64%) ⬇️
src/runtime/assemblymanager.cs 87.3% <64.28%> (-1.8%) ⬇️
src/runtime/constructorbinding.cs 9.85% <0%> (-29.58%) ⬇️
src/runtime/overload.cs 58.33% <0%> (-15.58%) ⬇️
src/runtime/pyiter.cs 63.63% <0%> (-13.64%) ⬇️
src/runtime/iterator.cs 81.81% <0%> (-7.08%) ⬇️
src/runtime/genericutil.cs 82.81% <0%> (-3.13%) ⬇️
src/runtime/moduleobject.cs 81.6% <0%> (-1.54%) ⬇️
src/runtime/pyobject.cs 38.85% <0%> (-0.92%) ⬇️
src/runtime/runtime.cs 90.98% <0%> (-0.4%) ⬇️
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fb84cd2...f016059. Read the comment docs.

@dmitriyse dmitriyse force-pushed the implicit_assembly_load_opt branch from a13e4f6 to e2efdda Compare August 28, 2017 13:14
@dmitriyse dmitriyse force-pushed the implicit_assembly_load_opt branch from e2efdda to 9304d72 Compare September 4, 2017 15:23
@dmitriyse dmitriyse force-pushed the implicit_assembly_load_opt branch 2 times, most recently from 09acf53 to e7edeab Compare September 7, 2017 15:51
@dmitriyse dmitriyse force-pushed the implicit_assembly_load_opt branch from e7edeab to 1fd5998 Compare September 22, 2017 01:30
@dmitriyse dmitriyse mentioned this pull request Nov 2, 2017
@dmitriyse dmitriyse force-pushed the implicit_assembly_load_opt branch from 1fd5998 to e4764f2 Compare November 16, 2017 22:12
@dmitriyse dmitriyse force-pushed the implicit_assembly_load_opt branch from e4764f2 to d09fc92 Compare January 12, 2018 18:41
Type[] types = new Type[0];
try
{
types = assembly.IsDynamic ? assembly.GetTypes():assembly.GetExportedTypes();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmitriyse can you please add explanation for why you added check for isDynamic?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be a test for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the documentation, GetExportedTypes() is not supported on dynamic types and will throw NotSupportedException

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cronan good explanation! still maybe worth adding a comment about this. especially explaining the switch from GetTypes to GetExportedTypes for normal types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmitriyse @Cronan ok, I see this comment in the header of the PR:

Assembly.GetTypes replaced to Assembly.GetExportedTypes. So we are really skipping non-public types now.
Type.IsNested types are excluded from the names collection that is used for implicit assembly loading.

foreach (Type t in types)
{
if ((t.Namespace ?? "") == nsname)
{
names.Add(t.Name);
if (!t.IsNested)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@why is this nested check needed? should there be a test for this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmitriyse ok, I see this comment in the header of the PR:

Type.IsNested types are excluded from the names collection that is used for implicit assembly loading.

catch(TypeLoadException)
{
// Do nothing.
// This problem usually occurs when transitive dependencies have references to older packages than main application.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmitriyse what do you mean by transitive dependencies?

Copy link
Contributor

@Cronan Cronan Feb 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be wrong, but ReflectionTypeLoadException is thrown by GetTypes() if any of the dependent assemblies can't be loaded (with a list of the loaded types), but GetExportedTypes() throws FileNotFoundException with no information. I'm not sure if TypeLoadException is thrown here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cronan i think you are right about TypeLoadException -> ReflectionTypeLoadException, these are 2 different exceptions and I believe this was a typo from @dmitriyse:

https://msdn.microsoft.com/en-us/library/system.typeloadexception(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.reflection.reflectiontypeloadexception(v=vs.110).aspx

@@ -8,6 +8,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
## [unreleased][]

### Added
- Optimized implicit assembly loading on module import, PythonEngine.ImplicitAssemblyLoading event added.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmitriyse please add reference to this pull request and issues from which you referred to this pull request.

@@ -196,7 +196,17 @@ public static Assembly LoadAssembly(string name)
Assembly assembly = null;
try
{
assembly = Assembly.Load(name);
var importEvent = new ImplicitAssemblyLoadingEventArgs(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmitriyse why did you augment assembly loading with this event, I cannot see the purpose of this.

@den-run-ai
Copy link
Contributor

@dmitriyse I left some comments in this pull request. Once you explain the intent of this code, feel free to merge this one with all CI's passing tests. And please address @Cronan concern with likely incorrect exception names.

@den-run-ai
Copy link
Contributor

@Cronan thanks for reviewing this!

@dmitriyse if you can come up with a test for nested types, that would be great.

@filmor
Copy link
Member

filmor commented Aug 21, 2018

To be honest, I don't see the point of the whole event/event-args part of this pull request. SkipAssemblyLoad is never set explicitly, so I fail to see where we actually skip anything. I'll prepare a PR that only includes the IsNested check and GetExportedTypes, that should do the trick.

@filmor
Copy link
Member

filmor commented Oct 29, 2018

Superceded by #723.

@filmor filmor closed this Oct 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants