Skip to content

Batch phpcs calls into a single invocation#110

Open
sirbrillig wants to merge 1 commit intotrunkfrom
batch-phpcs-invocations
Open

Batch phpcs calls into a single invocation#110
sirbrillig wants to merge 1 commit intotrunkfrom
batch-phpcs-invocations

Conversation

@sirbrillig
Copy link
Copy Markdown
Owner

@sirbrillig sirbrillig commented Apr 20, 2026

Fixes #115

Summary

  • Replaces the 2N per-file phpcs launches in runGitWorkflow and runSvnWorkflow with a single phpcs invocation that scans all file versions at once
  • All modified and unmodified file contents are written to a temp directory (/tmp/phpcs-changed-XXXX/new/… and /tmp/phpcs-changed-XXXX/old/…) and phpcs runs once across all of them, eliminating startup overhead that previously scaled linearly with the number of changed files
  • The per-file library functions runGitWorkflowForFile and runSvnWorkflowForFile are unchanged

This is a different approach to #114

Approach

Each workflow now has three phases:

  1. Pre-batch — check caches, determine which files are new (no unmodified version), collect the lists of files that need scanning
  2. Batch — single getPhpcsOutputForGitBatch / getPhpcsOutputForSvnBatch call writes all content to a temp dir and runs phpcs once; results are cached individually per file
  3. Filter — parse results and compute new messages per file as before

Trade-off

The batch approach always scans the unmodified version of uncached files even when the modified version has no messages. Previously the per-file path skipped the unmodified scan in that case. The trade-off is intentional: one saved phpcs startup cost (≥250 ms) outweighs the cost of a few extra file sniffs.

Performance

Measured with benchmark.sh on 10 staged PHP files (PSR2 standard, 10 runs):

Mean Relative
trunk: 20 phpcs calls 5.74 s ± 0.11 s 1.00×
batch (this branch): 1 phpcs call 2.44 s ± 0.05 s 2.36× faster

Changes

  • PhpcsChanged/ShellOperator.php — add getPhpcsOutputForGitBatch and getPhpcsOutputForSvnBatch to the interface
  • PhpcsChanged/UnixShell.php — implement both batch methods using a temp directory and a single phpcs invocation; private helpers writeTempFile, runBatchPhpcs, cleanupTempDir
  • tests/helpers/TestShell.php — override both batch methods to delegate to existing per-file mocks (shell redirections can't be simulated in tests)
  • PhpcsChanged/Cli.php — rewrite runGitWorkflow and runSvnWorkflow with the three-phase approach
  • tests/GitWorkflowTest.php / tests/SvnWorkflowTest.php — update tests affected by the always-scan-unmodified trade-off; add multi-file batch tests verifying new-file handling and cache hit behaviour

@sirbrillig sirbrillig force-pushed the batch-phpcs-invocations branch from a75e2c2 to 129b081 Compare April 20, 2026 23:26
Replace the 2N per-file phpcs launches in runGitWorkflow and runSvnWorkflow
with a single batch invocation. All modified and unmodified file contents are
written to a temp directory and phpcs is run once on all of them, eliminating
the startup overhead cost that scaled linearly with the number of changed files.

- Add getPhpcsOutputForGitBatch / getPhpcsOutputForSvnBatch to ShellOperator
- Implement batch methods in UnixShell using a temp dir layout (new/ and old/)
- Override batch methods in TestShell to delegate to existing per-file mocks
- Rewrite runGitWorkflow and runSvnWorkflow with pre-batch/batch/filter phases
- Add and update tests for the new batch behavior
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.

Improve performance by reducing the number of phpcs calls

1 participant