Skip to content

High cpu usage when parsing scripts that include [ordered] #17308

@Sjark

Description

@Sjark

Prerequisites

Steps to reproduce

We are parsing multiple powershell scripts in parallel using Parser.ParseInput but are having issues with perf when the scrips being parsed includes multiple [ordered] attributes/types.

The parser starts to use 100% cpu, and it takes a long time for the parsing to complete.

Repro here

The console app above has 2 identical powershell scripts included, the only difference is that on of them has a bunch of [ordered] before the hashtable assignments.

The one without [ordered] completes almost instantly, while the one with [ordered] takes some time and uses a lot of cpu.

Expected behavior

Parsing to complete in about the same time with both scripts.

Actual behavior

Parsing takes a lot longer and uses a lot more resources when the script includes [ordered].

Error details

The error seems to be related to LookForTypeInAssemblies method within the TypeResolver.cs:68

Every time it finds a [ordered] in the script it goes through all loaded assemblies to try and find the matching type, but since [ordered] is not actually a type (just a helper for creating System.Collections.Specialized.OrderedDictionary) the method returns null, and will search again the next time it finds an [ordered] attribute.

I think there should either be a cache for types that did not match any assemblies, so that it doesn't have to look through the same assemblies for the same type multiple times, or there should be a special case for [ordered] so that it does not even try to search through the assemblies for it.

Environment data

We are using nuget package: System.Management.Automation version 7.2.3

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-No ActivityIssue has had no activity for 6 months or moreWG-Engine-Performancecore PowerShell engine, interpreter, and runtime performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions