Skip to content

Commit 469545d

Browse files
authored
Performance optimizations (#172)
1 parent 23e38b5 commit 469545d

12 files changed

Lines changed: 132 additions & 104 deletions

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
for variable values, type comparisons, and internal diagnostics.
1515
- Comparison result still uses `Write-Verbose` to provide user-actionable
1616
information about parameter state differences.
17+
- Use ArrayList and not fixed size array.
18+
- Remove some ForEach-Object usage.
1719
- `Test-ModuleExist`
1820
- Changed module filtering messages from `Write-Verbose` to `Write-Debug`
1921
for internal implementation details.
@@ -23,11 +25,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2325
- Changed operating system SKU diagnostic message from `Write-Verbose` to `Write-Debug`.
2426
- `Find-Certificate`
2527
- Changed certificate filter diagnostic message from `Write-Verbose` to `Write-Debug`.
28+
- Use ArrayList and not fixed size array.
2629
- `Get-LocalizedDataForInvariantCulture`
2730
- Changed file processing message from `Write-Verbose` to `Write-Debug`
2831
for internal diagnostic information.
2932
- Localized hardcoded `Write-Debug` messages to use localized strings
3033
([#169](https://github.com/dsccommunity/DscResource.Common/issues/169)).
34+
- `Clear-ZeroedEnumPropertyValue`
35+
- Add begin, end blocks.
36+
- `Test-DscPropertyState`
37+
- Use ArrayList and not fixed size array.
38+
- `Assert-BoundParameter`
39+
- Fix PSSA warning.
40+
- `ConvertFrom-DscResourceInstance`
41+
- Fix PSSA warning.
42+
- `Get-DscProperty`
43+
- Use ArrayList and not fixed size array.
44+
- Fix PSSA warning.
45+
- `Remove-CommonParameter`
46+
- Remove use of `Where-Object` and `ForEach-Object`.
3147

3248
### Fixed
3349

source/Private/Clear-ZeroedEnumPropertyValue.ps1

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ function Clear-ZeroedEnumPropertyValue
2727
$InputObject
2828
)
2929

30-
process
30+
begin
3131
{
3232
$result = @{}
33+
}
3334

35+
process
36+
{
3437
foreach ($property in $InputObject.Keys)
3538
{
3639
$value = $InputObject.$property
@@ -41,7 +44,10 @@ function Clear-ZeroedEnumPropertyValue
4144

4245
$result.$property = $value
4346
}
47+
}
4448

49+
end
50+
{
4551
return $result
4652
}
4753
}

source/Private/Test-DscPropertyState.ps1

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,15 @@ function Test-DscPropertyState
4848
}
4949
elseif (
5050
$Values.DesiredValue -is [Microsoft.Management.Infrastructure.CimInstance[]] `
51-
-or $Values.DesiredValue -is [System.Array] -and $Values.DesiredValue[0] -is [Microsoft.Management.Infrastructure.CimInstance]
51+
-or $Values.DesiredValue -is [System.Array] -and $Values.DesiredValue[0] -is [Microsoft.Management.Infrastructure.CimInstance]
5252
)
5353
{
5454
if (-not $Values.ContainsKey('KeyProperties'))
5555
{
56-
$errorMessage = $script:localizedData.KeyPropertiesMissing
57-
58-
New-InvalidOperationException -Message $errorMessage
56+
New-InvalidOperationException -Message $script:localizedData.KeyPropertiesMissing
5957
}
6058

61-
$propertyState = @()
59+
$propertyState = [System.Collections.ArrayList]::new()
6260

6361
<#
6462
It is a collection of CIM instances, then recursively call
@@ -80,23 +78,20 @@ function Test-DscPropertyState
8078

8179
if ($currentCimInstance.Count -gt 1)
8280
{
83-
$errorMessage = $script:localizedData.TooManyCimInstances
84-
85-
New-InvalidOperationException -Message $errorMessage
81+
New-InvalidOperationException -Message $script:localizedData.TooManyCimInstances
8682
}
8783

8884
if ($currentCimInstance)
8985
{
90-
$keyCimInstanceProperties = $currentCimInstance.CimInstanceProperties |
91-
Where-Object -FilterScript {
86+
$keyCimInstanceProperties = $currentCimInstance.CimInstanceProperties.Where({
9287
$_.Name -in $Values.KeyProperties
93-
}
88+
})
9489

9590
<#
9691
For each key property build a string representation of the
9792
property name and its value.
9893
#>
99-
$keyPropertyValues = $keyCimInstanceProperties.ForEach({'{0}="{1}"' -f $_.Name, ($_.Value -join ',')})
94+
$keyPropertyValues = $keyCimInstanceProperties.ForEach({ '{0}="{1}"' -f $_.Name, ($_.Value -join ',') })
10095

10196
Write-Debug -Message (
10297
$script:localizedData.TestingCimInstance -f @(
@@ -107,16 +102,15 @@ function Test-DscPropertyState
107102
}
108103
else
109104
{
110-
$keyCimInstanceProperties = $desiredCimInstance.CimInstanceProperties |
111-
Where-Object -FilterScript {
105+
$keyCimInstanceProperties = $desiredCimInstance.CimInstanceProperties.Where({
112106
$_.Name -in $Values.KeyProperties
113-
}
107+
})
114108

115109
<#
116110
For each key property build a string representation of the
117111
property name and its value.
118112
#>
119-
$keyPropertyValues = $keyCimInstanceProperties.ForEach({'{0}="{1}"' -f $_.Name, ($_.Value -join ',')})
113+
$keyPropertyValues = $keyCimInstanceProperties.ForEach({ '{0}="{1}"' -f $_.Name, ($_.Value -join ',') })
120114

121115
Write-Debug -Message (
122116
$script:localizedData.MissingCimInstance -f @(
@@ -138,7 +132,7 @@ function Test-DscPropertyState
138132
}
139133
elseif ($Values.DesiredValue -is [Microsoft.Management.Infrastructure.CimInstance])
140134
{
141-
$propertyState = @()
135+
$propertyState = [System.Collections.ArrayList]::new()
142136

143137
<#
144138
It is a CIM instance, recursively call Test-DscPropertyState for each
@@ -186,21 +180,21 @@ function Test-DscPropertyState
186180
{
187181
Write-Debug -Message $script:localizedData.ArrayDoesNotMatch
188182

189-
$arrayCompare |
190-
ForEach-Object -Process {
191-
if ($_.SideIndicator -eq '=>')
192-
{
193-
Write-Debug -Message (
194-
$script:localizedData.ArrayValueIsAbsent -f $_.InputObject
195-
)
196-
}
197-
else
198-
{
199-
Write-Debug -Message (
200-
$script:localizedData.ArrayValueIsPresent -f $_.InputObject
201-
)
202-
}
183+
foreach ($item in $arrayCompare)
184+
{
185+
if ($item.SideIndicator -eq '=>')
186+
{
187+
Write-Debug -Message (
188+
$script:localizedData.ArrayValueIsAbsent -f $item.InputObject
189+
)
203190
}
191+
else
192+
{
193+
Write-Debug -Message (
194+
$script:localizedData.ArrayValueIsPresent -f $item.InputObject
195+
)
196+
}
197+
}
204198

205199
$returnValue = $false
206200
}
@@ -215,17 +209,7 @@ function Test-DscPropertyState
215209

216210
$returnValue = $false
217211

218-
$supportedTypes = @(
219-
'String'
220-
'Int32'
221-
'UInt32'
222-
'Int16'
223-
'UInt16'
224-
'Single'
225-
'Boolean'
226-
)
227-
228-
if ($desiredType.Name -notin $supportedTypes)
212+
if ($desiredType.Name -notin 'String', 'Int32', 'UInt32', 'Int16', 'UInt16', 'Single', 'Boolean')
229213
{
230214
Write-Warning -Message ($script:localizedData.UnableToCompareType -f $desiredType.Name)
231215
}

source/Public/Assert-BoundParameter.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
#>
154154
function Assert-BoundParameter
155155
{
156+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('UseSyntacticallyCorrectExamples', '', Justification = 'Because the rule does not yet support parsing the code when a parameter type is not available. The ScriptAnalyzer rule UseSyntacticallyCorrectExamples will always error in the editor due to https://github.com/indented-automation/Indented.ScriptAnalyzerRules/issues/8.')]
156157
[CmdletBinding()]
157158
param
158159
(

0 commit comments

Comments
 (0)