Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

Path performance, validate only changed styles (#1600)

Ivan Starkova7f0a807a7fe
diff --git a/apps/builder/app/routes/rest.patch.ts b/apps/builder/app/routes/rest.patch.ts
index f163387961..c30cd781ce 100644
--- a/apps/builder/app/routes/rest.patch.ts
+++ b/apps/builder/app/routes/rest.patch.ts
@@ -77,6 +77,9 @@ export const action = async ({ request }: ActionArgs) => {
const skipValidation = true;
+ // Used to optimize by validating only changed styles, as they accounted for 99% of validation time
+ const patchedStyleDeclKeysSet = new Set<string>();
+
for await (const transaction of transactions) {
for await (const change of transaction.changes) {
const { namespace, patches } = change;
@@ -123,6 +126,11 @@ export const action = async ({ request }: ActionArgs) => {
}
if (namespace === "styles") {
+ // It's somehow implementation detail leak, as we use the fact that styles patches has ids in path
+ for (const patch of patches) {
+ patchedStyleDeclKeysSet.add(`${patch.path[0]}`);
+ }
+
const styles =
buildData.styles ?? parseStyles(build.styles, skipValidation);
buildData.styles = applyPatches(styles, patches);
@@ -163,19 +171,23 @@ export const action = async ({ request }: ActionArgs) => {
// parse with zod before serialization to avoid saving invalid data
dbBuildData.pages = serializePages(Pages.parse(buildData.pages));
}
+
if (buildData.breakpoints) {
dbBuildData.breakpoints = serializeBreakpoints(
Breakpoints.parse(buildData.breakpoints)
);
}
+
if (buildData.instances) {
dbBuildData.instances = serializeInstances(
Instances.parse(buildData.instances)
);
}
+
if (buildData.props) {
dbBuildData.props = serializeProps(Props.parse(buildData.props));
}
+
if (buildData.styleSources) {
dbBuildData.styleSources = serializeStyleSources(
StyleSources.parse(buildData.styleSources)
@@ -186,9 +198,25 @@ export const action = async ({ request }: ActionArgs) => {
StyleSourceSelections.parse(buildData.styleSourceSelections)
);
}
+
if (buildData.styles) {
- dbBuildData.styles = serializeStyles(Styles.parse(buildData.styles));
+ // Optimize by validating only changed styles, as they accounted for 99% of validation time
+ const stylesToValidate: Styles = new Map();
+ for (const styleId of patchedStyleDeclKeysSet) {
+ const style = buildData.styles.get(styleId);
+ // In case of deletion style could be undefined
+ if (style === undefined) {
+ continue;
+ }
+
+ stylesToValidate.set(styleId, style);
+ }
+
+ Styles.parse(stylesToValidate);
+
+ dbBuildData.styles = serializeStyles(buildData.styles);
}
+
// check any build data is changed because only assets could change
if (Object.keys(dbBuildData).length > 0) {
await prisma.build.update({
diff --git a/packages/prisma-client/src/prisma.ts b/packages/prisma-client/src/prisma.ts
index 36fa92e5ed..37cc22c787 100644
--- a/packages/prisma-client/src/prisma.ts
+++ b/packages/prisma-client/src/prisma.ts
@@ -52,7 +52,7 @@ prisma.$on("query", (e) => {
);
// eslint-disable-next-line no-console
- console.log("Params: " + e.params);
+ console.log("Params: " + e.params.slice(0, 100));
// eslint-disable-next-line no-console
console.log("Duration: " + e.duration + "ms");
});