GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Performance opportunity for yarn 3 cache · Issue #325 · actions/setup-node

Performance opportunity for yarn 3 cache #325

New issue
Copy link
New issue
Copy link
Closed
Closed
Performance opportunity for yarn 3 cache#325
Copy link
Assignees
dsame
Labels
feature requestNew feature or request to improve the current logic

Description

@belgattitude
belgattitude
opened on Sep 3, 2021
Issue body actions

Thanks for the new cache feature. Much easier.

After few weeks, I realized that while it supports yarn, there's some improvements that can be made.

Yarn 3 (probably yarn 2+ too) manages downloaded archives pretty well (.yarn/cache/*.zip) and invalidating on yarn.lock changes does not take that into account and I saw a lot of cache misses.

As an example I converted back to action-cache to illustrate and test.

I'm wondering if a similar approach could be done with setup-node ?

Updated example with action cache

Updated on Nov 23th, taking into account @merceyz comment.

Setup action

      # Get the yarn cache path.
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
    
      - name: Restore yarn cache
        uses: actions/cache@v2
        id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: yarn-cache-folder-${{ hashFiles('**/yarn.lock', '.yarnrc.yml') }}
          restore-keys: |
            yarn-cache-folder-

Testing a cache hit after adding a dependency

With setup node cache, as the yarn.lock have changed all packages would be fetched again (1m 28s)
rather than (1s 124ms). Environmentally friendlier 🌳

yarn install --immutable
➤ YN0000: ┌ Fetch step
  ➤ YN0013: │ 1719 packages were already cached, one had to be fetched (superjson@npm:1.7.5)
➤ YN0000: └ Completed in 1s 124ms

Cache Size: ~127 MB (133261279 B)
Cache saved successfully
Cache saved with key: yarn-cache-folder-os-Linux-node--f118ea4bee07eada9df36ad2e83fd6febcbaf06b5b8962689c7650659e872ad3

PS: Key points

~Example with action cache~ (old version, before @merceyz improvements)
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
      - name: Restore yarn cache
        uses: actions/cache@v2
        id: yarn-cache 
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}       
          key: yarn-cache-folder-os-${{ runner.os }}-node-${{ env.node-version }}-${{ hashFiles('**/yarn.lock', '.yarnrc.yml') }}      
          restore-keys: |
            yarn-cache-folder-os-${{ runner.os }}-node-${{ env.node-version }}-
            yarn-cache-folder-os-${{ runner.os }}-

Note

Here's a gist with an optimized install example: https://gist.github.com/belgattitude/042f9caf10d029badbde6cf9d43e400a

Reacted by Gadi Rotenberg, Nicky McCurdy, Jason Kuhrt, Johann Pardanaud, Myyk Seok, Yuku Takahashi, Marco Fugaro, devgioele, Rutul Patel, tony kerz and 15 more

Activity

Next
  1. merceyz commented on Sep 10, 2021

    @merceyz
    merceyz
    on Sep 10, 2021
    More actions

    It can be even more efficient by not including the OS and Node version in the cache key as well - #272 (comment)

    Reacted by Sébastien Vanvelthem and Jason Kuhrt
  2. dmitry-shibanov commented on Nov 12, 2021

    @dmitry-shibanov
    dmitry-shibanov
    on Nov 12, 2021
    Contributor
    More actions

    Hello @belgattitude. Thank you for your feature request. Could you please describe it little bit. Do you want to add restore-keys to the action to take previous cache or you also want to add .yarn/cache/*.zip to cached directories ?

  3. dmitry-shibanov
    added
    feature requestNew feature or request to improve the current logic
    on Nov 12, 2021
  4. dmitry-shibanov commented on Nov 22, 2021

    @dmitry-shibanov
    dmitry-shibanov
    on Nov 22, 2021
    Contributor
    More actions

    Hello @belgattitude, just a gentle ping.

    Reacted by Sébastien Vanvelthem
  5. belgattitude commented on Nov 22, 2021

    @belgattitude
    belgattitude
    on Nov 22, 2021
    Author
    More actions

    Let me check something in a couple of days. I'll be back asap.

    Reacted by Johannes Schickling
  6. belgattitude commented on Nov 23, 2021

    @belgattitude
    belgattitude
    on Nov 23, 2021
    Author
    More actions

    @dmitry-shibanov first of all thanks for considering this issue. I've edited the P/R desc with latest comments from @merceyz

    Do you want to add restore-keys to the action to take previous cache

    Yes that's the idea, that way we take advantage of the built-in yarn 2+/3+ cache management. Yarn will prune old packages and only fetch new ones keeping the packages zipped into cacheFolder in sync with package.json (works with workspace/monorepo too).

    Note that if multiple actions are run in parallel, only the first one will be able to reserve the cache, others will print a warning after:

    > Post restore yarn cache
       Unable to reserve cache with key yarn-cache-folder- 
       cb79cb8e7c0fdd859237ac516da53bc2fdafceb3230fd68444aa4f55c9238980, another job may be creating this cache.
    

    As far as I tested it does not create problems and can be safely ignored..

    you also want to add .yarn/cache/*.zip to cached directories

    Actually this seems to be what matters most. (AFAIK setup/node try to cache only the node_modules folders, right ?)

    There's some kind of guarantee that yarn cache is portable across OS and node versions as @merceyz pointed out, while node_modules might not (native binaries, postinstall tricks).

    As the cache folder is configurable (through yarnrc.yml or YARN_CACHE_FOLDER env), a good way to read it

          # Get the yarn cache path.
          - name: Get yarn cache directory path
            id: yarn-cache-dir-path
            run: echo "::set-output name=dir::$(yarn config get cacheFolder)"

    The restore key could be a constant:

          - name: Restore yarn cache
            uses: actions/cache@v2
            id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
            with:
              path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
              key: yarn-cache-folder-${{ hashFiles('**/yarn.lock', '.yarnrc.yml') }}
              restore-keys: |
                yarn-cache-folder-

    Interestingly pnpm has a benchmark page with some insights about the different modes (cache+node_modules, cache only)

    image

    Looks having both node_modules + yarn cache will be faster (excluding action/cache restore/generate...). I guess even more as yarn won't probably have to re-run the link phase (generally slow with native binaries: esbuild, swc, sharp, prisma...). But with caching node_modules I'm not sure of the portability though (it depends also of setup/node). Note also that node_modules folder is only used in nmLinker: node-modules or nmLinker: pnp modes (otherwise it's assumed to be yarn pnp)

    I would say a safe way would be to just save 'yarn cache folder' and not node_modules.

    Let me know if it answer your questions.

    Have a great day

    PS: @merceyz thanks for your comment about portability, I was wondering if the newly introduced supportedtArchitecture change something ?

  7. devgioele commented on May 5, 2022

    @devgioele
    devgioele
    on May 5, 2022
    More actions

    Do you recommend using actions/setup-node to cache node_modules and to use actions/cache to cache "the yarn cache"?
    Caching both would allow us to skip the linking step too, right? I don't see why it cannot be done.

  8. panticmilos commented on May 9, 2022

    @panticmilos
    panticmilos
    on May 9, 2022
    Contributor
    More actions

    Hi @devgioele,

    Caching node_modules is not possible using setup-node. This action is only caching on a global level. If you would like to cache node_modules, please use actions/cache.

    Cheers

  9. mklueh commented on Jul 10, 2022

    @mklueh
    mklueh
    on Jul 10, 2022
    More actions

    Deleting yarn.lock and running yarn install helped, but in my case with yarn caching enabled

  10. mrtnzlml
    added a commit that references this issue on Jul 15, 2022

    CI: use custom (better) Yarn cache strategy

    7ec627f
  11. kodiakhq
    added a commit that references this issue on Jul 15, 2022

    CI: use custom (better) Yarn cache strategy

    82d9052
  12. nickserv commented on Sep 24, 2022

    @nickserv
    nickserv
    on Sep 24, 2022
    More actions

    I think this is a duplicate of #328, which is similar but not specific to yarn

  13. belgattitude commented on Oct 9, 2022

    @belgattitude
    belgattitude
    on Oct 9, 2022
    Author
    More actions

    Just to share, here's my updated composite action for

    1. yarn 3+ with node-modules linker: https://gist.github.com/belgattitude/042f9caf10d029badbde6cf9d43e400a
    2. pnpm 7+: https://gist.github.com/belgattitude/838b2eba30c324f1f0033a797bab2e31
    Reacted by Thibault Gérondal, Marcus R. Brown, Mateus Craveiro, Omar López, Benoit CATILLON and Vinson Chuong
  14. marcusrbrown
    added 2 commits that reference this issue on Oct 22, 2022

    feat(ci): update Yarn caching

    fc7e71c

    feat(ci): update Yarn caching (#8)

    c33b027
  15. 13 remaining items

  16. dsame commented on May 5, 2023

    @dsame
    dsame
    on May 5, 2023
    Contributor
    More actions

    Thank you @belgattitude for your opinion, now i agree it is worth to continue with the feature in order to avoid huge repo.

    Can you please take a look at this Proof of concept draft PR and tell you opinion about does it make a sense to include node version and step id into the cache key keeping in mind having many huge cache will hit storage limit soon

    https://github.com/actions/setup-node/pull/744/files#diff-55f15e2366942ad15f71a41ac983f8ce9a9882b28b7fd9082f3a26c799783064R42

  17. dsame commented on May 5, 2023

    @dsame
    dsame
    on May 5, 2023
    Contributor
    More actions

    @belgattitude can you please also confirm the following :

    We have to use 3rd parameter of restoreCache in case if all of 3 conditions met:

    The 3rd parameter of restoreCache has to be set to not null in case if all of 3 conditions met:

    1. package manager is yarn
    2. enableGlobalCache is omitted or false
    3. there's no .yarn/cache directory in the working dir
  18. belgattitude commented on May 6, 2023

    @belgattitude
    belgattitude
    on May 6, 2023
    Author
    More actions

    @dsame

    1. only from v2 (v1 behaviour should fallback to current way of doing)
    2. (mmmh). on CI this should be overriden to enableGlobalCache: false through env. Don't run with true on CI (see previous comment in Performance opportunity for yarn 3 cache #325 (comment))
    3. yes

    I have looked into your PR. stepId not necessary in my opinion. But there's something missing to ensure yarn 4. Not a lot of time the next days... I'll figure out a way to share info asap

  19. belgattitude commented on May 6, 2023

    @belgattitude
    belgattitude
    on May 6, 2023
    Author
    More actions

    In the meantime I've tried to answers few questions in

    • CI: speed up CI time by improving yarn install and caches (iteration 1 / >30%) strapi/strapi#16581
    • Ci: improve install time iteration 2 strapi/strapi#16638

    Might worth to have a quick glance

  20. dsame commented on May 9, 2023

    @dsame
    dsame
    on May 9, 2023
    Contributor
    More actions

    The next iteration of PoC PR showed the detection of all the conditions implies the significant changes in code base and makes it unreasonable complicated.

    @belgattitude can you please take a look at this issue - does not it seem the goal of reusing the existing cache can be achieved with resolving that issue in the more straightforward and common way?

    I looked through the links you've sent,

    • according to the key-prefix idea, it is clear but you've provided a custom solution for the specific build while we are trying to provide universal solution for the whole action - this is why it become complicated
    • according to including .yarnec.yaml in hash calculation, it is the overkill and cause unnecessary cache refreshes
    • according to node_modules caching it brings performance gain only for cases of some binaries have to be rebuilt from sources and it is not considered now to be effective enough for most users - downloading and unpacking lot of files can be event slower than npm install ... if the dependencies are already cached.

    Summary: can you please confirm or deny that key-prefix input added to the setup-node cache can boost the yarn3 performance and achieve the same goal as auto-generating the same key-prefix if the conditions are met?

  21. belgattitude commented on May 10, 2023

    @belgattitude
    belgattitude
    on May 10, 2023
    Author
    More actions

    Sorry I couldn't read properly but some quick toughts:

    an you please take a look at this actions/setup-go#358 - does not it seem the goal of reusing the existing cache can be achieved with resolving that issue in the more straightforward and common way?

    I would agree that params helps.

    ccording to including .yarnec.yaml in hash calculation, it is the overkill and cause unnecessary cache refreshes

    yarnrc.yml isn't something that is mutated often, so I wouldn't worry about it to invalidate things often.

    Not accounting for it looks fragile in my impression, but I'm not 100% sure. I would let the benefit to doubt and include it in the hash (ie someone changes an advanced param...)

    What do you mean by overkill ? is the hash call slow ?

    according to node_modules caching it brings performance gain only for cases of some binaries have to be rebuilt from sources and it is not considered now to be effective enough for most users - downloading and unpacking lot of files can be event slower than npm install ... if the dependencies are already cached

    For yarn 2+, I would not cache node_modules at all. (only few edges case would benefit from it, in this case they might use a totally different action than the one in setup/node)

    Can you please confirm or deny that key-prefix input added to the setup-node cache can boost the yarn3 performance and achieve the same goal as auto-generating the same key-prefix if #325 (comment)?

    The general idea seems good, but as always a deep testing is required. Do you expect me to do it ? In that case I'll need some time.

    showed the detection of #325 (comment) implies the significant changes in code base and makes it unreasonable complicated.

    Can you precise the meaning of ? "Unreasonable" => "in your opinion you wouldnt include the feature ?". Or you're still wondering ? I would understand of course.

    But as a small personal note, it might have an very nice impact on global CI time... . Not sure about how many projects uses yarn 2+ and setup/node cache, but that's an interesting feature imho

  22. dsame commented on May 10, 2023

    @dsame
    dsame
    on May 10, 2023
    Contributor
    More actions

    Hello, @belgattitude

    The general idea seems good, but as always a deep testing is required. Do you expect me to do it ?

    Not at all, i've requested you opinion just to confirm we are no the same page and to get rid off some my hesitations. Now there's a plan an we are on the road. Thanks for the cooperation.

    Reacted by Sébastien Vanvelthem and Ben Irvin
  23. nijikon commented on Jul 18, 2023

    @nijikon
    nijikon
    on Jul 18, 2023
    More actions

    @marko-zivic-93 back in April #325 (comment), you wrote that you will be having a look at this in the upcoming quarter. Were you talking about Q2 or Q3 of 2023?

  24. dsame commented on Jul 19, 2023

    @dsame
    dsame
    on Jul 19, 2023
    Contributor
    More actions

    Hello @nijikon the requested changes are merged into the main branch and are available since v3.7.0 release

  25. nijikon commented on Jul 19, 2023

    @nijikon
    nijikon
    on Jul 19, 2023
    More actions

    @dsame thanks. I will have a look if that helps my case.

  26. dsame commented on Jul 25, 2023

    @dsame
    dsame
    on Jul 25, 2023
    Contributor
    More actions

    Hello @nijikon, i am going to close this issue because the PRs are merged and due to inactivity, but please feel free to reopen it or create new issue in case if the problem still exists

  27. dsame
    closed this as completedon Jul 25, 2023
  28. nijikon commented on Jul 25, 2023

    @nijikon
    nijikon
    on Jul 25, 2023
    More actions

    @dsame I think I'm good. My current problem is that it does not cache the node_modules folder, but I read that it's expected.

  29. deining
    added a commit that references this issue on Nov 9, 2023

    chore(deps-dev): bump @types/js-yaml from 4.0.4 to 4.0.5 (actions#325)

    fbeb5fd
  30. CrispyBaguette
    added a commit that references this issue on Nov 8, 2024

    Update actions/setup-node action to v4 (#56)

    a2ef20d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @dsame
    dsame

Labels

feature requestNew feature or request to improve the current logic

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.