Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/ui-layout-style-reuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/ui': patch
---

Improves rendering memory use by sharing default styles and reusing the computed style of unchanged elements
31 changes: 31 additions & 0 deletions packages/ui/src/layout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,37 @@ describe('Layout', () => {
}
});

it('reuses styles for unchanged elements and recomputes changed ones', async () => {
const document = createHTMLDocument(() => {});
const root = document.createElement('document');
const child = document.createElement('document');
child.style.paddingLeft = '2px';
child.appendChild(document.createTextNode('content'));
root.appendChild(child);
const layout = new Layout(root);
const term = await createTerm({ width: 80, height: 24 });
const result = term.render([...layout.layout()]);
const values = vi.spyOn(child.style, 'values', 'get');
const padded = (left: number) => [
open('DOCUMENT: 0'),
open('DOCUMENT: 1', { layout: { padding: { left } } }),
text('content'),
close(),
close(),
];

// Without replay info the unchanged child is laid out again from its last style.
expect([
...layout.layout({ changes: new Set([root]), info: { get: () => undefined } }),
]).toEqual(padded(2));
expect(values).not.toHaveBeenCalled();

child.style.paddingLeft = '4px';
expect([...layout.layout({ changes: new Set([root, child]), info: result.info })]).toEqual(
padded(4),
);
});

it('rebuilds elements when switching between flex and inline display', async () => {
const document = createHTMLDocument(() => {});
const root = document.createElement('document');
Expand Down
27 changes: 21 additions & 6 deletions packages/ui/src/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface LayoutOptions {
export class Layout {
#lastIdNumber = 0;
#stableIds = new WeakMap<Element, string>();
#styles = new WeakMap<Element, StyleValues>();
#root: Element;

constructor(root: Element) {
Expand Down Expand Up @@ -61,7 +62,7 @@ export class Layout {
yield text(content);
content = '';
}
const childStyle = computeStyle(child);
const childStyle = this.#style(child, options);
if (childStyle.display.value === 'inline') {
const inlineContent = collectInlineText(child);
if (inlineContent !== '') {
Expand Down Expand Up @@ -92,12 +93,19 @@ export class Layout {
return id ? info.get(id)?.replay : undefined;
}

/** Unchanged elements keep the style they were last opened with. */
#style(element: Element, { changes }: LayoutOptions): StyleValues {
const cached = changes?.has(element) ? undefined : this.#styles.get(element);
return cached ?? computeStyle(element);
}

#open(element: Element, style: StyleValues): Op {
let stableId = this.#stableIds.get(element);
if (!stableId) {
stableId = `${element.nodeName}: ${this.#lastIdNumber++}`;
this.#stableIds.set(element, stableId);
}
this.#styles.set(element, style);
return open(stableId, ttyContainerOptions(style));
}
}
Expand Down Expand Up @@ -199,11 +207,17 @@ function computeStyle(element: Element): StyleValues {
}

function computeDefaultStyles(element: Element): StyleValues {
const display =
element.namespaceURI === 'http://www.w3.org/1999/xhtml' && element.localName === 'span'
? 'inline'
: 'flex';
return {
return element.namespaceURI === 'http://www.w3.org/1999/xhtml' && element.localName === 'span'
? INLINE_DEFAULTS
: FLEX_DEFAULTS;
}

const FLEX_DEFAULTS = defaultStyles('flex');
const INLINE_DEFAULTS = defaultStyles('inline');

/** Defaults are shared, so they must never be mutated. */
function defaultStyles(display: StyleValues['display']['value']): StyleValues {
const styles: StyleValues = {
color: { value: 'currentColor', representation: 'currentColor' },
display: { value: display, representation: display },
flexDirection: { value: 'row', representation: 'row' },
Expand All @@ -219,6 +233,7 @@ function computeDefaultStyles(element: Element): StyleValues {
borderLeftWidth: { value: 1, representation: '1px' },
borderColor: { value: 'currentColor', representation: 'currentColor' },
};
return Object.freeze(styles);
}

function applyInlineStyles(element: Element, defaults: StyleValues): StyleValues {
Expand Down
Loading