Skip to content

Commit

Permalink
perf(code-gen): init js validator objects with known keys
Browse files Browse the repository at this point in the history
We don't accept dynamic keys at all, so there is no reason to keep `Object.create(null)`. By also pre-initializing the object with all known keys, we keep the shape of the object always the same. This gives a 10-30% improvement when validating structures
  • Loading branch information
dirkdev98 committed May 31, 2023
1 parent 6956723 commit 47259fb
Show file tree
Hide file tree
Showing 6 changed files with 1,145 additions and 218 deletions.
14 changes: 7 additions & 7 deletions benchmark/validate.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ async function main(logger) {
const baseDir = pathJoin(testTemporaryDirectory, `./bench-validate/`);

const generator = new Generator(logger);
generator.addStructure("./packages/code-gen/src/experimental/generated");
generator.addStructure("./packages/code-gen/src/generated");

generator.generate({
targetLanguage: "js",
Expand All @@ -22,8 +22,8 @@ async function main(logger) {
outputDirectory: baseDir,
});

const { validateExperimentalStructure } = await import(
pathJoin(process.cwd(), baseDir, "./experimental/validators.js")
const { validateStructureStructure } = await import(
pathJoin(process.cwd(), baseDir, "./structure/validators.js")
);

const structures = [
Expand All @@ -35,7 +35,7 @@ async function main(logger) {
),
JSON.parse(
readFileSync(
"./packages/code-gen/src/experimental/generated/common/structure.json",
"./packages/code-gen/src/generated/common/structure.json",
"utf-8",
),
),
Expand All @@ -45,7 +45,7 @@ async function main(logger) {

for (let i = 0; i < 100; ++i) {
for (const str of structures) {
const { error } = validateExperimentalStructure(str);
const { error } = validateStructureStructure(str);
if (error) {
throw AppError.validationError("validator.error", error);
}
Expand All @@ -56,13 +56,13 @@ async function main(logger) {

bench("store structure", (b) => {
for (let i = 0; i < b.N; ++i) {
validateExperimentalStructure(structures[0]);
validateStructureStructure(structures[0]);
}
});

bench("code-gen experimental structure", (b) => {
for (let i = 0; i < b.N; ++i) {
validateExperimentalStructure(structures[1]);
validateStructureStructure(structures[1]);
}
});

Expand Down
64 changes: 53 additions & 11 deletions packages/cli/src/generated/cli/validators.js

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

Loading

0 comments on commit 47259fb

Please sign in to comment.