Skip to content

Conversation

@Snider
Copy link
Contributor

@Snider Snider commented Feb 2, 2026

Performance Optimization: Config Version Pruning

πŸ’‘ What

Optimized ConfigVersioning::pruneOldVersions to replace the inefficient N+1 deletion pattern with a direct database query.

🎯 Why

The previous implementation loaded all configuration versions into memory and then iterated through them to delete old ones individually. This caused:

  • High Memory Usage: Loading thousands of Eloquent models.
  • N+1 Queries: Executing one DELETE query per old version.
  • Slow Execution: Linear degradation as the number of versions grew.

πŸ”§ Implementation Details

  • Cutoff Strategy: Instead of fetching all records, we now fetch only the Nth newest record (the "cutoff").
  • Single Delete Query: We issue a single DELETE statement for all records strictly older than the cutoff version (using created_at and id for deterministic comparison).
  • Race Condition Safety: Unlike a whereNotIn approach, the cutoff strategy is safe against concurrent insertions of new versions, as they will be newer than the cutoff and thus preserved.

πŸ“Š Measured Improvement

Benchmark test (ConfigVersioningPerformanceTest) shows:

  • Execution Time: ~6ms (in-memory SQLite) for 500 records.
  • Query Count: Reduced to 2 queries (1 SELECT for cutoff, 1 DELETE) regardless of the number of records to prune.
  • Correctness: Verified that exactly maxVersions (50) are kept.

PR created automatically by Jules for task 6708350155386618536 started by @Snider

Replaced the N+1 deletion loop (fetching all versions, slicing in memory, and deleting individually) with a single efficient DELETE query using a cutoff strategy.

This change:
- Reduces memory usage by only fetching the Nth newest record (cutoff).
- Reduces database roundtrips from O(N) to O(1) (1 select + 1 delete).
- Prevents race conditions by using a deterministic cutoff (created_at + id) rather than a `whereNotIn` exclusion list.
- Includes a regression/benchmark test verifying correctness and performance (~6ms execution).
@google-labs-jules
Copy link

πŸ‘‹ Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a πŸ‘€ emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@github-actions
Copy link

github-actions bot commented Feb 2, 2026

QA Pipeline Results

Check Status Details
Tests βœ“ 207 tests, 410 assertions
PHPStan βœ“ 0 errors
Psalm βœ— 1 issues
Code Style βœ“ 0 files need formatting
Security βœ“ 0 vulnerabilities
Artifacts
  • test-results.xml - JUnit test results
  • phpstan.json - PHPStan analysis
  • psalm.json / psalm.sarif - Psalm analysis
  • pint.json - Code style report
  • audit.json - Security audit

Generated by core php qa pipeline

- Removed `2>&1` stderr redirection from Psalm, PHPStan, Pint, and Composer Audit commands. This prevents progress messages and warnings (which are written to stderr) from corrupting the JSON/SARIF output files (which are captured via stdout redirection).
- Upgraded `github/codeql-action/upload-sarif` from v3 to v4 to resolve deprecation warnings and improve handling of SARIF files (including empty location fixes).
- This resolves the "Invalid SARIF. JSON syntax error" failure in the QA pipeline.
Psalm occasionally generates SARIF reports with location coordinates of 0 (e.g., `startLine: 0`), which violates the SARIF spec (minimum 1) and causes the `github/codeql-action/upload-sarif` action to fail.

This change adds a `jq` post-processing step to the QA workflow that recursively sanitizes the SARIF file, ensuring all region coordinates (lines and columns) are at least 1.
@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

The previous sanitization logic assumed a specific parent structure (`region` object). This updated logic recursively traverses the entire JSON structure and targets any key matching `startLine`, `startColumn`, `endLine`, or `endColumn`, forcing their values to be at least 1. This ensures full compliance with the SARIF schema regardless of where these coordinates appear.
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