Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use faster object syntax for postcss-mixins #11765

Merged
merged 1 commit into from
Mar 20, 2024
Merged

Conversation

jesstelford
Copy link
Contributor

@jesstelford jesstelford commented Mar 20, 2024

There was a performance regression introduced by our non-sass mixins:

Some debugging the performance flamegraph from 0x shows it's the usage of postcss within those mixins trying to generate source maps that's taking a significant amount of time:

Screenshot 2024-03-20 at 17 00 35

It was possible to convert the mixins to the simpler object syntax which doesn't suffer from this issue.

Performance

tl;dr: Building @shopify/polaris is now 4.4s faster 🎉

I ran perf benchmarks using this hyperfine command:

hyperfine --parameter-list branch main,fix-postcss-mixins-perf --setup='git checkout {branch} && . ~/.nvm/nvm.sh && nvm use && yarn --prefer-offline && yarn build --filter="@shopify/polaris"' --prepare="rm -rf polaris-react/build" --runs 5 --warmup 1 '. ~/.nvm/nvm.sh && nvm use && yarn workspace @shopify/polaris build:js'

Results

Benchmark 1: (branch = main)
  Time (mean ± σ):     15.852 s ±  0.110 s    [User: 19.668 s, System: 4.021 s]
  Range (min … max):   15.743 s … 16.030 s    5 runs

Benchmark 2: (branch = fix-postcss-mixins-perf)
  Time (mean ± σ):     11.419 s ±  0.115 s    [User: 15.215 s, System: 2.943 s]
  Range (min … max):   11.285 s … 11.592 s    5 runs

Summary
  (branch = fix-postcss-mixins-perf) ran 1.39 ± 0.02 times faster than (branch = main)

Validation

I confirmed the generated .css file is identical with this script:

# go.sh
if [ -z "$1" ]; then echo "A filename is required:\n> $0 ./no-sass.css"; exit 1; fi;

# Write a temporary postcss config for cssnano to work
cat <<EOF > postcss.config.js
module.exports = {
  plugins: [
    require('cssnano')({
      preset: 'lite',
    }),
  ],
};
EOF

yarn workspace @shopify/polaris add cssnano-preset-lite
yarn
yarn build --filter="@shopify/polaris"

cp polaris-react/build/esm/styles.css "$1" # Move out of ignored directories
yarn prettier -w "$1" # Pre-format to normalize whitespace
npx postcss-cli --yes postcss-cli -r --no-map --config="`pwd`" "$1" # cssnano removes more whitespace prettier doesn't
yarn prettier -w "$1" # Format again to get back into un-minified version
rm postcss.config.js # Clean up temporary files
yarn workspace @shopify/polaris remove cssnano-preset-lite

run like this:

git checkout main
./go.sh old.css
git checkout fix-postcss-mixins-perf
./go.sh new.css
git diff --histogram --no-index old.css new.css

And the result was: no change 🎉

Copy link
Contributor

@gwyneplaine gwyneplaine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heck yeah lgtm, nice one!

@jesstelford jesstelford merged commit 42c298e into main Mar 20, 2024
10 checks passed
@jesstelford jesstelford deleted the fix-postcss-mixins-perf branch March 20, 2024 12:34
kyledurand pushed a commit that referenced this pull request Mar 21, 2024
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @shopify/polaris@12.22.0

### Minor Changes

- [#11677](#11677)
[`f6ba2b2a8`](f6ba2b2)
Thanks [@jesstelford](https://github.com/jesstelford)! - Migrated
@shopify/polaris from SASS to CSS using postcss plugins


- [#11723](#11723)
[`4699bb229`](4699bb2)
Thanks [@mrcthms](https://github.com/mrcthms)! - Updated `BulkActions`
to only show actions when selectMode is `true`


- [#11727](#11727)
[`c3ba6ae1b`](c3ba6ae)
Thanks [@sam-b-rose](https://github.com/sam-b-rose)! - Removed the
responsive logic that disabled the Card bevel on mobile. Removing this
until we are ready to rollout bevel changes across all components.

### Patch Changes

- [#11757](#11757)
[`e0ae9565c`](e0ae956)
Thanks [@sophschneider](https://github.com/sophschneider)! - Added
dynamicTopBarAndReframe feature flag type


- [#11733](#11733)
[`9c24a465c`](9c24a46)
Thanks [@jesstelford](https://github.com/jesstelford)! - Convert
SASS-style inline comments to CSS-style multiline comments.


- [#11724](#11724)
[`1543246b7`](1543246)
Thanks [@sam-b-rose](https://github.com/sam-b-rose)! - Updated
responsive styles for `Text` component


- [#11765](#11765)
[`42c298ea7`](42c298e)
Thanks [@jesstelford](https://github.com/jesstelford)! - Fix build
performance regression from using postcss-mixins.


- [#11725](#11725)
[`3e011e3b6`](3e011e3)
Thanks [@kyledurand](https://github.com/kyledurand)! - Fixed a bug where
iOS 16 font patch wasn't added for mobile app web views


- [#11763](#11763)
[`e7ab4a8f5`](e7ab4a8)
Thanks [@sydturn](https://github.com/sydturn)! - Fixed `IndexTable.Row`
`onClick` not being called when `selectable` is `false`


- [#11745](#11745)
[`831a683a2`](831a683)
Thanks [@kyledurand](https://github.com/kyledurand)! - Fixed bug in
math.ts for popover with position cover


- [#11735](#11735)
[`6d8ef8c99`](6d8ef8c)
Thanks [@sam-b-rose](https://github.com/sam-b-rose)! - Used `Text`
component to apply text styles for `Button`


- [#11592](#11592)
[`ad6315845`](ad63158)
Thanks [@SMAKSS](https://github.com/SMAKSS)! - Passed missing `id` prop
to the root element of `BlockStack`

## @shopify/stylelint-polaris@15.4.0

### Minor Changes

- [#11677](#11677)
[`f6ba2b2a8`](f6ba2b2)
Thanks [@jesstelford](https://github.com/jesstelford)! - Add `--pg-` as
a disallowed CSS Custom Property prefix (these are "global" Custom
Properties used within @shopify/polaris-react).

## @shopify/polaris-migrator@0.28.2

### Patch Changes

- [#11754](#11754)
[`f57db81df`](f57db81)
Thanks [@jesstelford](https://github.com/jesstelford)! - Move migrations
to v14 since the node v20 requirement will be the only change in v13

- Updated dependencies
\[[`f6ba2b2a8`](f6ba2b2)]:
    -   @shopify/stylelint-polaris@15.4.0

## polaris.shopify.com@0.66.0

### Minor Changes

- [#11720](#11720)
[`97184dc80`](97184dc)
Thanks [@sarahill](https://github.com/sarahill)! - Added common action
guidance.


- [#11690](#11690)
[`c78f125c7`](c78f125)
Thanks [@heyjoethomas](https://github.com/heyjoethomas)! - Updates
images for icon design guidance

### Patch Changes

- [#11747](#11747)
[`5cff96245`](5cff962)
Thanks [@sarahill](https://github.com/sarahill)! - Updated card layout
patterns to match common action patterns.

- Updated dependencies
\[[`e0ae9565c`](e0ae956),
[`9c24a465c`](9c24a46),
[`f6ba2b2a8`](f6ba2b2),
[`4699bb229`](4699bb2),
[`c3ba6ae1b`](c3ba6ae),
[`1543246b7`](1543246),
[`42c298ea7`](42c298e),
[`3e011e3b6`](3e011e3),
[`e7ab4a8f5`](e7ab4a8),
[`831a683a2`](831a683),
[`6d8ef8c99`](6d8ef8c),
[`ad6315845`](ad63158)]:
    -   @shopify/polaris@12.22.0

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@lgriffee lgriffee added the #gsd:38846 Admin Quality Improvements (Q1 2024) label Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
#gsd:38846 Admin Quality Improvements (Q1 2024)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants