-
Notifications
You must be signed in to change notification settings - Fork 128
Speed up linker by 10% #1130
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
Merged
Merged
Speed up linker by 10% #1130
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.IO.MemoryMappedFiles; | ||
| using Mono.Collections.Generic; | ||
| using Mono.Cecil; | ||
|
|
||
|
|
@@ -15,6 +16,10 @@ public abstract class DirectoryAssemblyResolver : IAssemblyResolver { | |
|
|
||
| readonly Collection<string> directories; | ||
|
|
||
| protected readonly Dictionary<AssemblyDefinition, string> assemblyToPath = new Dictionary<AssemblyDefinition, string> (); | ||
|
|
||
| readonly List<MemoryMappedViewStream> viewStreams = new List<MemoryMappedViewStream> (); | ||
|
|
||
| public void AddSearchDirectory (string directory) | ||
| { | ||
| directories.Add (directory); | ||
|
|
@@ -40,7 +45,28 @@ protected AssemblyDefinition GetAssembly (string file, ReaderParameters paramete | |
| if (parameters.AssemblyResolver == null) | ||
| parameters.AssemblyResolver = this; | ||
|
|
||
| return ModuleDefinition.ReadModule (file, parameters).Assembly; | ||
| MemoryMappedViewStream viewStream = null; | ||
| try { | ||
| // Create stream because CreateFromFile(string, ...) uses FileShare.None which is too strict | ||
| using var fileStream = new FileStream (file, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, false); | ||
| using var mappedFile = MemoryMappedFile.CreateFromFile ( | ||
| fileStream, null, fileStream.Length, MemoryMappedFileAccess.Read, HandleInheritability.None, true); | ||
| viewStream = mappedFile.CreateViewStream (0, 0, MemoryMappedFileAccess.Read); | ||
|
|
||
| AssemblyDefinition result = ModuleDefinition.ReadModule (viewStream, parameters).Assembly; | ||
|
|
||
| assemblyToPath.Add (result, file); | ||
|
|
||
| viewStreams.Add (viewStream); | ||
|
|
||
| // We transferred the ownership of the viewStream to the collection. | ||
| viewStream = null; | ||
|
|
||
| return result; | ||
| } finally { | ||
| if (viewStream != null) | ||
| viewStream.Dispose (); | ||
| } | ||
| } | ||
|
|
||
| public virtual AssemblyDefinition Resolve (AssemblyNameReference name) | ||
|
|
@@ -89,6 +115,13 @@ public void Dispose () | |
|
|
||
| protected virtual void Dispose (bool disposing) | ||
| { | ||
| if (disposing) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should there be also finalizer?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The class is not holding unmanaged resources so I don't think we need one. |
||
| foreach (var viewStream in viewStreams) { | ||
| viewStream.Dispose (); | ||
| } | ||
|
|
||
| viewStreams.Clear (); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's needed because we want to
DisposetheviewStreamifModuleDefinition.ReadModuleabove failed for any reason - such assembly is unusable and we might as well throw away the mapping now. If accessing the assembly from the module succeeded, we do not want to dispose it because it's used by Cecil and theviewStreamscollection now manages the lifetime.It's also a reason why I'm not using a
using- theviewStreamhandling is odd. If you see a better way to do this, I can change it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might consider using
usingforfileStreamandmappedFileand keep the onlyviewStreamin custom finally to make the logic/flow more obvious