Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Rework image generation to improve performance #8821

Merged
merged 14 commits into from
Oct 25, 2023
Merged
5 changes: 5 additions & 0 deletions .changeset/good-mirrors-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': minor
---

Add concurrency to asset generation
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
69 changes: 32 additions & 37 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as colors from 'kleur/colors';
import { bgGreen, black, cyan, dim, green, magenta } from 'kleur/colors';
import fs from 'node:fs';
import OS from 'node:os';
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
import { fileURLToPath } from 'node:url';
import pLimit from 'p-limit';
import type { OutputAsset, OutputChunk } from 'rollup';
import type { BufferEncoding } from 'vfile';
import type {
Expand Down Expand Up @@ -197,57 +199,50 @@ export async function generatePages(opts: StaticBuildOptions, internals: BuildIn
}

const staticImageList = getStaticImageList();
let imageCount = 0;

if (staticImageList.size)
if (staticImageList.size) {
const cpuCount = OS.cpus().length;
const limit = pLimit(cpuCount);
logger.info(null, `\n${bgGreen(black(` generating optimized images `))}`);
let count = 0;
for (const imageData of staticImageList.entries()) {
count++;
await generateImage(
pipeline,
imageData[1].options,
imageData[1].path,
count,
staticImageList.size

await Promise.all(
Array.from(staticImageList.entries()).map((imageData) =>
limit(() => generateImage(imageData[1].options, imageData[1].path, staticImageList.size))
)
);
}

delete globalThis?.astroAsset?.addStaticImage;
delete globalThis?.astroAsset?.addStaticImage;
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
}

await runHookBuildGenerated({
config: opts.settings.config,
logger: pipeline.getLogger(),
});

logger.info(null, dim(`Completed in ${getTimeStat(timer, performance.now())}.\n`));
}

async function generateImage(
pipeline: BuildPipeline,
transform: ImageTransform,
path: string,
count: number,
totalCount: number
) {
const logger = pipeline.getLogger();
let timeStart = performance.now();
const generationData = await generateImageInternal(pipeline, transform, path);
async function generateImage(transform: ImageTransform, path: string, totalCount: number) {
let timeStart = performance.now();
const generationData = await generateImageInternal(pipeline, transform, path);

if (!generationData) {
return;
}
if (!generationData) {
return;
}

const timeEnd = performance.now();
const timeChange = getTimeStat(timeStart, timeEnd);
const timeIncrease = `(+${timeChange})`;
const statsText = generationData.cached
? `(reused cache entry)`
: `(before: ${generationData.weight.before}kB, after: ${generationData.weight.after}kB)`;
const counter = `(${count}/${totalCount})`;
logger.info(
null,
` ${green('▶')} ${path} ${dim(statsText)} ${dim(timeIncrease)} ${dim(counter)}`
);
const timeEnd = performance.now();
const timeChange = getTimeStat(timeStart, timeEnd);
const timeIncrease = `(+${timeChange})`;
const statsText = generationData.cached
? `(reused cache entry)`
: `(before: ${generationData.weight.before}kB, after: ${generationData.weight.after}kB)`;
const counter = `(${imageCount}/${totalCount})`;
logger.info(
null,
` ${green('▶')} ${path} ${dim(statsText)} ${dim(timeIncrease)} ${dim(counter)}`
);
imageCount++;
}
}

async function generatePage(
Expand Down