Skip to content

Fast path in merge() for single class default#161

Closed
SanderMuller wants to merge 1 commit intolivewire:mainfrom
SanderMuller:autoresearch/merge-class-fast-path
Closed

Fast path in merge() for single class default#161
SanderMuller wants to merge 1 commit intolivewire:mainfrom
SanderMuller:autoresearch/merge-class-fast-path

Conversation

@SanderMuller
Copy link
Copy Markdown
Contributor

The most common usage of merge() in Blade components is $attributes->merge(['class' => 'some classes']), a single string default for the class key. This adds an early return for that exact pattern, bypassing the general-purpose loop that separates appendable/non-appendable attributes, iterates defaults, and calls array_merge().

What changed

A fast path at the top of BlazeAttributeBag::merge() detects when:

  1. Exactly one default is passed
  2. The key is class
  3. The value is a string

When all three hold, it merges the class directly and returns, skipping the escape loop, the appendable/non-appendable separation, and the final array_merge().

Why this works

The general path iterates $this->attributes to classify each key as appendable or not, runs a second loop to resolve appendable values against defaults (with AppendableAttributeValue instanceof checks), and finishes with array_merge(). The fast path skips one e() call, one array copy, one string concatenation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@SanderMuller
Copy link
Copy Markdown
Contributor Author

/benchmark merge

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark Result: Merge

Attempt Blade Blaze Improvement
#1 233.96ms 13.95ms 94%
#2 238.00ms 14.08ms 94.1%
#3 236.28ms 13.91ms 94.1%
#4 237.18ms 14.51ms 93.9%
#5 233.56ms 14.24ms 93.9%
#6 237.98ms 13.94ms 94.1%
#7 238.56ms 14.18ms 94.1%
#8 231.87ms 14.16ms 93.9%
#9 235.71ms 13.91ms 94.1%
#10 230.51ms 14.40ms 93.8%
Snapshot 235.47ms 16.39ms 93%
Result 236.00ms (~) 14.12ms (-13.8%) 94% (+1%)

Median of 10 attempts, 5000 iterations x 10 rounds, 31.98s total

To run a specific benchmark, comment /benchmark <name> where name is one of: attributes, aware, class, default, forwarding, merge, named-slots, no-attributes, slot

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark Result: Default

Attempt Blade Blaze Improvement
#1 349.46ms 15.95ms 95.4%
#2 343.77ms 15.68ms 95.4%
#3 346.25ms 15.89ms 95.4%
#4 349.80ms 16.27ms 95.3%
#5 348.56ms 16.42ms 95.3%
#6 351.51ms 16.14ms 95.4%
#7 347.67ms 15.84ms 95.4%
#8 351.74ms 16.09ms 95.4%
#9 352.71ms 15.88ms 95.5%
#10 346.73ms 16.03ms 95.4%
Snapshot 345.45ms 15.95ms 95.4%
Result 349.01ms (~) 15.99ms (~) 95.4% (~)

Median of 10 attempts, 5000 iterations x 10 rounds, 45.99s total

To run a specific benchmark, comment /benchmark <name> where name is one of: attributes, aware, class, default, forwarding, merge, named-slots, no-attributes, slot

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