Skip to content

Commit

Permalink
feat(store): parallelize table codegen (#1754)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Oct 12, 2023
1 parent 7fa2ca1 commit f62c767
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-rings-switch.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions .changeset/plenty-guests-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@latticexyz/common": patch
---

Moved some codegen to use `fs/promises` for better parallelism.
3 changes: 3 additions & 0 deletions packages/common/src/codegen/debug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { debug as parentDebug } from "../debug";

export const debug = parentDebug.extend("codegen");
15 changes: 8 additions & 7 deletions packages/common/src/codegen/utils/formatAndWrite.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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(
Expand All @@ -18,8 +19,8 @@ export async function formatAndWriteTypescript(
): Promise<void> {
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}`);
}
33 changes: 20 additions & 13 deletions packages/store/ts/codegen/tablegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
}

0 comments on commit f62c767

Please sign in to comment.