Skip to content

Commit

Permalink
Teardown compiler to improve rendering perf (#6391)
Browse files Browse the repository at this point in the history
* Teardown compiler to improve rendering perf

* Upgrade compiler to minor

* Try hacky test guard

* Nicer teardown flow
  • Loading branch information
bluwy authored Mar 2, 2023
1 parent 0e378c3 commit 45501c5
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-forks-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Teardown compiler after Vite build to free up memory when rendering pages
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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': {
Expand Down
18 changes: 15 additions & 3 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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` */
Expand All @@ -42,13 +48,15 @@ class AstroBuilder {
private routeCache: RouteCache;
private manifest: ManifestData;
private timer: Record<string, number>;
private teardownCompiler: boolean;

constructor(settings: AstroSettings, options: BuildOptions) {
if (options.mode) {
this.mode = options.mode;
}
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
Expand Down Expand Up @@ -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,
Expand All @@ -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();
Expand Down
14 changes: 13 additions & 1 deletion packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/build/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface StaticBuildOptions {
pageNames: string[];
routeCache: RouteCache;
viteConfig: InlineConfig;
teardownCompiler: boolean;
}

export interface SingleFileBuiltModule {
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 45501c5

Please sign in to comment.