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:
parent
0e378c3b87
commit
45501c531b
7 changed files with 40 additions and 10 deletions
5
.changeset/bright-forks-add.md
Normal file
5
.changeset/bright-forks-add.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Teardown compiler after Vite build to free up memory when rendering pages
|
|
@ -99,7 +99,7 @@
|
||||||
"test:e2e:match": "playwright test -g"
|
"test:e2e:match": "playwright test -g"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@astrojs/compiler": "^1.1.0",
|
"@astrojs/compiler": "^1.2.0",
|
||||||
"@astrojs/language-server": "^0.28.3",
|
"@astrojs/language-server": "^0.28.3",
|
||||||
"@astrojs/markdown-remark": "^2.0.1",
|
"@astrojs/markdown-remark": "^2.0.1",
|
||||||
"@astrojs/telemetry": "^2.0.1",
|
"@astrojs/telemetry": "^2.0.1",
|
||||||
|
|
|
@ -203,7 +203,7 @@ async function runCommand(cmd: string, flags: yargs.Arguments) {
|
||||||
case 'build': {
|
case 'build': {
|
||||||
const { default: build } = await import('../core/build/index.js');
|
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': {
|
case 'check': {
|
||||||
|
|
|
@ -18,13 +18,19 @@ import { apply as applyPolyfill } from '../polyfill.js';
|
||||||
import { RouteCache } from '../render/route-cache.js';
|
import { RouteCache } from '../render/route-cache.js';
|
||||||
import { createRouteManifest } from '../routing/index.js';
|
import { createRouteManifest } from '../routing/index.js';
|
||||||
import { collectPagesData } from './page-data.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 { getTimeStat } from './util.js';
|
||||||
|
import { StaticBuildOptions } from './types.js';
|
||||||
|
|
||||||
export interface BuildOptions {
|
export interface BuildOptions {
|
||||||
mode?: RuntimeMode;
|
mode?: RuntimeMode;
|
||||||
logging: LogOptions;
|
logging: LogOptions;
|
||||||
telemetry: AstroTelemetry;
|
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` */
|
/** `astro build` */
|
||||||
|
@ -42,6 +48,7 @@ class AstroBuilder {
|
||||||
private routeCache: RouteCache;
|
private routeCache: RouteCache;
|
||||||
private manifest: ManifestData;
|
private manifest: ManifestData;
|
||||||
private timer: Record<string, number>;
|
private timer: Record<string, number>;
|
||||||
|
private teardownCompiler: boolean;
|
||||||
|
|
||||||
constructor(settings: AstroSettings, options: BuildOptions) {
|
constructor(settings: AstroSettings, options: BuildOptions) {
|
||||||
if (options.mode) {
|
if (options.mode) {
|
||||||
|
@ -49,6 +56,7 @@ class AstroBuilder {
|
||||||
}
|
}
|
||||||
this.settings = settings;
|
this.settings = settings;
|
||||||
this.logging = options.logging;
|
this.logging = options.logging;
|
||||||
|
this.teardownCompiler = options.teardownCompiler ?? false;
|
||||||
this.routeCache = new RouteCache(this.logging);
|
this.routeCache = new RouteCache(this.logging);
|
||||||
this.origin = settings.config.site
|
this.origin = settings.config.site
|
||||||
? new URL(settings.config.site).origin
|
? new URL(settings.config.site).origin
|
||||||
|
@ -126,7 +134,7 @@ class AstroBuilder {
|
||||||
colors.dim(`Completed in ${getTimeStat(this.timer.init, performance.now())}.`)
|
colors.dim(`Completed in ${getTimeStat(this.timer.init, performance.now())}.`)
|
||||||
);
|
);
|
||||||
|
|
||||||
await staticBuild({
|
const opts: StaticBuildOptions = {
|
||||||
allPages,
|
allPages,
|
||||||
settings: this.settings,
|
settings: this.settings,
|
||||||
logging: this.logging,
|
logging: this.logging,
|
||||||
|
@ -135,9 +143,13 @@ class AstroBuilder {
|
||||||
origin: this.origin,
|
origin: this.origin,
|
||||||
pageNames,
|
pageNames,
|
||||||
routeCache: this.routeCache,
|
routeCache: this.routeCache,
|
||||||
|
teardownCompiler: this.teardownCompiler,
|
||||||
viteConfig,
|
viteConfig,
|
||||||
buildConfig,
|
buildConfig,
|
||||||
});
|
};
|
||||||
|
|
||||||
|
const { internals } = await viteBuild(opts);
|
||||||
|
await staticBuild(opts, internals);
|
||||||
|
|
||||||
// Write any additionally generated assets to disk.
|
// Write any additionally generated assets to disk.
|
||||||
this.timer.assetsStart = performance.now();
|
this.timer.assetsStart = performance.now();
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { teardown } from '@astrojs/compiler';
|
||||||
import * as eslexer from 'es-module-lexer';
|
import * as eslexer from 'es-module-lexer';
|
||||||
import glob from 'fast-glob';
|
import glob from 'fast-glob';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
@ -25,7 +26,7 @@ import { registerAllPlugins } from './plugins/index.js';
|
||||||
import type { PageBuildData, StaticBuildOptions } from './types';
|
import type { PageBuildData, StaticBuildOptions } from './types';
|
||||||
import { getTimeStat } from './util.js';
|
import { getTimeStat } from './util.js';
|
||||||
|
|
||||||
export async function staticBuild(opts: StaticBuildOptions) {
|
export async function viteBuild(opts: StaticBuildOptions) {
|
||||||
const { allPages, settings } = opts;
|
const { allPages, settings } = opts;
|
||||||
|
|
||||||
// Make sure we have an adapter before building
|
// Make sure we have an adapter before building
|
||||||
|
@ -98,6 +99,17 @@ export async function staticBuild(opts: StaticBuildOptions) {
|
||||||
|
|
||||||
settings.timer.end('Client build');
|
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) {
|
switch (settings.config.output) {
|
||||||
case 'static': {
|
case 'static': {
|
||||||
settings.timer.start('Static generate');
|
settings.timer.start('Static generate');
|
||||||
|
|
|
@ -39,6 +39,7 @@ export interface StaticBuildOptions {
|
||||||
pageNames: string[];
|
pageNames: string[];
|
||||||
routeCache: RouteCache;
|
routeCache: RouteCache;
|
||||||
viteConfig: InlineConfig;
|
viteConfig: InlineConfig;
|
||||||
|
teardownCompiler: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SingleFileBuiltModule {
|
export interface SingleFileBuiltModule {
|
||||||
|
|
8
pnpm-lock.yaml
generated
8
pnpm-lock.yaml
generated
|
@ -395,7 +395,7 @@ importers:
|
||||||
|
|
||||||
packages/astro:
|
packages/astro:
|
||||||
specifiers:
|
specifiers:
|
||||||
'@astrojs/compiler': ^1.1.0
|
'@astrojs/compiler': ^1.2.0
|
||||||
'@astrojs/language-server': ^0.28.3
|
'@astrojs/language-server': ^0.28.3
|
||||||
'@astrojs/markdown-remark': ^2.0.1
|
'@astrojs/markdown-remark': ^2.0.1
|
||||||
'@astrojs/telemetry': ^2.0.1
|
'@astrojs/telemetry': ^2.0.1
|
||||||
|
@ -485,7 +485,7 @@ importers:
|
||||||
yargs-parser: ^21.0.1
|
yargs-parser: ^21.0.1
|
||||||
zod: ^3.17.3
|
zod: ^3.17.3
|
||||||
dependencies:
|
dependencies:
|
||||||
'@astrojs/compiler': 1.1.0
|
'@astrojs/compiler': 1.2.0
|
||||||
'@astrojs/language-server': 0.28.3
|
'@astrojs/language-server': 0.28.3
|
||||||
'@astrojs/markdown-remark': link:../markdown/remark
|
'@astrojs/markdown-remark': link:../markdown/remark
|
||||||
'@astrojs/telemetry': link:../telemetry
|
'@astrojs/telemetry': link:../telemetry
|
||||||
|
@ -3956,8 +3956,8 @@ packages:
|
||||||
/@astrojs/compiler/0.31.4:
|
/@astrojs/compiler/0.31.4:
|
||||||
resolution: {integrity: sha512-6bBFeDTtPOn4jZaiD3p0f05MEGQL9pw2Zbfj546oFETNmjJFWO3nzHz6/m+P53calknCvyVzZ5YhoBLIvzn5iw==}
|
resolution: {integrity: sha512-6bBFeDTtPOn4jZaiD3p0f05MEGQL9pw2Zbfj546oFETNmjJFWO3nzHz6/m+P53calknCvyVzZ5YhoBLIvzn5iw==}
|
||||||
|
|
||||||
/@astrojs/compiler/1.1.0:
|
/@astrojs/compiler/1.2.0:
|
||||||
resolution: {integrity: sha512-C4kTwirys+HafufMqaxCbML2wqkGaXJM+5AekXh/v1IIOnMIdcEON9GBYsG6qa8aAmLhZ58aUZGPhzcA3Dx7Uw==}
|
resolution: {integrity: sha512-O8yPCyuq+PU9Fjht2tIW6WzSWiq8qDF1e8uAX2x+SOGFzKqOznp52UlDG2mSf+ekf0Z3R34sb64O7SgX+asTxg==}
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
/@astrojs/language-server/0.28.3:
|
/@astrojs/language-server/0.28.3:
|
||||||
|
|
Loading…
Add table
Reference in a new issue