Skip to content

Index brace and pipeline lookups during formatting - #2224

Open
Przemysław Kłys (PrzemyslawKlys) wants to merge 1 commit into
PowerShell:mainfrom
PrzemyslawKlys:fix/formatter-token-index
Open

Przemysław Kłys (PrzemyslawKlys) wants to merge 1 commit into
PowerShell:mainfrom
PrzemyslawKlys:fix/formatter-token-index

Conversation

@PrzemyslawKlys

@PrzemyslawKlys Przemysław Kłys (PrzemyslawKlys) commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

PR Summary

Formatting large merged modules repeatedly scans the token array for script-block braces and scans earlier pipelines for each token. Index brace tokens by offset and pipeline endings by line, preserving the existing first-match selection and AST order. This changes no public API or formatting output.

On PowerForge-merged repository functions from Locksmith and PSWriteHTML, the formatted PSM1 and original manifest bytes match unchanged upstream (411c3d0) exactly. Mean warmed Invoke-Formatter time over three rotated samples:

Workload / L3 domain Upstream This change
Locksmith, 256 KB merged PSM1 / 0xFFFF 442 ms 242 ms
PSWriteHTML, 1.49 MB merged PSM1 / 0xFFFF 3,387 ms 1,138 ms
Locksmith / 0xFFFF0000 315 ms 214 ms
PSWriteHTML / 0xFFFF0000 2,035 ms 1,353 ms

These are local PowerShell 7.6.6 measurements on a Ryzen 9 9950X3D2. Comparisons are pinned to one L3 domain at a time: three rotated samples on 0xFFFF, two on 0xFFFF0000, retaining every sample. Each sample uses a fresh process; startup/import are excluded, and four workload passes are discarded before the warm measurement. Two small PSWriteHTML function files take about 10 ms together with either version. First-call timings do not show a consistent improvement. The merged workloads exclude optional dependency inlining and module execution and use the same six formatting rules. This optimization is independent of #2206's command-discovery changes.

Release builds passed for net8 and net462. The TokenOperations, InvokeFormatter, UseConsistentIndentation, PlaceOpenBrace and PlaceCloseBrace suites passed: 157 tests on PowerShell 7; 155 tests and two platform skips on Windows PowerShell 5.1. The new test checks brace selection and nested AST ordering.

PR Checklist

  • PR has a meaningful title
  • Summarized changes
  • Change is not breaking
  • Changed source and test files have the correct copyright header
  • Added a test for brace selection and ordering; existing indentation tests cover pipeline matching
  • This PR is ready for review and is not Work in Progress

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The optimizations preserve prior selection semantics and are supported by focused tests.

0 open findings

What changed in this PR

Optimizes formatter token and pipeline lookups while preserving existing output and ordering.

Changes:

  • Indexes brace tokens by offset.
  • Indexes pipeline endings by line.
  • Adds brace-selection and AST-order coverage.
File Description
Engine/​TokenOperations.cs Adds offset-based brace lookup.
Rules/​UseConsistentIndentation.cs Adds line-indexed pipeline-end lookup.
Tests/​Engine/​TokenOperations.tests.ps1 Tests brace filtering and ordering.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

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