Skip to content

Optimize hot-path array accumulation#275

Open
SamErde wants to merge 4 commits intomainfrom
samerde/optimize-array-accumulation
Open

Optimize hot-path array accumulation#275
SamErde wants to merge 4 commits intomainfrom
samerde/optimize-array-accumulation

Conversation

@SamErde
Copy link
Copy Markdown
Collaborator

@SamErde SamErde commented May 6, 2026

Summary

Eliminates an unnecessary intermediate array allocation in the ESC3 scan path of Invoke-Scans.ps1.

Problem

$ESC3 was built with a two-step pattern:

[array]$ESC3 = Find-ESC3C1 ...   # allocates array A
[array]$ESC3 += Find-ESC3C2 ...  # allocates array B, then copies A+B into a new array C

The += on a typed [array] forces PowerShell to allocate a new array and copy all existing elements from both sides. When ESC3C1/ESC3C2 return large result sets (common in large AD environments), this creates an avoidable O(n) copy.

Fix

Combine both Find-ESC3C* calls into a single array-concatenation expression, producing the combined array in one allocation:

[array]$ESC3 = @(Find-ESC3C1 ...) + @(Find-ESC3C2 ...)

This change is applied in both the ESC3 case and the All case of the switch block.

Scope

  • Changed: Private/Invoke-Scans.ps1 only — two symmetric edits (ESC3 case + All case).
  • Not changed: All other += usages are low-volume (bounded small arrays), string/integer arithmetic, or one-shot operations — left as-is per the task scope.
  • Not changed: Root Invoke-Locksmith.ps1 — this is a build artifact generated by Build-Module.ps1; source changes will be incorporated on next build.
  • No behavior change: $ESC3 contains the same elements in the same order (ESC3C1 results followed by ESC3C2 results).

Validation

  • PSScriptAnalyzer run on changed file — no new errors or warnings introduced.

Replace two-step [array]\ = ...; [array]\ += ... pattern with a
single array concatenation expression in both the ESC3 case and the All case
of Invoke-Scans. This eliminates the unnecessary intermediate array allocation
and copy that occurs when += is used to append a second scan result array to
the first.

No behavior change: the resulting \ array contains exactly the same
elements in the same order (ESC3C1 results followed by ESC3C2 results).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@SamErde SamErde marked this pull request as ready for review May 6, 2026 09:24
Copilot AI review requested due to automatic review settings May 6, 2026 09:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes (intended) the ESC3 scan result accumulation in Invoke-Scans.ps1 by replacing a two-step array build (= then +=) with a single expression, targeting reduced overhead in large AD environments.

Changes:

  • Updated the ESC3 switch case to build $ESC3 using a single concatenation expression.
  • Updated the All switch case to build $ESC3 using the same pattern for consistency.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Private/Invoke-Scans.ps1 Outdated
Comment thread Private/Invoke-Scans.ps1 Outdated
@SamErde SamErde marked this pull request as draft May 6, 2026 09:27
SamErde and others added 3 commits May 6, 2026 05:34
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace @(A) + @(B) with @(A; B) multi-statement form to avoid
creating 3 intermediate arrays. The @() + @() pattern materializes
both arrays separately and then allocates a third via the + operator.
The single-subexpression form @(stmt1; stmt2) collects all pipeline
output in one pass using PowerShell's internal List-based collector,
reducing from 3 allocations to 1.

Addresses GitHub Copilot code review on PR #275.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@SamErde SamErde requested a review from Copilot May 6, 2026 09:43
@SamErde SamErde marked this pull request as ready for review May 6, 2026 09:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants