Skip to content

Add cache for tailwind-variants #13018

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

Merged
merged 4 commits into from
May 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion MODULE.bazel.lock

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

2 changes: 1 addition & 1 deletion app/gui/src/dashboard/components/dashboard/AssetRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ export function RealAssetRow(props: RealAssetRowProps) {
}
}}
className={tailwindMerge.twMerge(
'h-table-row rounded-full transition-all ease-in-out rounded-rows-child [contain-intrinsic-size:44px] [content-visibility:auto]',
'h-table-row rounded-full transition-all ease-in-out rounded-rows-child',
visibility,
(isDraggedOver || isSelected) && 'selected',
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const COLUMN_SHOW_TEXT_ID: Readonly<Record<Column, text.TextId>> = {
} satisfies { [C in Column]: `${C}ColumnShow` }

const COLUMN_CSS_CLASSES =
'max-w-96 text-left bg-clip-padding last:border-r-0 last:rounded-r-full last:w-full'
'max-w-96 text-left bg-clip-padding last:border-r-0 last:rounded-r-full last:w-full [contain-intrinsic-size:44px] [content-visibility:auto]'
const NORMAL_COLUMN_CSS_CLASSES = `px-cell-x py ${COLUMN_CSS_CLASSES}`

/** CSS classes for every column. */
Expand Down
48 changes: 48 additions & 0 deletions app/gui/src/dashboard/utilities/LruCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/** @file A simple LRU cache. */
/**
* A simple LRU cache.
*/
export class LRUCache<K, V> {
private readonly cache: Map<K, V>
private readonly maxSize: number

/**
* Create a new LRU cache.
*/
constructor(maxSize: number) {
this.cache = new Map()
this.maxSize = maxSize
}

/**
* Get a value from the cache.
*/
get(key: K): V | undefined {
const value = this.cache.get(key)
if (value != null) {
this.cache.delete(key)
this.cache.set(key, value)
}
return value
}

/**
* Set a value in the cache.
*/
set(key: K, value: V) {
if (this.cache.size >= this.maxSize) {
const oldestKey = this.cache.keys().next().value
if (oldestKey != null) {
this.cache.delete(oldestKey)
}
}
this.cache.set(key, value)
}

/**
* Clear the cache.
*/
clear() {
this.cache.clear()
}
}
70 changes: 69 additions & 1 deletion app/gui/src/dashboard/utilities/tailwindVariants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,80 @@ import type { OmitUndefined } from 'tailwind-variants'
import { createTV } from 'tailwind-variants'

import { TAILWIND_MERGE_CONFIG } from '#/utilities/tailwindMerge'
import { LRUCache } from './LruCache'

export * from 'tailwind-variants'

const MAX_CACHE_SIZE = 256

// eslint-disable-next-line no-restricted-syntax
const tvConstructor = createTV({ twMergeConfig: TAILWIND_MERGE_CONFIG })

// This is a function, even though it does not contain function syntax.
// eslint-disable-next-line no-restricted-syntax
export const tv = createTV({ twMergeConfig: TAILWIND_MERGE_CONFIG })
export const tv = function tvWithLRU(construct: Parameters<typeof tvConstructor>[0]) {
const cache = new LRUCache<string, unknown>(MAX_CACHE_SIZE)
/**
* Get a cache key for a given set of arguments.
*/
function getCacheKey(props: Parameters<typeof baseVariants>[0]) {
return JSON.stringify(props)
}

const baseVariants = tvConstructor(construct)

// We use proxy here because `tailwind-variants` returns a function with extra properties.
// and we want to preserve these properties.
const withLRU = new Proxy(baseVariants, {
apply: (target, _thisArg, argArray: unknown[]) => {
const cacheKey = getCacheKey(argArray)
const cached = cache.get(cacheKey)

if (cached != null) {
return cached
}

const result: unknown = target(...argArray)

if (typeof result === 'object' && result != null) {
for (const slot in result) {
// eslint-disable-next-line no-restricted-syntax
const value = result[slot as keyof typeof result]

if (typeof value === 'function') {
const slotCachePrefix = slot + cacheKey
/**
* Wrap a slot function with a cache.
*/
// @ts-expect-error - This is a valid assignment.
result[slot] = function withSlotCache(props: Parameters<typeof value>[0]) {
const slotCacheKey = slotCachePrefix + getCacheKey(props)
const slotCache = cache.get(slotCacheKey)

if (slotCache != null) {
return slotCache
}

// @ts-expect-error - This is a valid assignment.
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const classes = value(props)

cache.set(slotCacheKey, classes)

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return classes
}
}
}
}

cache.set(cacheKey, result)
return result
},
})

return withLRU
} as typeof tvConstructor

/** Extract function signatures from a type. */
export type ExtractFunction<T> =
Expand Down
Loading