Teardown compiler to improve rendering perf (#6391)

* Teardown compiler to improve rendering perf

* Upgrade compiler to minor

* Try hacky test guard

* Nicer teardown flow
This commit is contained in:
Bjorn Lu 2023-03-02 22:54:07 +08:00 committed by GitHub
parent 0e378c3b87
commit 45501c531b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 10 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Teardown compiler after Vite build to free up memory when rendering pages

View file

@ -99,7 +99,7 @@
"test:e2e:match": "playwright test -g"
},
"dependencies": {
"@astrojs/compiler": "^1.1.0",
"@astrojs/compiler": "^1.2.0",
"@astrojs/language-server": "^0.28.3",
"@astrojs/markdown-remark": "^2.0.1",
"@astrojs/telemetry": "^2.0.1",

View file

@ -203,7 +203,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
case 'build': {
const { default: build } = await import('../core/build/index.js');
return await build(settings, { ...flags, logging, telemetry });
return await build(settings, { ...flags, logging, telemetry, teardownCompiler: true });
}
case 'check': {

View file

@ -18,13 +18,19 @@ import { apply as applyPolyfill } from '../polyfill.js';
import { RouteCache } from '../render/route-cache.js';
import { createRouteManifest } from '../routing/index.js';
import { collectPagesData } from './page-data.js';
import { staticBuild } from './static-build.js';
import { staticBuild, viteBuild } from './static-build.js';
import { getTimeStat } from './util.js';
import { StaticBuildOptions } from './types.js';
export interface BuildOptions {
mode?: RuntimeMode;
logging: LogOptions;
telemetry: AstroTelemetry;
/**
* Teardown the compiler WASM instance after build. This can improve performance when
* building once, but may cause a performance hit if building multiple times in a row.
*/
teardownCompiler?: boolean;
}
/** `astro build` */
@ -42,6 +48,7 @@ class AstroBuilder {
private routeCache: RouteCache;
private manifest: ManifestData;
private timer: Record<string, number>;
private teardownCompiler: boolean;
constructor(settings: AstroSettings, options: BuildOptions) {
if (options.mode) {
@ -49,6 +56,7 @@ class AstroBuilder {
}
this.settings = settings;
this.logging = options.logging;
this.teardownCompiler = options.teardownCompiler ?? false;
this.routeCache = new RouteCache(this.logging);
this.origin = settings.config.site
? new URL(settings.config.site).origin
@ -126,7 +134,7 @@ class AstroBuilder {
colors.dim(`Completed in ${getTimeStat(this.timer.init, performance.now())}.`)
);
await staticBuild({
const opts: StaticBuildOptions = {
allPages,
settings: this.settings,
logging: this.logging,
@ -135,9 +143,13 @@ class AstroBuilder {
origin: this.origin,
pageNames,
routeCache: this.routeCache,
teardownCompiler: this.teardownCompiler,
viteConfig,
buildConfig,
});
};
const { internals } = await viteBuild(opts);
await staticBuild(opts, internals);
// Write any additionally generated assets to disk.
this.timer.assetsStart = performance.now();

View file

@ -1,3 +1,4 @@
import { teardown } from '@astrojs/compiler';
import * as eslexer from 'es-module-lexer';
import glob from 'fast-glob';
import fs from 'fs';
@ -25,7 +26,7 @@ import { registerAllPlugins } from './plugins/index.js';
import type { PageBuildData, StaticBuildOptions } from './types';
import { getTimeStat } from './util.js';
export async function staticBuild(opts: StaticBuildOptions) {
export async function viteBuild(opts: StaticBuildOptions) {
const { allPages, settings } = opts;
// Make sure we have an adapter before building
@ -98,6 +99,17 @@ export async function staticBuild(opts: StaticBuildOptions) {
settings.timer.end('Client build');
// Free up memory
internals.ssrEntryChunk = undefined;
if (opts.teardownCompiler) {
teardown();
}
return { internals };
}
export async function staticBuild(opts: StaticBuildOptions, internals: BuildInternals) {
const { settings } = opts;
switch (settings.config.output) {
case 'static': {
settings.timer.start('Static generate');

View file

@ -39,6 +39,7 @@ export interface StaticBuildOptions {
pageNames: string[];
routeCache: RouteCache;
viteConfig: InlineConfig;
teardownCompiler: boolean;
}
export interface SingleFileBuiltModule {

View file

@ -395,7 +395,7 @@ importers:
packages/astro:
specifiers:
'@astrojs/compiler': ^1.1.0
'@astrojs/compiler': ^1.2.0
'@astrojs/language-server': ^0.28.3
'@astrojs/markdown-remark': ^2.0.1
'@astrojs/telemetry': ^2.0.1
@ -485,7 +485,7 @@ importers:
yargs-parser: ^21.0.1
zod: ^3.17.3
dependencies:
'@astrojs/compiler': 1.1.0
'@astrojs/compiler': 1.2.0
'@astrojs/language-server': 0.28.3
'@astrojs/markdown-remark': link:../markdown/remark
'@astrojs/telemetry': link:../telemetry
@ -3956,8 +3956,8 @@ packages:
/@astrojs/compiler/0.31.4:
resolution: {integrity: sha512-6bBFeDTtPOn4jZaiD3p0f05MEGQL9pw2Zbfj546oFETNmjJFWO3nzHz6/m+P53calknCvyVzZ5YhoBLIvzn5iw==}
/@astrojs/compiler/1.1.0:
resolution: {integrity: sha512-C4kTwirys+HafufMqaxCbML2wqkGaXJM+5AekXh/v1IIOnMIdcEON9GBYsG6qa8aAmLhZ58aUZGPhzcA3Dx7Uw==}
/@astrojs/compiler/1.2.0:
resolution: {integrity: sha512-O8yPCyuq+PU9Fjht2tIW6WzSWiq8qDF1e8uAX2x+SOGFzKqOznp52UlDG2mSf+ekf0Z3R34sb64O7SgX+asTxg==}
dev: false
/@astrojs/language-server/0.28.3: