Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: try to cache computed styles at least within updates #4461

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions apps/builder/app/builder/features/style-panel/shared/model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,26 @@ export const $availableColorVariables = computed(
availableVariables.filter((value) => value.fallback?.type !== "unit")
);

// completely bust the cache when model is changed
let prevModel: undefined | StyleObjectModel;
// store cached computed declarations by following key
// instanceSelector + styleSourceId + state + property
const cache = new Map<string, ComputedStyleDecl>();

const validateCache = (model: StyleObjectModel) => {
if (model !== prevModel) {
prevModel = model;
cache.clear();
}
};

export const createComputedStyleDeclStore = (property: StyleProperty) => {
return computed(
[$model, $instanceAndRootSelector, $selectedOrLastStyleSourceSelector],
(model, instanceSelector, styleSourceSelector) => {
validateCache(model);
return getComputedStyleDecl({
cache,
model,
instanceSelector,
styleSourceId: styleSourceSelector?.styleSourceId,
Expand All @@ -306,10 +321,6 @@ export const createComputedStyleDeclStore = (property: StyleProperty) => {
);
};

export const useStyleObjectModel = () => {
return useStore($model);
};

export const useComputedStyleDecl = (property: StyleProperty) => {
const $store = useMemo(
() => createComputedStyleDeclStore(property),
Expand All @@ -324,7 +335,9 @@ export const useParentComputedStyleDecl = (property: StyleProperty) => {
computed(
[$model, $instanceAndRootSelector],
(model, instanceSelector) => {
validateCache(model);
return getComputedStyleDecl({
cache,
model,
instanceSelector: instanceSelector?.slice(1),
property,
Expand Down
27 changes: 27 additions & 0 deletions apps/builder/app/shared/style-object-model.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1576,3 +1576,30 @@ describe("style value source", () => {
});
});
});

describe("cache", () => {
test("reuse computed values", () => {
const model = createModel({
css: `
bodyLocal {
color: red;
}
`,
jsx: <$.Body ws:id="body" ws:tag="body" class="bodyLocal"></$.Body>,
});
const cache = new Map();
const first = getComputedStyleDecl({
cache,
model,
instanceSelector: ["body"],
property: "color",
});
const second = getComputedStyleDecl({
cache,
model,
instanceSelector: ["body"],
property: "color",
});
expect(first).toBe(second);
});
});
23 changes: 22 additions & 1 deletion apps/builder/app/shared/style-object-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,15 @@ export type ComputedStyleDecl = {
* https://drafts.csswg.org/css-cascade-5/#value-stages
*/
export const getComputedStyleDecl = ({
cache = new Map(),
model,
instanceSelector = [],
styleSourceId,
state,
property,
customPropertiesGraph = new Map(),
}: {
cache?: Map<string, ComputedStyleDecl>;
model: StyleObjectModel;
instanceSelector?: InstanceSelector;
styleSourceId?: StyleDecl["styleSourceId"];
Expand All @@ -302,6 +304,17 @@ export const getComputedStyleDecl = ({
*/
customPropertiesGraph?: Map<Instance["id"], Set<Property>>;
}): ComputedStyleDecl => {
const cacheKey = JSON.stringify({
instanceSelector,
styleSourceId,
state,
property,
});
const cachedValue = cache.get(cacheKey);
if (cachedValue) {
return cachedValue;
}

const isCustomProperty = property.startsWith("--");
const propertyData = isCustomProperty
? customPropertyData
Expand Down Expand Up @@ -431,5 +444,13 @@ export const getComputedStyleDecl = ({
// fallback to inherited value
cascadedValue ??= computedValue;

return { property, source, cascadedValue, computedValue, usedValue };
const computedStyleDecl: ComputedStyleDecl = {
property,
source,
cascadedValue,
computedValue,
usedValue,
};
cache.set(cacheKey, computedStyleDecl);
return computedStyleDecl;
};