Skip to content

Slow function when parameter is Mandatory its Type is specified #5417

@Atamido

Description

@Atamido

Steps to reproduce

[Byte[]]$Value = [System.IO.File]::ReadAllBytes('C:\Some10MBfile')

#  ~1000ms for 10MB array
#  ~0.5ms for 1byte array
function MandType {
    Param (
        [parameter(Mandatory=$True)]
        [Byte[]]$Value
    )
}

#  ~0.3ms for 10MB array
function TypeOnly {
    Param (
        [parameter()]
        [Byte[]]$Value
    )
}

#  ~0.3 for 10MB array
function MandOnly {
    Param (
        [parameter(Mandatory=$True)]
        $Value
    )
}

(Measure-Command { MandType $Value}).TotalMilliseconds
(Measure-Command { MandType ([Byte[]]@(0))}).TotalMilliseconds
(Measure-Command { TypeOnly $Value}).TotalMilliseconds
(Measure-Command { MandOnly $Value}).TotalMilliseconds

Expected behavior

All versions of the above functions should behave identically and complete immediately, regardless of of parameter size as the functions perform no functions.

1.1
1.1
1.1
1.1

Actual behavior

When setting a parameter to Mandatory, specifying its Type, and passing a large array in, the function will pause for some amount of time before starting.

1000
1.1
1.1
1.1

Environment data

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-beta.9
PSEdition                      Core
GitCommitId                    v6.0.0-beta.9
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0


PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.16299.19
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.19
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Metadata

Metadata

Assignees

Labels

Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-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