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

perf - create a single extension context #8821

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/command/preview/cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,11 @@ export const previewCommand = new Command()
// see if we are serving a project or a file
if (Deno.statSync(file).isDirectory) {
// project preview
await serveProject(projectTarget, flags, args, {
const renderOptions = {
services: renderServices(notebookContext()),
flags,
};
await serveProject(projectTarget, renderOptions, args, {
port: options.port,
host: options.host,
browser: (options.browser === false || options.browse === false)
Expand Down
2 changes: 1 addition & 1 deletion src/command/render/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export async function renderProject(
context = await projectContextForDirectory(
context.dir,
context.notebookContext,
projectRenderConfig.options.flags,
projectRenderConfig.options,
);

// Validate that certain project properties haven't been mutated
Expand Down
8 changes: 2 additions & 6 deletions src/command/render/render-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import { renderProject } from "./project.ts";
import { renderFiles } from "./render-files.ts";
import { resourceFilesFromRenderedFile } from "./resources.ts";
import { RenderFlags, RenderOptions, RenderResult } from "./types.ts";
import {
fileExecutionEngine,
fileExecutionEngineAndTarget,
} from "../../execute/engine.ts";

import {
isProjectInputFile,
Expand Down Expand Up @@ -49,12 +45,12 @@ export async function render(
const nbContext = notebookContext();

// determine target context/files
let context = await projectContext(path, nbContext, options.flags);
let context = await projectContext(path, nbContext, options);

// if there is no project parent and an output-dir was passed, then force a project
if (!context && options.flags?.outputDir) {
// recompute context
context = await projectContextForDirectory(path, nbContext, options.flags);
context = await projectContextForDirectory(path, nbContext, options);

// force clean as --output-dir implies fully overwrite the target
options.forceClean = options.flags.clean !== false;
Expand Down
12 changes: 12 additions & 0 deletions src/core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { initializeLogger, logError, logOptions } from "../../src/core/log.ts";
import { Args } from "flags/mod.ts";
import { parse } from "flags/mod.ts";
import { exitWithCleanup } from "./cleanup.ts";
import {
captureFileReads,
reportPeformanceMetrics,
} from "./performance/metrics.ts";

type Runner = (args: Args) => Promise<unknown>;
export async function mainRunner(runner: Runner) {
Expand All @@ -24,6 +28,10 @@ export async function mainRunner(runner: Runner) {
Deno.addSignalListener("SIGTERM", abend);
}

if (Deno.env.get("QUARTO_REPORT_PERFORMANCE_METRICS") !== undefined) {
captureFileReads();
}

await runner(args);

// if profiling, wait for 10 seconds before quitting
Expand All @@ -33,6 +41,10 @@ export async function mainRunner(runner: Runner) {
await new Promise((resolve) => setTimeout(resolve, 10000));
}

if (Deno.env.get("QUARTO_REPORT_PERFORMANCE_METRICS") !== undefined) {
reportPeformanceMetrics();
}

exitWithCleanup(0);
} catch (e) {
if (e) {
Expand Down
24 changes: 24 additions & 0 deletions src/core/performance/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,33 @@

import { inputTargetIndexCacheMetrics } from "../../project/project-index.ts";

type FileReadRecord = {
path: string;
stack: string;
};

let fileReads: FileReadRecord[] | undefined = undefined;

export function captureFileReads() {
fileReads = [];

const originalReadTextFileSync = Deno.readTextFileSync;

Deno.readTextFileSync = function (path: string | URL) {
try {
throw new Error("File read");
} catch (e) {
const stack = e.stack!.split("\n").slice(2);
fileReads!.push({ path: String(path), stack });
}
return originalReadTextFileSync(path);
};
}

export function quartoPerformanceMetrics() {
return {
inputTargetIndexCache: inputTargetIndexCacheMetrics,
fileReads,
};
}

Expand Down
18 changes: 12 additions & 6 deletions src/project/project-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ import {
projectResolveFullMarkdownForFile,
projectVarsFile,
} from "./project-shared.ts";
import { RenderFlags, RenderServices } from "../command/render/types.ts";
import {
RenderFlags,
RenderOptions,
RenderServices,
} from "../command/render/types.ts";
import { kWebsite } from "./types/website/website-constants.ts";

import { readAndValidateYamlFromFile } from "../core/schema/validated-yaml.ts";
Expand All @@ -95,16 +99,18 @@ import { MappedString } from "../core/mapped-text.ts";
export async function projectContext(
path: string,
notebookContext: NotebookContext,
flags?: RenderFlags,
renderOptions?: RenderOptions,
force = false,
): Promise<ProjectContext | undefined> {
const flags = renderOptions?.flags;
let dir = normalizePath(
Deno.statSync(path).isDirectory ? path : dirname(path),
);
const originalDir = dir;

// create a shared extension context
const extensionContext = createExtensionContext();
// create an extension context if one doesn't exist
const extensionContext = renderOptions?.services.extension ||
createExtensionContext();

// first pass uses the config file resolve
const configSchema = await getProjectConfigSchema();
Expand Down Expand Up @@ -636,9 +642,9 @@ async function resolveLanguageTranslations(
export function projectContextForDirectory(
path: string,
notebookContext: NotebookContext,
flags?: RenderFlags,
renderOptions?: RenderOptions,
): Promise<ProjectContext> {
return projectContext(path, notebookContext, flags, true) as Promise<
return projectContext(path, notebookContext, renderOptions, true) as Promise<
ProjectContext
>;
}
Expand Down
19 changes: 14 additions & 5 deletions src/project/serve/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ import { htmlResourceResolverPostprocessor } from "../../project/types/website/w
import { inputFilesDir } from "../../core/render.ts";
import { kResources, kTargetFormat } from "../../config/constants.ts";
import { resourcesFromMetadata } from "../../command/render/resources.ts";
import { RenderFlags, RenderResult } from "../../command/render/types.ts";
import {
RenderFlags,
RenderOptions,
RenderResult,
} from "../../command/render/types.ts";
import {
kPdfJsInitialPath,
pdfJsBaseDir,
Expand Down Expand Up @@ -124,18 +128,23 @@ export const kRenderDefault = "default";

export async function serveProject(
target: string | ProjectContext,
flags: RenderFlags,
renderOptions: RenderOptions,
pandocArgs: string[],
options: ServeOptions,
noServe: boolean,
) {
let project: ProjectContext | undefined;
const nbContext = notebookContext();
let flags = renderOptions.flags;
const nbContext = renderOptions.services.notebook;
if (typeof target === "string") {
if (target === ".") {
target = Deno.cwd();
}
project = await projectContext(target, nbContext, flags);
project = await projectContext(
target,
nbContext,
renderOptions,
);
if (!project || !project?.config) {
throw new Error(`${target} is not a project`);
}
Expand Down Expand Up @@ -301,7 +310,7 @@ export async function serveProject(
project,
extensionDirs,
resourceFiles,
flags,
{ ...renderOptions, flags },
pandocArgs,
options,
!pdfOutput, // we don't render on reload for pdf output
Expand Down
8 changes: 5 additions & 3 deletions src/project/serve/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { projectContext } from "../../project/project-context.ts";

import { ProjectWatcher, ServeOptions } from "./types.ts";
import { httpDevServer } from "../../core/http-devserver.ts";
import { RenderFlags } from "../../command/render/types.ts";
import { RenderOptions } from "../../command/render/types.ts";
import { renderProject } from "../../command/render/project.ts";
import { render } from "../../command/render/render-shared.ts";
import { renderServices } from "../../command/render/render-services.ts";
Expand Down Expand Up @@ -53,17 +53,19 @@ export function watchProject(
project: ProjectContext,
extensionDirs: string[],
resourceFiles: string[],
flags: RenderFlags,
renderOptions: RenderOptions,
pandocArgs: string[],
options: ServeOptions,
renderingOnReload: boolean,
renderManager: ServeRenderManager,
stopServer: VoidFunction,
): Promise<ProjectWatcher> {
const nbContext = notebookContext();
const flags = renderOptions.flags;
// helper to refresh project config
const refreshProjectConfig = async () => {
project = (await projectContext(project.dir, nbContext, flags, false))!;
project =
(await projectContext(project.dir, nbContext, renderOptions, false))!;
};

// See if we're in draft mode
Expand Down
11 changes: 2 additions & 9 deletions src/quarto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,7 @@ import {
} from "cliffy/command/mod.ts";

import { commands } from "./command/command.ts";
import {
appendLogOptions,
cleanupLogger,
initializeLogger,
logError,
logOptions,
} from "./core/log.ts";
import { appendLogOptions } from "./core/log.ts";
import { debug } from "log/mod.ts";

import { cleanupSessionTempDir, initSessionTempDir } from "./core/temp.ts";
Expand All @@ -36,9 +30,8 @@ import {
reconfigureQuarto,
} from "./core/devconfig.ts";
import { typstBinaryPath } from "./core/typst.ts";
import { exitWithCleanup, onCleanup } from "./core/cleanup.ts";
import { onCleanup } from "./core/cleanup.ts";

import { parse } from "flags/mod.ts";
import { runScript } from "./command/run/run.ts";

// ensures run handlers are registered
Expand Down
Loading