diff --git a/.changeset/gold-rings-switch.md b/.changeset/gold-rings-switch.md new file mode 100644 index 0000000000..619de8ba77 --- /dev/null +++ b/.changeset/gold-rings-switch.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/store": patch +--- + +Parallelized table codegen. Also put logs behind debug flag, which can be enabled using the `DEBUG=mud:*` environment variable. diff --git a/.changeset/plenty-guests-hunt.md b/.changeset/plenty-guests-hunt.md new file mode 100644 index 0000000000..ae00b2d0c3 --- /dev/null +++ b/.changeset/plenty-guests-hunt.md @@ -0,0 +1,5 @@ +--- +"@latticexyz/common": patch +--- + +Moved some codegen to use `fs/promises` for better parallelism. diff --git a/packages/common/src/codegen/debug.ts b/packages/common/src/codegen/debug.ts new file mode 100644 index 0000000000..ab1807cb00 --- /dev/null +++ b/packages/common/src/codegen/debug.ts @@ -0,0 +1,3 @@ +import { debug as parentDebug } from "../debug"; + +export const debug = parentDebug.extend("codegen"); diff --git a/packages/common/src/codegen/utils/formatAndWrite.ts b/packages/common/src/codegen/utils/formatAndWrite.ts index 23786bb584..af748d95d4 100644 --- a/packages/common/src/codegen/utils/formatAndWrite.ts +++ b/packages/common/src/codegen/utils/formatAndWrite.ts @@ -1,14 +1,15 @@ -import { mkdirSync, writeFileSync } from "fs"; +import { mkdir, writeFile } from "fs/promises"; import { dirname } from "path"; import { formatSolidity, formatTypescript } from "./format"; +import { debug } from "../debug"; export async function formatAndWriteSolidity(output: string, fullOutputPath: string, logPrefix: string): Promise { const formattedOutput = await formatSolidity(output); - mkdirSync(dirname(fullOutputPath), { recursive: true }); + await mkdir(dirname(fullOutputPath), { recursive: true }); - writeFileSync(fullOutputPath, formattedOutput); - console.log(`${logPrefix}: ${fullOutputPath}`); + await writeFile(fullOutputPath, formattedOutput); + debug(`${logPrefix}: ${fullOutputPath}`); } export async function formatAndWriteTypescript( @@ -18,8 +19,8 @@ export async function formatAndWriteTypescript( ): Promise { const formattedOutput = await formatTypescript(output); - mkdirSync(dirname(fullOutputPath), { recursive: true }); + await mkdir(dirname(fullOutputPath), { recursive: true }); - writeFileSync(fullOutputPath, formattedOutput); - console.log(`${logPrefix}: ${fullOutputPath}`); + await writeFile(fullOutputPath, formattedOutput); + debug(`${logPrefix}: ${fullOutputPath}`); } diff --git a/packages/store/ts/codegen/tablegen.ts b/packages/store/ts/codegen/tablegen.ts index 64c1fd9082..18d0bcfb56 100644 --- a/packages/store/ts/codegen/tablegen.ts +++ b/packages/store/ts/codegen/tablegen.ts @@ -4,23 +4,34 @@ import { getTableOptions } from "./tableOptions"; import { renderTable } from "./renderTable"; import { renderTypesFromConfig } from "./renderTypesFromConfig"; import { renderTableIndex } from "./renderTableIndex"; -import { rmSync } from "fs"; +import { rm } from "fs/promises"; import { StoreConfig } from "../config"; export async function tablegen(config: StoreConfig, outputBaseDirectory: string, remappings: [string, string][]) { const solidityUserTypes = loadAndExtractUserTypes(config.userTypes, outputBaseDirectory, remappings); const allTableOptions = getTableOptions(config, solidityUserTypes); - const uniqueTableDirectories = new Set(allTableOptions.map(({ outputPath }) => path.dirname(outputPath))); - for (const tableDir of uniqueTableDirectories) { - rmSync(path.join(outputBaseDirectory, tableDir), { recursive: true, force: true }); - } + const uniqueTableDirectories = Array.from(new Set(allTableOptions.map(({ outputPath }) => path.dirname(outputPath)))); + await Promise.all( + uniqueTableDirectories.map(async (tableDir) => { + await rm(path.join(outputBaseDirectory, tableDir), { recursive: true, force: true }); + }) + ); // write tables to files - for (const { outputPath, renderOptions } of allTableOptions) { - const fullOutputPath = path.join(outputBaseDirectory, outputPath); - const output = renderTable(renderOptions); - await formatAndWriteSolidity(output, fullOutputPath, "Generated table"); + await Promise.all( + allTableOptions.map(async ({ outputPath, renderOptions }) => { + const fullOutputPath = path.join(outputBaseDirectory, outputPath); + const output = renderTable(renderOptions); + await formatAndWriteSolidity(output, fullOutputPath, "Generated table"); + }) + ); + + // write table index + if (allTableOptions.length > 0) { + const fullOutputPath = path.join(outputBaseDirectory, config.codegenIndexFilename); + const output = renderTableIndex(allTableOptions); + await formatAndWriteSolidity(output, fullOutputPath, "Generated table index"); } // write types to file @@ -29,8 +40,4 @@ export async function tablegen(config: StoreConfig, outputBaseDirectory: string, const output = renderTypesFromConfig(config); await formatAndWriteSolidity(output, fullOutputPath, "Generated types file"); } - - const fullOutputPath = path.join(outputBaseDirectory, config.codegenIndexFilename); - const output = renderTableIndex(allTableOptions); - await formatAndWriteSolidity(output, fullOutputPath, "Generated table index"); }