Skip output buffering for pure text slots#163
Skip output buffering for pure text slots#163SanderMuller wants to merge 1 commit intolivewire:mainfrom
Conversation
Benchmark Result: Slot
Median of 10 attempts (* = outlier, excluded from result), 5000 iterations x 10 rounds, 20.75s total To run a specific benchmark, comment |
Benchmark Result: Default
Median of 10 attempts, 5000 iterations x 10 rounds, 46.04s total To run a specific benchmark, comment |
Benchmark Result: Named Slots
Median of 10 attempts (* = outlier, excluded from result), 5000 iterations x 10 rounds, 31.57s total To run a specific benchmark, comment |
|
What if there are Blade directives / components in the slot? |
868a3f2 to
2d37ca9
Compare
When a slot contains only plain text (no PHP blocks, Blade echo syntax, or directives), use a literal string instead of ob_start/ob_get_clean + trim. Eliminates three function calls per text slot per render. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2d37ca9 to
37e0e2d
Compare
|
/benchmark slot |
|
/benchmark named-slots |
You were right. I've added 2 tests that failed and now pass. Not the happiest about the several |
| { | ||
| // Fast path: if content is plain text (no PHP, Blade syntax, or directives), | ||
| // use a literal string instead of output buffering. | ||
| if (! $this->manager->isFolding() && ! str_contains($content, '<?') && ! str_contains($content, '{{') && ! str_contains($content, '{!!') && ! str_contains($content, '@')) { |
There was a problem hiding this comment.
Maybe extract as a function for readability? Like
Edited
protected function containsAll(string $haystack, array $needles): bool
{
return array_reduce($needles, fn($a, $n) => $a && str_contains($haystack, $n), true);
}
protected function containsAny(string $haystack, array $needles): bool
{
return array_reduce($needles, fn($a, $n) => $a || str_contains($haystack, $n), false);
}Reference: https://www.php.net/manual/en/function.str-contains.php#128796
|
We do these types of checks in multiple places in the codebase, maybe there's a way to extract that globally somehow? But overall I'm not super confident by this. I see we're escaping While the gains are tempting I'm not sure if this is the best way to achieve this. We're planning to integrate Forte for full static analysis which would allow us to check if the content is truly static. I think we hold off until then. Thoughts? |
It currently does handle the escaped escapes properly. Email does indeed break the optimization, but I don't think that's that bad. We could however delay it until Forte integration, that could be a more solid implementation. It would also slow down compilation speed, significant for cold boots. Any thought about hooking into |
When a slot contains only plain text (no PHP blocks), use a literal string instead of
ob_start/ob_get_clean+trim. Eliminates three function calls per text slot per render.