Skip to content

Rebuild the framework caches in the background after an update - #190

Merged
fgilio merged 4 commits into
mainfrom
perf/background-optimize
Sep 6, 2026
Merged

fgilio merged 4 commits into
mainfrom
perf/background-optimize

Conversation

@fgilio

@fgilio fgilio commented Sep 6, 2026

Copy link
Copy Markdown
Owner

Why

Item 1 of the ranked list in #183. After a version change NativePHP ran php artisan optimize synchronously before spawning the PHP server, which held the post-update launch for about 1.7 s while every Blade view recompiled and the config, route, and event caches were rebuilt. The same-version launch was already unaffected (the version gate from #182); this PR takes the cost off the post-update launch too.

What

  • rfa:optimize (new artisan command, CompileViewsAction): runs optimize --except=views, then compiles every Blade template in place. Plain optimize empties the compiled-views directory through view:cache first, which a request served in the meantime can trip over. Blade writes each compiled file through an atomic rename and skips unchanged output, so compiling without clearing is safe next to a serving process.
  • server-optimize patch: behind the same gate as before, the rebuild now runs as a background child while the server spawns and serves from source.
    • Laravel writes config.php, routes-v7.php, and events.php with a plain file_put_contents and the server requires them on every request, so the child gets those three paths pointed at a staging directory. They are renamed into place, one atomic rename each, after a clean exit.
    • After a version change the previous version's cache files and the compiled views are removed before the server spawns, so new code never boots against old caches. Blade and Livewire keep a compiled file while it is newer than its source, and a build's sources predate anything the previous version compiled, so a compiled view left in place would have been served as fresh.
    • The child is killed with the app, and optimized_version is stamped only once the caches are in place. A launch that quits early redoes the rebuild next time; the staging directory is wiped on entry.
  • Launch timeline: the optimize marks move into the background runner, the flush waits for the finish mark until the 60 s deadline, and the report gains a phase for the background rebuild.

Measured on this branch (local build, forced post-update state)

Launch Presented (ms) Background rebuild
Warm 1515 not run
Warm 1362 not run
Post-update 1394 431 to 1750
Post-update 1620 441 to 1952

Post-update launches now present in the same band as warm ones. Before, the same state presented at about 3.1 s on a warm binary (1.7 s of blocking optimize on top of the 1.4 s warm launch). After each post-update launch the three cache files were in place, the staging directory gone, the version stamped, and the log free of errors.

Related

🤖 Generated with Claude Code

https://claude.ai/code/session_01RqwYa86be82YSKkJYb9VLz

fgilio and others added 4 commits September 6, 2026 14:10
`optimize` empties the compiled-views directory through view:cache before
recompiling, which a request served in the meantime can trip over. The
new command runs `optimize --except=views` and then compiles every Blade
template in place through CompileViewsAction: Blade writes each compiled
file with an atomic rename and skips unchanged output, so the live
directory is never cleared.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RqwYa86be82YSKkJYb9VLz
After a version change the optimize used to run synchronously before the
PHP server spawned and held the launch for about 1.7 s. The server now
starts right away and rfa:optimize runs as a background child with the
config, route, and event cache paths pointed at a staging directory. The
three files are renamed into place once the child exits cleanly, so no
request ever requires a torn cache file. Caches left by the previous
version are removed before the server spawns, the child is killed with
the app, and the optimized version is stamped only after the rename.

The previous synchronous block is upgraded in place, and the launch
timeline marks for the optimize move into the background runner.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RqwYa86be82YSKkJYb9VLz
The optimize finish mark now lands after the window presents, so the
flush holds for it until the 60 s deadline. The report gains a phase for
the background rebuild.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RqwYa86be82YSKkJYb9VLz
Blade and Livewire keep a compiled file while it is newer than its
source, and a build's sources predate whatever the previous version
compiled. rfa:optimize never clears the live directory, so a stale view
survived an update until something recompiled it. The main process now
empties storage/framework/views before the server spawns whenever the
app version changed.

The in-place upgrade from the synchronous optimize block goes with it:
the runner now patches from a stored stock copy.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RqwYa86be82YSKkJYb9VLz
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@coderabbitai

coderabbitai Bot commented Sep 6, 2026

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 50ba68cd-324d-46f5-8d5f-1afd34ca3cc3

📥 Commits

Reviewing files that changed from the base of the PR and between 4764ae4 and a48bd85.

📒 Files selected for processing (10)
  • .claude/skills/rfa-debug/SKILL.md
  • app/Actions/CompileViewsAction.php
  • app/Console/Commands/OptimizeCommand.php
  • app/Services/LaunchTimelineService.php
  • scripts/patch-nativephp.php
  • tests/Feature/Console/OptimizeCommandTest.php
  • tests/Helpers/native-php-dist-fixtures.php
  • tests/Unit/Actions/CompileViewsActionTest.php
  • tests/Unit/Scripts/PatchNativePhpTest.php
  • tests/Unit/Services/LaunchTimelineServiceTest.php

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@fgilio
fgilio merged commit 83a0854 into main Sep 6, 2026
42 of 43 checks passed
@fgilio
fgilio deleted the perf/background-optimize branch September 6, 2026 13:32
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.

1 participant