Skip to content

Build splatted attributes in a single output buffer - #983

Open
OskarEichler wants to merge 1 commit into
slim-template:mainfrom
OskarEichler:codex/splat-attribute-buffer
Open

OskarEichler wants to merge 1 commit into
slim-template:mainfrom
OskarEichler:codex/splat-attribute-buffer

Conversation

@OskarEichler

Copy link
Copy Markdown

Summary

Build splatted HTML attributes in one output buffer instead of allocating a result string for every attribute and then joining an array. Preserve attribute ordering, quoting and boolean handling.

Measurements

Ruby 4.0.6 with explicit YJIT; three alternating original/fixed pairs, five batches of 30,000 build_attrs calls per case (median over 15 batches):

Case Original time / allocations Patched time / allocations
8 sorted attributes (default) 0.067717 s / 660,000 0.053864 s / 390,000
32 sorted attributes 0.245998 s / 2,100,000 0.194801 s / 1,110,000
8 unsorted attributes 0.044375 s / 330,000 0.022923 s / 30,000
32 unsorted attributes 0.149172 s / 1,050,000 0.081655 s / 30,000

The benchmark prepopulates a Builder with string keys and a mix of true/string values, warms it, then measures only repeated build_attrs calls using monotonic time and total_allocated_objects. Empty and single-attribute cases also improved. These are local microbenchmarks, not application throughput claims.

Verification

  • Unmodified core/plugin suites: 415 runs / 650 assertions passing on Ruby 3.2.11 and 4.0.6; existing Rails suite 15 runs / 36 assertions passing on each.
  • 4,800 focused comparisons per Ruby against the original algorithm, with sorted/unsorted output, XHTML/HTML, both quote styles and scalar/Unicode values; repeated calls preserve state/output.
  • Syntax passes; no new comparative Lint offenses. No test files changed.

Compatibility / limitations

No intended output/API break or dependency change. Ordering and quoting options are retained. Performance varies by Ruby/workload; JRuby and the full supported-Ruby matrix have not been benchmarked.

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