Skip to content

Commit

Permalink
Improve runtime caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dorssel committed Dec 30, 2024
1 parent 085a8c8 commit 1da159e
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 11 deletions.
30 changes: 30 additions & 0 deletions .github/actions/calculate-cache-key/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
#
# SPDX-License-Identifier: MIT

---
# yaml-language-server: $schema=https://json.schemastore.org/github-action.json

name: 'Calculate cache key'

description: 'Calculate the cache key for the given runtime'

inputs:
runtime:
description: 'Runtime identifier'
required: true

outputs:
key:
description: 'The key value for the runtime cache'
value: ${{ steps.calculate-cache-key.outputs.key }}

runs:
using: composite

steps:
- id: calculate-cache-key
shell: bash
run: |
echo "key=runtime-${{ inputs.runtime }}-${{ hashFiles('.gitmodules', 'libc/libc.c', format('.github/workflows/runtime-{0}.yml', inputs.runtime)) }}" \
>> "$GITHUB_OUTPUT"
39 changes: 39 additions & 0 deletions .github/actions/lookup-runtime-cache/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
#
# SPDX-License-Identifier: MIT

---
# yaml-language-server: $schema=https://json.schemastore.org/github-action.json

name: 'Lookup runtime cache'

description: 'Checks if a cache exists for the given runtime'

inputs:
runtime:
description: 'Runtime identifier'
required: true

outputs:
cache-hit:
description: 'Indicates if the cache exists or not'
value: ${{ steps.cache-lookup.outputs.cache-hit }}

runs:
using: composite

steps:
- id: calculate-cache-key
uses: ./.github/actions/calculate-cache-key
with:
runtime: ${{ inputs.runtime }}

- id: cache-lookup
uses: actions/cache/restore@v4
with:
key: ${{ steps.calculate-cache-key.outputs.key }}
with:
path: |
Xmss/runtimes/**
enableCrossOsArchive: true
lookup-only: true
32 changes: 32 additions & 0 deletions .github/actions/restore-runtime/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-FileCopyrightText: 2024 Frans van Dorsselaer
#
# SPDX-License-Identifier: MIT

---
# yaml-language-server: $schema=https://json.schemastore.org/github-action.json

name: 'Restore runtime'

description: 'Retrieve a previously cached runtime (which must exists)'

inputs:
runtime:
description: 'Runtime identifier'
required: true

runs:
using: composite

steps:
- id: calculate-cache-key
uses: ./.github/actions/calculate-cache-key
with:
runtime: ${{ inputs.runtime }}

- id: restore
uses: actions/cache/restore@v4
with:
path: |
Xmss/runtimes/**
key: ${{ steps.calculate-cache-key.outputs.key }}
fail-on-cache-miss: true
29 changes: 29 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,33 @@ jobs:
contents: read
actions: write

lookup-runtimes-cache:
runs-on: ubuntu-latest

outputs:
osx-arm64: ${{ steps.lookup-osx-arm64.outputs.cache-hit }}

steps:
- id: checkout
uses: actions/checkout@v4

- id: lookup-osx-arm64
uses: ./.github/actions/lookup-runtime-cache
with:
runtime: osx-arm64

runtime-osx-arm64:
needs: lookup-runtimes-cache
if: ${{ !needs.lookup-runtimes-cache.outputs.osx-arm64 }}
uses: ./.github/workflows/runtime-osx-arm64.yml
permissions:
contents: read
actions: write

build-dotnet:
needs:
- cache-runtimes
- runtime-osx-arm64

runs-on: ubuntu-latest

Expand All @@ -63,6 +87,11 @@ jobs:
key: runtime-win-x64-${{ hashFiles('.gitmodules', 'libc/libc.c', '.github/workflows/runtime-win-x64.yml') }}
fail-on-cache-miss: true

- name: Restore osx-arm64 runtime
uses: ./.github/actions/restore-runtime
with:
runtime: osx-arm64

- name: Restore browser-wasm artifact
uses: actions/cache/restore@v4
with:
Expand Down
39 changes: 28 additions & 11 deletions .github/workflows/runtime-osx-arm64.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ name: Runtime osx-arm64

on:
workflow_dispatch:
inputs:
ignore_cache:
description: 'Force building of the runtime, even if a cached result already exists.'
type: boolean
required: true
default: true
workflow_call:
inputs:
ignore_cache:
description: 'Force building of the runtime, even if a cached result already exists.'
type: boolean
required: false
default: false

env:
RUNTIME: osx-arm64
Expand All @@ -38,7 +50,18 @@ jobs:
echo "value=runtime-${{ env.RUNTIME }}-${{ hashFiles('.gitmodules', 'libc/libc.c', '.github/workflows/runtime-osx-arm64.yml') }}" \
>> "$GITHUB_OUTPUT"
- name: Check existing cache
id: cache-check
uses: actions/cache/restore@v4
with:
path: |
Xmss/runtimes/**
key: ${{ steps.cache-key.outputs.value }}
enableCrossOsArchive: true
lookup-only: true

- name: Configure
if: ${{ inputs.ignore_cache || !steps.cache-check.outputs.cache-hit }}
run: >
cmake
-S xmss-library
Expand All @@ -49,36 +72,30 @@ jobs:
-DCMAKE_C_FLAGS="-Wno-error=implicit-int-conversion"
- name: Build
if: ${{ inputs.ignore_cache || !steps.cache-check.outputs.cache-hit }}
run: cmake --build build

- name: Test
if: ${{ inputs.ignore_cache || !steps.cache-check.outputs.cache-hit }}
working-directory: build
run: ctest

- name: Copy artifact
if: ${{ inputs.ignore_cache || !steps.cache-check.outputs.cache-hit }}
run: |
mkdir -p "Xmss/runtimes/${{ env.RUNTIME }}/native"
cp build/src/libxmss.dylib "Xmss/runtimes/${{ env.RUNTIME }}/native/xmss.dylib"
- name: Check existing cache
id: cache-check
uses: actions/cache/restore@v4
with:
path: |
Xmss/runtimes/**
key: ${{ steps.cache-key.outputs.value }}
enableCrossOsArchive: true
lookup-only: true

- name: Delete existing cache
if: ${{ steps.cache-check.outputs.cache-hit }}
if: ${{ inputs.ignore_cache && steps.cache-check.outputs.cache-hit }}
run: |
gh extension install actions/gh-actions-cache
gh actions-cache delete "${{ steps.cache-key.outputs.value }}" --confirm
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Save cache
if: ${{ inputs.ignore_cache || !steps.cache-check.outputs.cache-hit }}
uses: actions/cache/save@v4
with:
path: |
Expand Down

0 comments on commit 1da159e

Please sign in to comment.