swc icon indicating copy to clipboard operation
swc copied to clipboard

Optimize `atom!` for inlinable strings and adopt it across swc

Open lukesandberg opened this issue 5 months ago • 3 comments

This ports an optimization from Turbopacks rcstr implementation to swc

Atom has an optimization to store small strings inline, this optimizes the atom! macro to produce a constant Atom struct when possible instead using a once_cell::Lazy. This shifts work from runtime to compile time and should be a small code size optimization as well.

Many of the literal Atoms constructed in swc are for things like keywords, the vast majority of which fit within the 7 byte limit. Even for the pesky ones that don't (looking at you function!), we still optimize construction by leveraging the once_cell.

At the same time replace ~all calls to "<string-literal>".into() that were constructing an Atom with a call to the atom! macro., which should improve the impact of the change.

lukesandberg avatar Jun 15 '25 02:06 lukesandberg

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Jun 15 '25 02:06 CLAassistant

🦋 Changeset detected

Latest commit: ed0b82c04df35d58a8309513ccca0eced96da912

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Jun 15 '25 02:06 changeset-bot[bot]

CodSpeed Performance Report

Merging #10612 will improve performances by 7.65%

Comparing lukesandberg:atom (ed0b82c) with main (01e5bd1)

Summary

⚡ 1 improvements
✅ 139 untouched benchmarks

Benchmarks breakdown

Benchmark BASE HEAD Change
es2020_nullish_coalescing 307.9 µs 286 µs +7.65%

codspeed-hq[bot] avatar Jun 15 '25 05:06 codspeed-hq[bot]