Skip to content

Commit 28c626b

Browse files
author
Andrew
authored
Merge pull request #568 from anmenaga/lktable_prf
Perf optimization in adapter lookup table
2 parents df2d3ab + 4a6054f commit 28c626b

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,25 @@ Describe 'tests for resource discovery' {
144144
$TestClassResourcePath = Resolve-Path "$PSScriptRoot/../../powershell-adapter/Tests"
145145
$env:DSC_RESOURCE_PATH = $null
146146
$env:PSModulePath += [System.IO.Path]::PathSeparator + $TestClassResourcePath
147-
dsc resource list -a Microsoft.DSC/PowerShell | Out-Null
148-
"{'Name':'TestClassResource1'}" | dsc -l trace resource get -r 'TestClassResource/TestClassResource' 2> $TestDrive/tracing.txt
149147

148+
# remove adapter lookup table file
149+
Remove-Item -Force -Path $script:lookupTableFilePath -ErrorAction Stop
150+
Test-Path $script:lookupTableFilePath -PathType Leaf | Should -BeFalse
151+
152+
# initial invocation should populate and save adapter lookup table
153+
dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
154+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly "Read 0 items into lookup table"
155+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly "Saving lookup table"
156+
157+
# second invocation (without an update) should use but not save adapter lookup table
158+
"{'Name':'TestClassResource1'}" | dsc -l trace resource get -r 'TestClassResource/TestClassResource' 2> $TestDrive/tracing.txt
150159
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly "Lookup table found resource 'testclassresource/testclassresource' in adapter 'Microsoft.DSC/PowerShell'"
160+
"$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly "Saving lookup table"
161+
162+
# third invocation (with an update) should save updated adapter lookup table
163+
dsc -l trace resource list -a Test/TestGroup 2> $TestDrive/tracing.txt
164+
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly "Saving lookup table"
165+
151166
$env:PSModulePath = $oldPSModulePath
152167
}
153168

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,15 +536,22 @@ fn add_resources_to_lookup_table(adapted_resources: &BTreeMap<String, Vec<DscRes
536536
{
537537
let mut lookup_table = load_adapted_resources_lookup_table();
538538

539+
let mut lookup_table_changed = false;
539540
for (resource_name, res_vec) in adapted_resources {
540541
if let Some(adapter_name) = &res_vec[0].require_adapter {
541-
lookup_table.insert(resource_name.to_string().to_lowercase(), adapter_name.to_string());
542+
let new_value = adapter_name.to_string();
543+
let oldvalue = lookup_table.insert(resource_name.to_string().to_lowercase(), new_value.clone());
544+
if !lookup_table_changed && (oldvalue.is_none() || oldvalue.is_some_and(|val| val != new_value)) {
545+
lookup_table_changed = true;
546+
};
542547
} else {
543548
info!("Resource '{resource_name}' in 'adapted_resources' is missing 'require_adapter' field.");
544549
}
545550
};
546551

547-
save_adapted_resources_lookup_table(&lookup_table);
552+
if lookup_table_changed {
553+
save_adapted_resources_lookup_table(&lookup_table);
554+
}
548555
}
549556

550557
fn save_adapted_resources_lookup_table(lookup_table: &HashMap<String, String>)

0 commit comments

Comments
 (0)