Skip to content

perf: optimize hot-path form rendering - #71

Merged
mlantz merged 2 commits into
mainfrom
develop
Jun 23, 2026
Merged

perf: optimize hot-path form rendering#71
mlantz merged 2 commits into
mainfrom
develop

Conversation

@mlantz

@mlantz mlantz commented Jun 23, 2026

Copy link
Copy Markdown
Member

What

Performance optimizations on the hot per-field / per-attribute form rendering path. All changes are output-preserving internal refactors — no public API change and no behavior change.

1. AttributeBuilder (the main win)

  • Replaced the 168-element array literal that was rebuilt on every attribute with a private const VALID_HTML_ATTRIBUTES keyed map, swapping the O(n) in_array() scan for an O(1) isset() lookup.
  • Replaced Str::of($key)->startsWith(...) with native str_starts_with() (no Stringable allocation per attribute) and removed the now-unused Str import.
  • Replaced collect($html)->unique()->implode(' ') with implode(' ', array_unique($html)).
  • Kept the public validHtmlAttributes() method for backward compatibility — it now derives from the const.

2. FormAssets

  • core.js is now read from disk once and held in a static property, instead of on every compileScripts() call.

3. FormMaker

  • Added a readJavaScriptFile() helper backed by a static cache so default.js and validation.js are read once per request. Per-call config string replacements are unchanged, so output is identical even when config differs between forms.

Why

These run once per attribute / once per field / once per form render. On a typical multi-field form the AttributeBuilder change alone removes thousands of array allocations and linear scans per request; the asset changes remove redundant disk I/O when multiple forms render on a page.

Reviewer notes

  • Output is byte-for-byte identical; covered by the existing suite.
  • Verification: php -l clean on all three files, full suite green (174 tests, 354 assertions), pint passes.

🤖 Generated with Claude Code

mlantz and others added 2 commits June 22, 2026 22:25
Reduce per-field/per-attribute work on the form rendering path:

- AttributeBuilder: replace the 168-element array rebuilt on every
  attribute with a keyed const map, swapping the O(n) in_array scan
  for an O(1) isset lookup. Use native str_starts_with instead of
  Str::of()->startsWith (no Stringable allocation per attribute) and
  array_unique/implode instead of a Collection. validHtmlAttributes()
  is kept for backward compatibility, now derived from the const.
- FormAssets: cache the core.js read in a static property so it is
  read from disk once per request instead of on every compileScripts().
- FormMaker: cache the raw default.js/validation.js reads via a static
  cache; per-call config replacements are unchanged so output is identical.

All output-preserving internal refactors. No public API change.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@mlantz
mlantz merged commit 1bc06d1 into main Jun 23, 2026
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