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

[system] Fix composeClasses v6 behavior change #43537

Merged
merged 1 commit into from
Sep 3, 2024

Conversation

oliviertassinari
Copy link
Member

@oliviertassinari oliviertassinari commented Aug 30, 2024

Fix the behavior change introduced in #43363. I worked on this PR because I was trying a button customize in the store and got frustrated in the DX to find the class names applied:

SCR-20240830-uqcw

The performance win stays comparable as before https://jsben.ch/S3LCV

SCR-20240831-biqm

A related design principle: https://www.usertesting.com/blog/gestalt-principles#principle-#3:-proximity.

@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work package: system Specific to @mui/system regression A bug, but worse labels Aug 30, 2024
@mui-bot
Copy link

mui-bot commented Aug 30, 2024

Netlify deploy preview

https://deploy-preview-43537--material-ui.netlify.app/

Bundle size report

Details of bundle changes (Toolpad)
Details of bundle changes

Generated by 🚫 dangerJS against 7fdbf72

@oliviertassinari oliviertassinari added the dx Related to developers' experience label Aug 31, 2024
@HermanBilous
Copy link

By taking inspiration from https://github.com/lukeed/clsx/blob/master/src/lite.js the performance could be improved even more by writing the function like so:

function composeClasses(
  slots,
  getUtilityClass,
  classes = undefined,
) {
  let classValue, output = {}, slot, i = 0, buffer = '', value, slotName;

  for (slotName in slots) {
    slot = slots[slotName]

    for (; i < slot.length; i ++) {
      if (value = slot[i]) {
        buffer += (buffer && ' ') + getUtilityClass(value);


        if (classes && (classValue = classes[value])) {
          buffer += ' ' + classValue;
        }
      }
    }

    output[slotName] = buffer;
  }

  return output;
}

I ran some tests over at https://jsbench.me/, which will run the code a lot more times than jsben.ch and it showed 25% improvement over code currently in v6.

If such code is not up for code convention of mui, changing buffer += getUtilityClass(value) + ' '; into buffer += (buffer && ' ') + getUtilityClass(value); and changing the concatenation order in the next if block fixes the issue, and it's probably the best way to do it since clsx uses it.

Copy link
Contributor

@romgrk romgrk left a comment

Choose a reason for hiding this comment

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

The changes proposed by @HermanBilous also LGTM, caching the result of classes[value] can be good.

@oliviertassinari
Copy link
Member Author

+1 to explore those proposed changes, if we can get an extra performance gain, with a low bundler size overhead, we should grab it.

I don't have the bandwidth to carry this PR further, @mnajdova you merged the initial PR, handing this over to you.

@flaviendelangle flaviendelangle mentioned this pull request Sep 3, 2024
1 task
@brijeshb42
Copy link
Contributor

@oliviertassinari Should me merge this and the proposed changes can be taken up in a follow up ?
Mainly because it fixes a crash in mui-x docs.

@joserodolfofreitas
Copy link
Member

Yes, it'd be great if we could merge this sooner rather than later.

Copy link
Member

@mnajdova mnajdova left a comment

Choose a reason for hiding this comment

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

I am merging as this is a regression. @HermanBilous would you like to open a PR with the proposed changes as a follow up?

@mnajdova mnajdova merged commit 7a60e40 into mui:master Sep 3, 2024
20 checks passed
@oliviertassinari oliviertassinari deleted the fix-classname-space-dx branch September 3, 2024 17:38
@oliviertassinari
Copy link
Member Author

oliviertassinari commented Sep 13, 2024

@HermanBilous I have tried your suggestion at https://jsben.ch/UE7FD, but it seems to be more or less the same:

SCR-20240914-bmnx

I have also tried https://github.blog/news-insights/product-news/openai-o1-in-github-copilot/#optimize-complex-algorithms-with-advanced-reasoning, but GitHub Copilot gave me crap. Maybe with o1, it will get better at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work dx Related to developers' experience package: system Specific to @mui/system regression A bug, but worse
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants