microsoft / griffel

CSS-in-JS with ahead-of-time compilation ⚡️
https://griffel.js.org
MIT License
1.2k stars 61 forks source link

Can we cache the `makeStyles` internal #477

Closed Leolewin closed 10 months ago

Leolewin commented 10 months ago

Hi Griffel experts!

Recently we noticed that the makeStyles API is the bottleneck of a component runtime cost. So currently we cached the hooks result via useMemo. We looked through the source code of makeStyles and find it seems doesn't cache the styles internal. Since the API can only be called in the file root level and the result is static and time consuming, why not cache the result internal?

layershifter commented 10 months ago

So currently we cached the hooks result via useMemo. We looked through the source code of makeStyles and find it seems doesn't cache the styles internal. Since the API can only be called in the file root level and the result is static and time consuming, why not cache the result internal?

@Leolewin I don't follow what you're caching in a React component 🤔 After the first expensive pass, a hook returned by makeStyles() will do nothing:

https://github.com/microsoft/griffel/blob/f134bc6056599a78cf27b71dec854224a162ee22/packages/core/src/makeStyles.ts#L30-L60

Can you please clarify WDYM?


Anyway, yes, makeStyles() and makeResetStyles() are expensive as any other CSS-in-JS, that's why AOT (that removes computation cost) and CSS extraction should be used it in production.

Leolewin commented 10 months ago

So currently we cached the hooks result via useMemo. We looked through the source code of makeStyles and find it seems doesn't cache the styles internal. Since the API can only be called in the file root level and the result is static and time consuming, why not cache the result internal?

@Leolewin I don't follow what you're caching in a React component 🤔 After the first expensive pass, a hook returned by makeStyles() will do nothing:

https://github.com/microsoft/griffel/blob/f134bc6056599a78cf27b71dec854224a162ee22/packages/core/src/makeStyles.ts#L30-L60

Can you please clarify WDYM?

Anyway, yes, makeStyles() and makeResetStyles() are expensive as any other CSS-in-JS, that's why AOT (that removes computation cost) and CSS extraction should be used it in production.

Hi @layershifter

Sorry for the late reply and the misunderstanding caused by the issue description.

At very beginning, I fond the makeStyles API has large runtime cost. So I tried to use React.useMemo API to cache the styles returned by hook like:

const useStyles = makeStyles({ root: {color: 'red'}});

const Component = (props) => {
    const styles = React.useMemo(() => useStyles());
    // ...
}

But as the codes you mentioned, the large styles computation and stylesheet insertion is just happened in the first time been called. In following re-render time the styles result is get and returned directly from the closure variables. So it has almost zero runtime cost and useMemo will increase the time consuming in this case.

Thanks for your help to resolve my puzzles. Will close this issue.