Skip to content

Commit

Permalink
Merge branch 'perf-v3'
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Oct 28, 2024
2 parents 4d5576e + 6c3f36c commit c901131
Show file tree
Hide file tree
Showing 25 changed files with 321 additions and 147 deletions.
100 changes: 90 additions & 10 deletions package-lock.json

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

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@11ty/eleventy",
"version": "3.0.0",
"version": "3.0.1-alpha.1",
"description": "A simpler static site generator.",
"publishConfig": {
"access": "public",
Expand Down Expand Up @@ -117,7 +117,7 @@
"@11ty/dependency-tree-esm": "^1.0.0",
"@11ty/eleventy-dev-server": "^2.0.4",
"@11ty/eleventy-plugin-bundle": "^3.0.0",
"@11ty/eleventy-utils": "^1.0.3",
"@11ty/eleventy-utils": "2.0.0-alpha.2",
"@11ty/lodash-custom": "^4.17.21",
"@11ty/posthtml-urls": "^1.0.0",
"@11ty/recursive-copy": "^3.0.0",
Expand All @@ -144,7 +144,6 @@
"minimist": "^1.2.8",
"moo": "^0.5.2",
"node-retrieve-globals": "^6.0.0",
"normalize-path": "^3.0.0",
"nunjucks": "^3.2.4",
"please-upgrade-node": "^3.2.0",
"posthtml": "^0.16.6",
Expand Down
15 changes: 5 additions & 10 deletions src/Data/TemplateData.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class TemplateData {
return this.dataDir;
}

get _fsExistsCache() {
exists(pathname) {
// It's common for data files not to exist, so we avoid going to the FS to
// re-check if they do via a quick-and-dirty cache.
return this.eleventyConfig.existsCache;
return this.eleventyConfig.existsCache.exists(pathname);
}

setFileSystemSearch(fileSystemSearch) {
Expand Down Expand Up @@ -385,7 +385,7 @@ class TemplateData {
// Filter out files we know don't exist to avoid overhead for checking
const dataPaths = await Promise.all(
localDataPaths.map((path) => {
if (this._fsExistsCache.exists(path)) {
if (this.exists(path)) {
return path;
}
return false;
Expand Down Expand Up @@ -495,12 +495,7 @@ class TemplateData {

if (extension === "js" || extension === "cjs" || extension === "mjs") {
// JS data file or require’d JSON (no preprocessing needed)
let localPath = TemplatePath.absolutePath(path);
let exists = this._fsExistsCache.exists(localPath);
// Make sure that relative lookups benefit from cache
this._fsExistsCache.markExists(path, exists);

if (!exists) {
if (!this.exists(path)) {
return {};
}

Expand All @@ -515,7 +510,7 @@ class TemplateData {
}

// We always need to use `import()`, as `require` isn’t available in ESM.
let returnValue = await EleventyImport(localPath, type);
let returnValue = await EleventyImport(path, type);

// TODO special exception for Global data `permalink.js`
// module.exports = (data) => `${data.page.filePathStem}/`; // Does not work
Expand Down
4 changes: 2 additions & 2 deletions src/Engines/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class JavaScript extends TemplateEngine {
}

if (inst?.render) {
return function (data = {}) {
return (data = {}) => {
// TODO does this do anything meaningful for non-classes?
// `inst` should have a normalized `render` function from _getInstance

Expand All @@ -225,7 +225,7 @@ class JavaScript extends TemplateEngine {
Object.assign(inst, this.getJavaScriptFunctions(inst));

return this.normalize(inst.render.call(inst, data));
}.bind(this);
};
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/Plugins/RenderPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ async function compileFile(inputPath, options = {}, templateLang) {
throw new Error("Missing file path argument passed to the `renderFile` shortcode.");
}

if (!fs.existsSync(TemplatePath.normalizeOperatingSystemFilePath(inputPath))) {
throw new Error(
"Could not find render plugin file for the `renderFile` shortcode, looking for: " + inputPath,
);
}

let wasTemplateConfigMissing = false;
if (!templateConfig) {
templateConfig = new TemplateConfig(null, false);
Expand All @@ -77,6 +71,14 @@ async function compileFile(inputPath, options = {}, templateLang) {
await templateConfig.init();
}

let normalizedPath = TemplatePath.normalizeOperatingSystemFilePath(inputPath);
// Prefer the exists cache, if it’s available
if (!templateConfig.existsCache.exists(normalizedPath)) {
throw new Error(
"Could not find render plugin file for the `renderFile` shortcode, looking for: " + inputPath,
);
}

let tr = new TemplateRender(inputPath, templateConfig);
tr.extensionMap = extensionMap;

Expand Down
30 changes: 19 additions & 11 deletions src/Template.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import util from "node:util";
import os from "node:os";
import path from "node:path";
import fs from "node:fs";

import fs from "graceful-fs";
import lodash from "@11ty/lodash-custom";
import { DateTime } from "luxon";
import { TemplatePath, isPlainObject } from "@11ty/eleventy-utils";
Expand All @@ -25,6 +25,7 @@ import TemplateContentUnrenderedTemplateError from "./Errors/TemplateContentUnre
import EleventyBaseError from "./Errors/EleventyBaseError.js";
import ReservedData from "./Util/ReservedData.js";
import TransformsUtil from "./Util/TransformsUtil.js";
import { FileSystemManager } from "./Util/FileSystemManager.js";

const { set: lodashSet, get: lodashGet } = lodash;
const fsStat = util.promisify(fs.stat);
Expand All @@ -33,6 +34,8 @@ const debug = debugUtil("Eleventy:Template");
const debugDev = debugUtil("Dev:Eleventy:Template");

class Template extends TemplateContent {
#fsManager;

constructor(templatePath, templateData, extensionMap, config) {
debugDev("new Template(%o)", templatePath);
super(templatePath, config);
Expand Down Expand Up @@ -67,8 +70,11 @@ class Template extends TemplateContent {
this.templateData = templateData;
}

get existsCache() {
return this.eleventyConfig.existsCache;
get fsManager() {
if (!this.#fsManager) {
this.#fsManager = new FileSystemManager(this.eleventyConfig);
}
return this.#fsManager;
}

get logger() {
Expand Down Expand Up @@ -808,12 +814,12 @@ class Template extends TemplateContent {
let templateBenchmarkDir = this.bench.get("Template make parent directory");
templateBenchmarkDir.before();

let templateOutputDir = path.parse(outputPath).dir;
if (templateOutputDir) {
if (!this.existsCache.exists(templateOutputDir)) {
fs.mkdirSync(templateOutputDir, { recursive: true });
}
if (this.eleventyConfig.templateHandling?.writeMode === "async") {
await this.fsManager.createDirectoryForFile(outputPath);
} else {
this.fsManager.createDirectoryForFileSync(outputPath);
}

templateBenchmarkDir.after();

if (!Buffer.isBuffer(finalContent) && typeof finalContent !== "string") {
Expand All @@ -825,9 +831,11 @@ class Template extends TemplateContent {
let templateBenchmark = this.bench.get("Template Write");
templateBenchmark.before();

// Note: This deliberately uses the synchronous version to avoid
// unbounded concurrency: https://github.com/11ty/eleventy/issues/3271
fs.writeFileSync(outputPath, finalContent);
if (this.eleventyConfig.templateHandling?.writeMode === "async") {
await this.fsManager.writeFile(outputPath, finalContent);
} else {
this.fsManager.writeFileSync(outputPath, finalContent);
}

templateBenchmark.after();
this.writeCount++;
Expand Down
1 change: 0 additions & 1 deletion src/TemplateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ class TemplateConfig {
get existsCache() {
if (!this._existsCache) {
this._existsCache = new ExistsCache();
this._existsCache.setDirectoryCheck(true);
}
return this._existsCache;
}
Expand Down
2 changes: 1 addition & 1 deletion src/TemplateLayoutPathResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TemplateLayoutPathResolver {
return true;
}
let fullPath = this.eleventyConfig.directories.getLayoutPath(layoutPath);
if (fs.existsSync(fullPath)) {
if (this.eleventyConfig.existsCache.exists(fullPath)) {
return true;
}
return false;
Expand Down
6 changes: 1 addition & 5 deletions src/TemplatePassthrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const debug = debugUtil("Eleventy:TemplatePassthrough");
class TemplatePassthroughError extends EleventyBaseError {}

class TemplatePassthrough {
#isExistsCache = {};
#isDirectoryCache = {};

constructor(path, eleventyConfig) {
Expand Down Expand Up @@ -156,10 +155,7 @@ class TemplatePassthrough {
}

isExists(dir) {
if (this.#isExistsCache[dir] === undefined) {
this.#isExistsCache[dir] = fs.existsSync(dir);
}
return this.#isExistsCache[dir];
return this.eleventyConfig.existsCache.exists(dir);
}

isDirectory(dir) {
Expand Down
Loading

0 comments on commit c901131

Please sign in to comment.