Skip to content

Commit

Permalink
perf: add dynamically loaded cache-dependencies module
Browse files Browse the repository at this point in the history
This reduces the main bundle size by ⚠️  1.1 MB minified! This bundle is loaded only when caching is enabled.

The reason for this huge reduction is that caching dependencies uses the `@actions/cache` package, which is an extremely large package with big dependencies.

`setup-python` is used in `setup-cpp` as a library. This optimization reduces the bundle size for that package as well.
  • Loading branch information
aminya committed Sep 11, 2023
1 parent 65d7f2d commit 5958a00
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 73 deletions.
2 changes: 1 addition & 1 deletion __tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import path from 'path';
import {
validateVersion,
validatePythonVersionFormatForPyPy,
isCacheFeatureAvailable,
getVersionInputFromFile,
getVersionInputFromPlainFile,
getVersionInputFromTomlFile
} from '../src/utils';
import {isCacheFeatureAvailable} from '../src/cache-dependencies';

jest.mock('@actions/cache');
jest.mock('@actions/core');
Expand Down
107 changes: 75 additions & 32 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54782,7 +54782,7 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
var Stream = _interopDefault(__nccwpck_require__(2781));
var http = _interopDefault(__nccwpck_require__(3685));
var Url = _interopDefault(__nccwpck_require__(7310));
var whatwgUrl = _interopDefault(__nccwpck_require__(3323));
var whatwgUrl = _interopDefault(__nccwpck_require__(629));
var https = _interopDefault(__nccwpck_require__(5687));
var zlib = _interopDefault(__nccwpck_require__(9796));

Expand Down Expand Up @@ -57284,7 +57284,7 @@ module.exports = {

/***/ }),

/***/ 3323:
/***/ 629:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {

"use strict";
Expand Down Expand Up @@ -61700,7 +61700,7 @@ const minVersion = __nccwpck_require__(4179)
const validRange = __nccwpck_require__(2098)
const outside = __nccwpck_require__(420)
const gtr = __nccwpck_require__(9380)
const ltr = __nccwpck_require__(8726)
const ltr = __nccwpck_require__(3323)
const intersects = __nccwpck_require__(7008)
const simplifyRange = __nccwpck_require__(5297)
const subset = __nccwpck_require__(7863)
Expand Down Expand Up @@ -62085,7 +62085,7 @@ module.exports = intersects

/***/ }),

/***/ 8726:
/***/ 3323:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

const outside = __nccwpck_require__(420)
Expand Down Expand Up @@ -68605,6 +68605,74 @@ try {
} catch (er) {}


/***/ }),

/***/ 1852:
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {

"use strict";

var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.isCacheFeatureAvailable = exports.cacheDependencies = void 0;
const cache = __importStar(__nccwpck_require__(7799));
const core = __importStar(__nccwpck_require__(2186));
const cache_factory_1 = __nccwpck_require__(7549);
function cacheDependencies(cache, pythonVersion) {
return __awaiter(this, void 0, void 0, function* () {
if (isCacheFeatureAvailable()) {
const cacheDependencyPath = core.getInput('cache-dependency-path') || undefined;
const cacheDistributor = cache_factory_1.getCacheDistributor(cache, pythonVersion, cacheDependencyPath);
yield cacheDistributor.restoreCache();
}
});
}
exports.cacheDependencies = cacheDependencies;
function isGhes() {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
function isCacheFeatureAvailable() {
if (cache.isFeatureAvailable()) {
return true;
}
if (isGhes()) {
core.warning('Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.');
return false;
}
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;


/***/ }),

/***/ 8953:
Expand Down Expand Up @@ -69807,18 +69875,10 @@ const finderPyPy = __importStar(__nccwpck_require__(4003));
const path = __importStar(__nccwpck_require__(1017));
const os = __importStar(__nccwpck_require__(2037));
const fs_1 = __importDefault(__nccwpck_require__(7147));
const cache_factory_1 = __nccwpck_require__(7549);
const utils_1 = __nccwpck_require__(1314);
function isPyPyVersion(versionSpec) {
return versionSpec.startsWith('pypy');
}
function cacheDependencies(cache, pythonVersion) {
return __awaiter(this, void 0, void 0, function* () {
const cacheDependencyPath = core.getInput('cache-dependency-path') || undefined;
const cacheDistributor = cache_factory_1.getCacheDistributor(cache, pythonVersion, cacheDependencyPath);
yield cacheDistributor.restoreCache();
});
}
function resolveVersionInputFromDefaultFile() {
const couples = [
['.python-version', utils_1.getVersionInputFromPlainFile]
Expand Down Expand Up @@ -69891,7 +69951,8 @@ function run() {
}
core.endGroup();
const cache = core.getInput('cache');
if (cache && utils_1.isCacheFeatureAvailable()) {
if (cache) {
const { cacheDependencies } = yield Promise.resolve().then(() => __importStar(__nccwpck_require__(1852)));
yield cacheDependencies(cache, pythonVersion);
}
}
Expand Down Expand Up @@ -69948,9 +70009,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getVersionInputFromFile = exports.getVersionInputFromPlainFile = exports.getVersionInputFromTomlFile = exports.getOSInfo = exports.getLinuxInfo = exports.logWarning = exports.isCacheFeatureAvailable = exports.isGhes = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_MAC = exports.IS_LINUX = exports.IS_WINDOWS = void 0;
exports.getVersionInputFromFile = exports.getVersionInputFromPlainFile = exports.getVersionInputFromTomlFile = exports.getOSInfo = exports.getLinuxInfo = exports.logWarning = exports.validatePythonVersionFormatForPyPy = exports.writeExactPyPyVersionFile = exports.readExactPyPyVersionFile = exports.getPyPyVersionFromPath = exports.isNightlyKeyword = exports.validateVersion = exports.createSymlinkInFolder = exports.WINDOWS_PLATFORMS = exports.WINDOWS_ARCHS = exports.IS_MAC = exports.IS_LINUX = exports.IS_WINDOWS = void 0;
/* eslint no-unsafe-finally: "off" */
const cache = __importStar(__nccwpck_require__(7799));
const core = __importStar(__nccwpck_require__(2186));
const fs_1 = __importDefault(__nccwpck_require__(7147));
const path = __importStar(__nccwpck_require__(1017));
Expand Down Expand Up @@ -70023,23 +70083,6 @@ function validatePythonVersionFormatForPyPy(version) {
return re.test(version);
}
exports.validatePythonVersionFormatForPyPy = validatePythonVersionFormatForPyPy;
function isGhes() {
const ghUrl = new URL(process.env['GITHUB_SERVER_URL'] || 'https://github.com');
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
exports.isGhes = isGhes;
function isCacheFeatureAvailable() {
if (cache.isFeatureAvailable()) {
return true;
}
if (isGhes()) {
core.warning('Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.');
return false;
}
core.warning('The runner was not able to contact the cache service. Caching will be skipped');
return false;
}
exports.isCacheFeatureAvailable = isCacheFeatureAvailable;
function logWarning(message) {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${message}`);
Expand Down
41 changes: 41 additions & 0 deletions src/cache-dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import {getCacheDistributor} from './cache-distributions/cache-factory';

export async function cacheDependencies(cache: string, pythonVersion: string) {
if (isCacheFeatureAvailable()) {
const cacheDependencyPath =
core.getInput('cache-dependency-path') || undefined;
const cacheDistributor = getCacheDistributor(
cache,
pythonVersion,
cacheDependencyPath
);
await cacheDistributor.restoreCache();
}
}

function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}

export function isCacheFeatureAvailable(): boolean {
if (cache.isFeatureAvailable()) {
return true;
}

if (isGhes()) {
core.warning(
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'
);
return false;
}

core.warning(
'The runner was not able to contact the cache service. Caching will be skipped'
);
return false;
}
16 changes: 2 additions & 14 deletions src/setup-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import * as finderPyPy from './find-pypy';
import * as path from 'path';
import * as os from 'os';
import fs from 'fs';
import {getCacheDistributor} from './cache-distributions/cache-factory';
import {
isCacheFeatureAvailable,
logWarning,
IS_MAC,
getVersionInputFromFile,
Expand All @@ -17,17 +15,6 @@ function isPyPyVersion(versionSpec: string) {
return versionSpec.startsWith('pypy');
}

async function cacheDependencies(cache: string, pythonVersion: string) {
const cacheDependencyPath =
core.getInput('cache-dependency-path') || undefined;
const cacheDistributor = getCacheDistributor(
cache,
pythonVersion,
cacheDependencyPath
);
await cacheDistributor.restoreCache();
}

function resolveVersionInputFromDefaultFile(): string[] {
const couples: [string, (versionFile: string) => string[]][] = [
['.python-version', getVersionInputFromPlainFile]
Expand Down Expand Up @@ -125,7 +112,8 @@ async function run() {
}
core.endGroup();
const cache = core.getInput('cache');
if (cache && isCacheFeatureAvailable()) {
if (cache) {
const {cacheDependencies} = await import('./cache-dependencies');
await cacheDependencies(cache, pythonVersion);
}
} else {
Expand Down
26 changes: 0 additions & 26 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint no-unsafe-finally: "off" */
import * as cache from '@actions/cache';
import * as core from '@actions/core';
import fs from 'fs';
import * as path from 'path';
Expand Down Expand Up @@ -99,31 +98,6 @@ export function validatePythonVersionFormatForPyPy(version: string) {
return re.test(version);
}

export function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}

export function isCacheFeatureAvailable(): boolean {
if (cache.isFeatureAvailable()) {
return true;
}

if (isGhes()) {
core.warning(
'Caching is only supported on GHES version >= 3.5. If you are on a version >= 3.5, please check with your GHES admin if the Actions cache service is enabled or not.'
);
return false;
}

core.warning(
'The runner was not able to contact the cache service. Caching will be skipped'
);
return false;
}

export function logWarning(message: string): void {
const warningPrefix = '[warning]';
core.info(`${warningPrefix}${message}`);
Expand Down

0 comments on commit 5958a00

Please sign in to comment.