Please feel free to provide feedback or file bugs here.

Bug: Slow function when setting a parameter to Mandatory and specifying its Type

There is a bug that causes a function to be called much more slowly than it should if an array parameter has both Mandatory and Type specified. I'm guessing something is causing the system to iterate through the array, so the larger the array, the longer to call the function.

With both Mandatory and Type specified, it takes a whole second to call a function with a 10MB byte array. If a 1byte array is used, it takes less than a millisecond to call the function. If only one, the other, or neither are specified, then it takes less than a millisecond to the function.

I'm on Windows 10 with PS version 5.1.15063.674, and have reproduced on 6.0.0-beta.9. Others have confirmed. Below is code to reproduce the scenarios. You will need to change the first line to point to a large enough file to see the difference.

[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}
Measure-Command { MandType ([Byte[]]@(0))}
Measure-Command { TypeOnly $Value}
Measure-Command { MandOnly $Value}

3 votes
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)

    We’ll send you updates on this idea

    Paul shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    0 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...

      Feedback and Knowledge Base