← Back to Pull requests

Parse declarations with 2.3x fewer allocations (−32% per email through premailer) #194

Closed
unknown closed this pull request without merging on Oct 29, 2026 05:31 UTC
View on GitHub
unknown unknown commented on Oct 26, 2026 07:14 UTC
Author

Following up on #193 :) Profiling premailer on our emails again, most of what css_parser still allocated came from storing and printing declarations. Every Value copied its string through sub even with nothing to clean. reduce and each_with_object built a [key, value] pair per declaration, casecmp? case-folded both strings, and replace_declaration! rebuilt its hash through zip.

This branch copies a value only when it really has a trailing semicolon, an !important or surrounding whitespace to remove, and freezes the cleaned copy with -value, so repeated values (0, #fff, bold…) share one String. Hashes are iterated with plain each. casecmp(IMPORTANT).zero? replaces casecmp?: I checked every codepoint, none case-folds into !important. The strips in CssParser.merge are gone too, stored values are already stripped.

One trade-off to flag: when the value passed in is frozen and needs no cleaning, Value#value is now that String or its interned twin rather than a fresh copy. Unfrozen strings are still copied, and never frozen or mutated, whether they need cleaning or not. There's a test for both cases.

Against today's master, which includes #193 and #195, a parsed declaration goes from 10.4 objects to 4.4 and declarations_to_s from 15 to 3. Measured end to end, median of 11 interleaved runs, Ruby 4.0.9 on an Apple M5, premailer 1.32.0, nokogiri 1.19.4:

| | objects | bytes | CPU |
|---|---|---|---|
| our 167 transactional emails through premailer, per email | 13,948 → 9,448 (−32%) | 871 → 693 KiB (−20%) | ≈ |
| Mastodon's mailer CSS, parsed then merged | 42,088 → 24,586 (−42%) | 2,371 → 1,679 KiB (−29%) | −7.6% |
| premailer's own test files | 26,682 → 18,460 (−31%) | 1,733 → 1,424 KiB (−18%) | −3% |
| rake benchmark, complex.css | 8,068 → 6,365 (−21%) | 517 → 427 KiB (−17%) | ≈ |

All three corpora give byte-for-byte identical output. A differential fuzz against today's master (50,000 seeded random values, blocks, merges and shorthand round-trips, about 300,000 output lines) found no difference. The new allocation test fails on master, and is skipped on JRuby. The suite passes on Ruby 3.3 and 4.0, and RuboCop is clean.

Sister PR: #195, for shorthands and media types, now merged into this branch.

Pre-Merge Checklist


  • CHANGELOG.md updated with short summary
  • ---

    🤖 Generated with Claude Code, but prompted, read, and verified by @czj, a caring human :)

    Diff: 305 lines (+228 / -77) across 3 files Reviewer: grosser Review duration: 4216m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 305 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.342
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #194"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 40.2% (MEDIUM) · Higher = larger blast radius
    40.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 15.9% · Safe to merge
    15.9%