Skip to content

Commit

Permalink
cache: add support for zstd --adapt
Browse files Browse the repository at this point in the history
Signed-off-by: Sora Morimoto <[email protected]>
  • Loading branch information
smorimoto committed Jul 23, 2024
1 parent 1db7362 commit 470858f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/cache/__tests__/tar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ test('zstd create tar', async () => {
.concat(IS_MAC ? ['--delay-directory-restore'] : [])
.concat([
'--use-compress-program',
IS_WINDOWS ? '"zstd -T0 --long=30"' : 'zstdmt --long=30'
IS_WINDOWS ? '"zstd -T0 --adapt --long=30"' : 'zstdmt --adapt --long=30'
])
.join(' '),
undefined, // args
Expand Down Expand Up @@ -292,7 +292,7 @@ test('zstd create tar with windows BSDtar', async () => {
expect(execMock).toHaveBeenNthCalledWith(
2,
[
'zstd -T0 --long=30 --force -o',
'zstd -T0 --adapt --long=30 --force -o',
CacheFilename.Zstd.replace(/\\/g, '/'),
TarFilename.replace(/\\/g, '/')
].join(' '),
Expand Down
8 changes: 6 additions & 2 deletions packages/cache/src/internal/cacheUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ export async function getCompressionMethod(): Promise<CompressionMethod> {
const version = semver.clean(versionOutput)
core.debug(`zstd version: ${version}`)

if (versionOutput === '') {
if (version === null) {
return CompressionMethod.Gzip
} else {
} else if (semver.lt(version, '1.3.2')) {
return CompressionMethod.ZstdWithoutLong
} else if (semver.lt(version, '1.3.6')) {
return CompressionMethod.ZstdWithoutAdapt
} else {
return CompressionMethod.Zstd
}
}

Expand Down
5 changes: 4 additions & 1 deletion packages/cache/src/internal/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ export enum CacheFilename {
export enum CompressionMethod {
Gzip = 'gzip',
// Long range mode was added to zstd in v1.3.2.
// This enum is for earlier version of zstd that does not have --long support
// https://github.com/facebook/zstd/releases/tag/v1.3.2
ZstdWithoutLong = 'zstd-without-long',
// Adapt mode was added to zstd in v1.3.6.
// https://github.com/facebook/zstd/releases/tag/v1.3.6
ZstdWithoutAdapt = 'zstd-without-adapt',
Zstd = 'zstd'
}

Expand Down
14 changes: 14 additions & 0 deletions packages/cache/src/internal/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ async function getDecompressionProgram(
// Used for creating the archive
// -T#: Compress using # working thread. If # is 0, attempt to detect and use the number of physical CPU cores.
// zstdmt is equivalent to 'zstd -T0'
// --adapt: Dynamically adapt compression level to I/O conditions.
// --long=#: Enables long distance matching with # bits. Maximum is 30 (1GB) on 32-bit OS and 31 (2GB) on 64-bit.
// Using 30 here because we also support 32-bit self-hosted runners.
// Long range mode is added to zstd in v1.3.2 release, so we will not use --long in older version of zstd.
Expand All @@ -221,6 +222,19 @@ async function getCompressionProgram(
IS_WINDOWS
switch (compressionMethod) {
case CompressionMethod.Zstd:
return BSD_TAR_ZSTD
? [
'zstd -T0 --adapt --long=30 --force -o',
cacheFileName.replace(new RegExp(`\\${path.sep}`, 'g'), '/'),
TarFilename
]
: [
'--use-compress-program',
IS_WINDOWS
? '"zstd -T0 --adapt --long=30"'
: 'zstdmt --adapt --long=30'
]
case CompressionMethod.ZstdWithoutAdapt:
return BSD_TAR_ZSTD
? [
'zstd -T0 --long=30 --force -o',
Expand Down

0 comments on commit 470858f

Please sign in to comment.