You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use Roslyn source generators for cdxml cmdletization #12760
Currently, when a cdxml module is imported the engine takes the following steps:
Parse an XML document
Build a string representing an entire PowerShell module based on the XML
The generated module essentially just builds args for a C# based CmdletAdapter implementation
If source generators were used instead, the first two steps would be moved to compile time and the last would have less overhead. They would also be less prone to some of PowerShell's module scope quirks like ErrorAction inheritance.
Reacted by Rain Sallow (/u/ta11ow), Flavien MICHALECZEK, Thomas Nieto, Rob Holt and Michael Klement
I think what Patrick Meinecke (@SeeminglyScience) is suggesting is that rather than shipping the XML and parsing it into a module at import time, we could create a build task to generate a binary module so that we only need to ship the module DLL. That way, all of that processing is front-loaded and users just end up importing the module DLL directly.
From my understanding you say about new tool/module to create specific binary modules.
I would expect that you describe how this should work in a full-fledged RFC.
From my understanding you say about new tool/module to create specific binary modules.
I would say that the suggestion is an alternative/addition to the current cmdletization API that would result in better performance and UX. It's like saying PowerShell is a compiler, it's not wrong but it's not the whole picture.
I would expect that you describe how this should work in a full-fledged RFC.
Yeah I think if this gets implemented it might be a good idea for the implementer to submit an RFC. If this is something you're interested in pursuing, I'd be happy to expand on any particular detail that may be unclear.
So currently when a cdxml module is imported, PowerShell reads the cdxml file and uses it to generate a PowerShell script module. My suggestion is that instead of generating a PowerShell script module, it generates a binary module. And instead of doing it at import time, it's done at compile time. A good way to do this would be via Roslyn source generators.
Dongbo Wang (@daxian-dbw) - IIRC, command discovery would generate PowerShell from CDXML and you don't always search CDXML modules, but when you do, it stands out in a profile.
One could modify command discovery, but that solution feels too targeted. CDXML modules can be slow to load and the generated code feels quite bloated. For example the Storage modules takes 2s to load on my machine and generates nearly 40,000 lines of PowerShell.
Reacted by Patrick Meinecke, Rob Holt and Rain Sallow (/u/ta11ow)
I see a simplest thing we can do is to add a cache for compiled CDXML modules. Since we already have a cache for the command discovery it does not add more security risk. (I'd work on this if MSFT team want and we could include this in 7.1.)
The next thing we could do is try to improve the CDXML module compiler.
Thanks Jason Shirk (@lzybkr), I absolutely agree there is space to improve the CDXML modules (loading and execution).
Ilya (@iSazonov) do you mean caching the generated powershell script for CDXML modules? It's different from the cache for command discovery. The latter only contains metadata information (which command name belongs to which module), but no code for execution. I don't recommend to do it -- it's not an urgent thing that needs to be mitigated with a workaround. If we want to invest in this area, then we should directly improve the ScriptWriter and works on the tool that produces assemblies from CDXML files.
I don't see a need to duplicate the effort there. If we're going to have such functionality it makes more sense to me that we segregate its code to a separate dll that a small console app can be built to interface with in a minimal fashion. That way, PowerShell can cache built CDXML modules until their current authors get around to shipping compiled assemblies for them, and authors wanting to write CDXML modules have the option of just shipping the compiled assembly and not requiring PowerShell to perform the caching.
Over time we could deprecate the caching method and eventually remove the CDXML caching entirely, requiring authors to precompile the modules.
If one was considering building something that might someday get integrated into the Windows build - I would start with a standalone tool that does not depend on PowerShell - probably with a simple exe wrapper around a dll that could be consumed by PowerShell.
Reacted by Dongbo Wang and Patrick Meinecke
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
Activity
iSazonov commentedon May 23, 2020
What do you mean? Performance? More fast startup?
SeeminglyScience commentedon May 23, 2020
Both (assuming by start up you mean import time, afaik no cdxml modules are imported at process init).
iSazonov commentedon May 24, 2020
What steps will be?
Import-Module cdxml
vexx32 commentedon May 24, 2020
I think what Patrick Meinecke (@SeeminglyScience) is suggesting is that rather than shipping the XML and parsing it into a module at import time, we could create a build task to generate a binary module so that we only need to ship the module DLL. That way, all of that processing is front-loaded and users just end up importing the module DLL directly.
SeeminglyScience commentedon May 24, 2020
Yeah, Roslyn source generators attach like analyzers. Except instead of emitting diagnostic records, they add additional source files to the
objpath.iSazonov commentedon May 25, 2020
I don't understand why it should be in the repo. If a developer want create a binary module the developer can use any tools.
SeeminglyScience commentedon May 25, 2020
Ilya (@iSazonov) Not sure what you mean.
This issue is about creating a Roslyn source generator that generates binary modules from cdxml files.
iSazonov commentedon May 25, 2020
You can use Roslyn source generator or another tool to create custom binary module. I ask why it should be in PowerShell repo?
SeeminglyScience commentedon May 25, 2020
I'm not sure why it wouldn't be, but sure it can go in a new repo I guess.
iSazonov commentedon May 25, 2020
From my understanding you say about new tool/module to create specific binary modules.
I would expect that you describe how this should work in a full-fledged RFC.
SeeminglyScience commentedon May 25, 2020
I would say that the suggestion is an alternative/addition to the current cmdletization API that would result in better performance and UX. It's like saying PowerShell is a compiler, it's not wrong but it's not the whole picture.
Yeah I think if this gets implemented it might be a good idea for the implementer to submit an RFC. If this is something you're interested in pursuing, I'd be happy to expand on any particular detail that may be unclear.
iSazonov commentedon May 25, 2020
This is too blurry said. If you have a clear idea, then share the exact specifications.
SeeminglyScience commentedon May 25, 2020
So currently when a cdxml module is imported, PowerShell reads the cdxml file and uses it to generate a PowerShell script module. My suggestion is that instead of generating a PowerShell script module, it generates a binary module. And instead of doing it at import time, it's done at compile time. A good way to do this would be via Roslyn source generators.
10 remaining items
lzybkr commentedon Jul 23, 2020
Dongbo Wang (@daxian-dbw) - IIRC, command discovery would generate PowerShell from CDXML and you don't always search CDXML modules, but when you do, it stands out in a profile.
One could modify command discovery, but that solution feels too targeted. CDXML modules can be slow to load and the generated code feels quite bloated. For example the
Storagemodules takes 2s to load on my machine and generates nearly 40,000 lines of PowerShell.iSazonov commentedon Jul 23, 2020
I see a simplest thing we can do is to add a cache for compiled CDXML modules. Since we already have a cache for the command discovery it does not add more security risk. (I'd work on this if MSFT team want and we could include this in 7.1.)
The next thing we could do is try to improve the CDXML module compiler.
I suggest to open new issue for these optimizations and close the issue because RSGs is more for external projects like https://github.com/PowerShell/PlasterBuild and https://github.com/PowerShell/generator-powershell.
daxian-dbw commentedon Jul 23, 2020
Thanks Jason Shirk (@lzybkr), I absolutely agree there is space to improve the CDXML modules (loading and execution).
Ilya (@iSazonov) do you mean caching the generated powershell script for CDXML modules? It's different from the cache for command discovery. The latter only contains metadata information (which command name belongs to which module), but no code for execution. I don't recommend to do it -- it's not an urgent thing that needs to be mitigated with a workaround. If we want to invest in this area, then we should directly improve the
ScriptWriterand works on the tool that produces assemblies from CDXML files.iSazonov commentedon Jul 23, 2020
No, I mean to generate CDXML script, compile it and cache the assembly. Then we could improve
ScriptWriter.daxian-dbw commentedon Jul 23, 2020
This already sounds like the tool that Patrick Meinecke (@SeeminglyScience) is asking for 😄
iSazonov commentedon Jul 23, 2020
A tool is an external thing. I suggest to do this on the fly and check the cache folder before compile CDXML in next time.
vexx32 commentedon Jul 23, 2020
I don't see a need to duplicate the effort there. If we're going to have such functionality it makes more sense to me that we segregate its code to a separate dll that a small console app can be built to interface with in a minimal fashion. That way, PowerShell can cache built CDXML modules until their current authors get around to shipping compiled assemblies for them, and authors wanting to write CDXML modules have the option of just shipping the compiled assembly and not requiring PowerShell to perform the caching.
Over time we could deprecate the caching method and eventually remove the CDXML caching entirely, requiring authors to precompile the modules.
lzybkr commentedon Jul 23, 2020
If one was considering building something that might someday get integrated into the Windows build - I would start with a standalone tool that does not depend on PowerShell - probably with a simple exe wrapper around a dll that could be consumed by PowerShell.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.