clava 0.7.4 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # clava
2
2
 
3
+ ## 0.7.5
4
+
5
+ ### Recipe output cache
6
+
7
+ Recipes created with [`cv`](https://clava.style/docs/reference/cv) now cache their output by prop values, so components that render again with the same props do less work. In the repository's benchmarks, which call a recipe with the same props each time, a call is about 2x faster for a plain recipe and 10x to 14x faster for an extended recipe whose [`refine`](https://clava.style/docs/reference/refine) callback or computed default changes a variant.
8
+
9
+ A later call with the same variant values, `class`, `className`, and `style` returns the cached output and does not resolve the variants again. A call skips the cache when one of these props holds an object or a function, except a `style` object of primitive values. A call that skips or misses the cache does more work than before, because it reads its props into a new object, and a miss also stores the result. The minified bundle is about 2.4 kB larger, or 1 kB gzipped.
10
+
11
+ Function variants, computed `defaultVariants`, [`refine`](https://clava.style/docs/reference/refine) callbacks, and the `transformClass` option of [`create`](https://clava.style/docs/reference/create) don't run again for a cached call. They must not have side effects, and their result must depend only on their arguments. If one of them reads a value that can change, such as a theme setting, pass that value as a variant prop instead.
12
+
3
13
  ## 0.7.4
4
14
 
5
15
  - Fixed [`RecipeProps`](https://clava.style/docs/reference/recipe-props) to accept Solid 2 `class` values (including class objects and arrays) and `style: false`, and exported [`ClassPropValue`](https://clava.style/docs/reference/class-prop-value), so props returned by [`splitProps`](https://clava.style/docs/reference/split-props) from `@solidjs/web` component props can be passed back into recipes.
package/README.md CHANGED
@@ -130,6 +130,8 @@ button.variantKeys;
130
130
 
131
131
  `propKeys` includes style props plus variant props for that mode. `variantKeys` includes only variant props.
132
132
 
133
+ Recipes cache their output by prop values. When the variant props, `class`, `className`, and `style` hold only strings, numbers, booleans, `null`, or `undefined`, or `style` is an object of those values, a later call with the same values returns the cached output without running [function variants](#function-variants), computed `defaultVariants`, [`refine`](#refine) callbacks, or the [`transformClass`](#create-and-cx) option again. Keep these functions free of side effects, and make their result depend only on their arguments. If one of them reads a value that can change, such as a theme setting, pass that value as a variant prop instead. Each call still returns a new object, so changing a returned style does not change later results.
134
+
133
135
  ## Classes And Styles
134
136
 
135
137
  `class` values are passed through `clsx`, so strings, arrays, nested arrays, and falsy values work the same way they do in `clsx`.
@@ -721,13 +723,13 @@ The package also exports `ClassValue`, `ClassPropValue`, `StyleValue`, `StyleCla
721
723
 
722
724
  ## Comparison
723
725
 
724
- This table compares Clava with the packages used by the repository's alternative benchmark: `class-variance-authority@0.7.1`, `cva@1.0.0-beta.8`, `tailwind-variants/lite@3.3.1`, and `tailwind-variants@3.3.1`.
726
+ This table compares Clava with the packages used by the repository's alternative benchmark: `class-variance-authority@0.7.1`, `cva@1.0.0-beta.12`, `tailwind-variants/lite@4.1.0`, and `tailwind-variants@4.1.0`.
725
727
 
726
728
  | Feature | Clava | CVA v0 | CVA v1 beta | TV Lite | TV |
727
729
  | ---------------------------------------------------- | -------------- | ------------------ | ------------------ | ------------------ | ------------------ |
728
730
  | Typed variants and default variants | Yes | Yes | Yes | Yes | Yes |
729
731
  | Boolean shorthand variants | Yes | No | No | No | No |
730
- | Recipe extension or composition | `extend` | No helper | `compose()` | `extend` | `extend` |
732
+ | Recipe extension or composition | `extend` | No helper | `composes` | `extend` | `extend` |
731
733
  | Cross-variant conditions | `refine()` | `compoundVariants` | `compoundVariants` | `compoundVariants` | `compoundVariants` |
732
734
  | Function variant values | Yes | No | No | No | No |
733
735
  | Computed default variants | Yes | No | No | No | No |
@@ -739,33 +741,19 @@ This table compares Clava with the packages used by the repository's alternative
739
741
 
740
742
  Clava has no built-in Tailwind conflict merging. [`create({ transformClass })`](#create-and-cx) routes every generated class string through a merger such as `tailwind-merge` when you need one.
741
743
 
742
- The `pnpm perf-alternatives` benchmark resolves a composed Tailwind-style button with inherited variants and defaults. It runs in two groups so each group compares equivalent work, since cross-variant conditions cost every library something different. On Node v24.18.0, Vitest reported these results, where higher ops/sec is better.
743
-
744
- The first group composes three layers with no cross-variant conditions. `class-variance-authority` has no compose helper, so its build merges the three layers into a single config.
744
+ The `pnpm perf-alternatives` benchmark resolves a Tailwind-style button composed from three layers (surface, interaction, and product) with inherited variants and defaults. This table shows its group with cross-variant conditions at the interaction and product layers, which Clava expresses with `refine` and every other library with `compoundVariants`. `class-variance-authority` has no compose helper, so its build merges the three layers into a single config. On Node v26.14.0, these results are the median of five Vitest runs, computed from mean latency, where higher ops/sec is better.
745
745
 
746
746
  | Package | Ops/sec | Relative to Clava |
747
747
  | -------------------------------- | --------: | ----------------: |
748
- | `clava@0.6.2` | 2,384,411 | 1.00x |
749
- | `class-variance-authority@0.7.1` | 1,872,892 | 1.27x slower |
750
- | `tailwind-variants@3.3.1` | 1,312,965 | 1.82x slower |
751
- | `tailwind-variants/lite@3.3.1` | 1,308,798 | 1.82x slower |
752
- | `cva@1.0.0-beta.8` | 408,730 | 5.83x slower |
753
-
754
- The second group adds the same cross-variant conditions at the interaction and product layers. Clava expresses them with `refine`, and every other library with `compoundVariants`.
755
-
756
- | Package | Ops/sec | Relative to Clava |
757
- | -------------------------------- | --------: | ----------------: |
758
- | `clava@0.6.2` | 1,335,204 | 1.00x |
759
- | `tailwind-variants/lite@3.3.1` | 568,960 | 2.35x slower |
760
- | `tailwind-variants@3.3.1` | 563,659 | 2.37x slower |
761
- | `class-variance-authority@0.7.1` | 500,557 | 2.67x slower |
762
- | `cva@1.0.0-beta.8` | 313,641 | 4.26x slower |
763
-
764
- Dividing one table by the other shows what each library charges for cross-variant conditions. `cva` charges the least at 1.30x, Clava sits at 1.79x, both `tailwind-variants` entry points are near 2.31x, and `class-variance-authority` charges the most at 3.74x. The exact ratios move by several percent between runs, so read the ordering rather than the digits, and reach for the group that matches your recipes rather than reading either table as the whole picture.
748
+ | `clava@0.7.5` | 5,348,894 | 1.00x |
749
+ | `tailwind-variants@4.1.0` | 2,989,982 | 1.79x slower |
750
+ | `cva@1.0.0-beta.12` | 1,713,297 | 3.12x slower |
751
+ | `tailwind-variants/lite@4.1.0` | 1,456,542 | 3.67x slower |
752
+ | `class-variance-authority@0.7.1` | 484,132 | 11.05x slower |
765
753
 
766
- Benchmark results vary by runtime and hardware, so treat them as a reproducible snapshot of this repository's composed-variant case rather than a universal ranking. Rows within a few percent of each other sit inside run-to-run noise, so their relative order is not meaningful. That covers the two `tailwind-variants` entry points, which land within a few percent in both groups and swap order between runs.
754
+ Benchmark results vary by runtime and hardware, so treat them as a reproducible snapshot of this repository's composed-variant case rather than a universal ranking. Rows within a few percent of each other sit inside run-to-run noise, so their relative order is not meaningful.
767
755
 
768
- Both groups resolve one fixed prop set on every iteration, so every package with an internal cache serves repeated calls from cache after warmup. Both `tailwind-variants` entry points cache the resolved result by variant props. Clava, `cva`, and `class-variance-authority` have no such cache, so those rows recompute on every iteration.
756
+ The benchmark resolves one fixed prop set on every iteration, so every package with an internal cache serves repeated calls from cache after warmup. Clava caches its whole output by prop values, so its row measures cache hits. Both `tailwind-variants` entry points cache the variant classes by variant props, but still do some work for compound variants and the `class` prop on every call. `cva` and `class-variance-authority` have no such cache, so those rows recompute on every iteration. For the cost of a Clava call that misses the cache and stores its result, see the `resolve recipe props with a new class each call` case in `pnpm bench`.
769
757
 
770
758
  ## API Summary
771
759
 
package/dist/index.js CHANGED
@@ -341,6 +341,72 @@ function warnRefineLimit({ creationFrame, unstableChanges }) {
341
341
  // src/index.ts
342
342
  const META_KEY = "__meta";
343
343
  const EMPTY_DEFAULTS = {};
344
+ const MAX_CACHED_RESULTS = 500;
345
+ const NEGATIVE_ZERO_STEP = Symbol("negative zero");
346
+ const STYLE_OBJECT_STEP = Symbol("style object");
347
+ function createResultCacheNode() {
348
+ return {
349
+ next: void 0,
350
+ result: void 0,
351
+ parent: void 0,
352
+ index: -1,
353
+ value: void 0
354
+ };
355
+ }
356
+ function pushResultCachePathSteps(node, steps) {
357
+ const parent = node.parent;
358
+ if (!parent) return;
359
+ pushResultCachePathSteps(parent, steps);
360
+ steps.push(node.index, node.value);
361
+ }
362
+ function isCacheableValue(value) {
363
+ const type = typeof value;
364
+ if (type === "object") return value === null;
365
+ return type !== "function" && type !== "symbol";
366
+ }
367
+ function getResultCacheChildren(node, slot, create) {
368
+ if (!node) return;
369
+ let next = node.next;
370
+ let children = next && slot < next.length ? next[slot] : void 0;
371
+ if (children || !create) return children;
372
+ children = /* @__PURE__ */ new Map();
373
+ if (!next) {
374
+ next = [];
375
+ node.next = next;
376
+ }
377
+ while (next.length < slot) next.push(void 0);
378
+ next[slot] = children;
379
+ return children;
380
+ }
381
+ function followResultCacheStep(children, step, create) {
382
+ if (!children) return;
383
+ const key = Object.is(step, -0) ? NEGATIVE_ZERO_STEP : step;
384
+ let child = children.get(key);
385
+ if (child || !create) return child;
386
+ child = createResultCacheNode();
387
+ children.set(key, child);
388
+ return child;
389
+ }
390
+ function followResultCacheStyleSteps(children, style, create) {
391
+ if (typeof style !== "object" || style === null) {
392
+ if (!isCacheableValue(style)) return null;
393
+ return followResultCacheStep(children, style, create);
394
+ }
395
+ let styleNode = followResultCacheStep(children, STYLE_OBJECT_STEP, create);
396
+ for (const name in style) {
397
+ if (!hasOwn(style, name)) continue;
398
+ const value = style[name];
399
+ if (!isCacheableValue(value)) return null;
400
+ styleNode = followResultCacheStep(getResultCacheChildren(followResultCacheStep(getResultCacheChildren(styleNode, 0, create), name, create), 0, create), value, create);
401
+ }
402
+ return styleNode;
403
+ }
404
+ function areCacheableResultSteps(steps) {
405
+ for (let i = 1; i < steps.length; i += 2) if (!isCacheableValue(steps[i])) return false;
406
+ return true;
407
+ }
408
+ const MAX_MASKED_OWN_KEYS = 16;
409
+ const HAS_OWN_CALLS_AFTER_MANY_KEYS = 64;
344
410
  function deduplicateExtensions(extensions, seen = /* @__PURE__ */ new Set()) {
345
411
  if (extensions.length === 0) return extensions;
346
412
  const result = [];
@@ -1030,8 +1096,7 @@ function create(params = {}) {
1030
1096
  if (!hasOwn(workingResolved, variantName)) continue;
1031
1097
  const selectedKey = getVariantValueKey(selectedValue);
1032
1098
  if (ownSkipValues && selectedKey != null && getOwn(ownSkipValues, variantName)?.has(selectedKey)) continue;
1033
- const fn = functionVariantFns[i];
1034
- const computedResult = fn(selectedValue);
1099
+ const computedResult = (0, functionVariantFns[i])(selectedValue);
1035
1100
  if (computedResult == null) continue;
1036
1101
  const r = extractClassAndStylePrebuilt(computedResult);
1037
1102
  if (r.class != null) classesOut.push(r.class);
@@ -1191,9 +1256,139 @@ function create(params = {}) {
1191
1256
  return {
1192
1257
  className: transformClass(clsx(allClasses)),
1193
1258
  style,
1194
- variants
1259
+ variants,
1260
+ cached: false
1195
1261
  };
1196
1262
  };
1263
+ const resultPropKeys = [];
1264
+ for (const key of variantKeys) {
1265
+ if (key === "class") continue;
1266
+ if (key === "className") continue;
1267
+ if (key === "style") continue;
1268
+ resultPropKeys.push(key);
1269
+ }
1270
+ resultPropKeys.push("class", "className");
1271
+ const resultPropKeysLength = resultPropKeys.length;
1272
+ const hasStyleVariant = variantKeys.includes("style");
1273
+ let resultCache = createResultCacheNode();
1274
+ let resultCacheSize = 0;
1275
+ const canMaskOwnProps = resultPropKeysLength < 32;
1276
+ const styleBit = canMaskOwnProps ? 1 << resultPropKeysLength : 0;
1277
+ let ownPropBits;
1278
+ let lastOwnKeys = EMPTY_KEYS;
1279
+ let lastOwnPropMask = 0;
1280
+ let hasOwnCallsLeft = 0;
1281
+ const getOwnPropMask = (props) => {
1282
+ const keys = Object.getOwnPropertyNames(props);
1283
+ const keysLength = keys.length;
1284
+ if (keysLength > MAX_MASKED_OWN_KEYS) {
1285
+ hasOwnCallsLeft = HAS_OWN_CALLS_AFTER_MANY_KEYS;
1286
+ return;
1287
+ }
1288
+ if (keysLength === lastOwnKeys.length) {
1289
+ let i = 0;
1290
+ while (i < keysLength && keys[i] === lastOwnKeys[i]) i++;
1291
+ if (i === keysLength) return lastOwnPropMask;
1292
+ }
1293
+ if (!ownPropBits) {
1294
+ ownPropBits = /* @__PURE__ */ new Map();
1295
+ for (let i = 0; i < resultPropKeysLength; i++) ownPropBits.set(resultPropKeys[i], 1 << i);
1296
+ ownPropBits.set("style", styleBit);
1297
+ }
1298
+ let mask = 0;
1299
+ for (let i = 0; i < keysLength; i++) mask |= ownPropBits.get(keys[i]) ?? 0;
1300
+ lastOwnKeys = keys;
1301
+ lastOwnPropMask = mask;
1302
+ return mask;
1303
+ };
1304
+ const followResultStyleSteps = (children, style, create) => {
1305
+ if (hasStyleVariant && typeof style === "object" && style !== null) return null;
1306
+ return followResultCacheStyleSteps(children, style, create);
1307
+ };
1308
+ const createResultPath = (steps, style) => {
1309
+ let node = resultCache;
1310
+ let firstIndex = 0;
1311
+ for (let i = 0; i < steps.length; i += 2) {
1312
+ const index = steps[i];
1313
+ const value = steps[i + 1];
1314
+ const parent = node;
1315
+ node = followResultCacheStep(getResultCacheChildren(node, index - firstIndex, true), value, true);
1316
+ if (node && !node.parent) {
1317
+ node.parent = parent;
1318
+ node.index = index;
1319
+ node.value = value;
1320
+ }
1321
+ firstIndex = index + 1;
1322
+ }
1323
+ if (style === void 0) return node;
1324
+ return followResultStyleSteps(getResultCacheChildren(node, resultPropKeysLength - firstIndex, true), style, true);
1325
+ };
1326
+ const getResultProps = (steps, style) => {
1327
+ const props = {};
1328
+ for (let i = 0; i < steps.length; i += 2) {
1329
+ const index = steps[i];
1330
+ props[resultPropKeys[index]] = steps[i + 1];
1331
+ }
1332
+ if (style !== void 0) props.style = style;
1333
+ return props;
1334
+ };
1335
+ const storeResult = (steps, style) => {
1336
+ if (typeof style === "object" && style !== null) style = { ...style };
1337
+ const result = computeResult(getResultProps(steps, style));
1338
+ result.style = { ...result.style };
1339
+ result.cached = true;
1340
+ if (resultCacheSize >= MAX_CACHED_RESULTS) {
1341
+ resultCache = createResultCacheNode();
1342
+ resultCacheSize = 0;
1343
+ }
1344
+ resultCacheSize++;
1345
+ const node = createResultPath(steps, style);
1346
+ if (node) node.result = result;
1347
+ return result;
1348
+ };
1349
+ const getResult = (props = EMPTY_DEFAULTS) => {
1350
+ const propsRecord = props;
1351
+ let node = resultCache;
1352
+ let firstIndex = 0;
1353
+ let steps;
1354
+ let ownPropMask;
1355
+ if (hasOwnCallsLeft > 0) hasOwnCallsLeft--;
1356
+ else if (canMaskOwnProps) ownPropMask = getOwnPropMask(propsRecord);
1357
+ for (let i = 0; i < resultPropKeysLength; i++) {
1358
+ const name = resultPropKeys[i];
1359
+ if (!(ownPropMask === void 0 ? hasOwn(propsRecord, name) : (ownPropMask & 1 << i) !== 0)) continue;
1360
+ const value = propsRecord[name];
1361
+ if (value === void 0) continue;
1362
+ if (steps) {
1363
+ steps.push(i, value);
1364
+ continue;
1365
+ }
1366
+ const child = isCacheableValue(value) ? followResultCacheStep(getResultCacheChildren(node, i - firstIndex, false), value, false) : void 0;
1367
+ if (child) {
1368
+ node = child;
1369
+ firstIndex = i + 1;
1370
+ continue;
1371
+ }
1372
+ steps = [];
1373
+ pushResultCachePathSteps(node, steps);
1374
+ steps.push(i, value);
1375
+ }
1376
+ const style = (ownPropMask === void 0 ? hasOwn(propsRecord, "style") : (ownPropMask & styleBit) !== 0) ? propsRecord.style : void 0;
1377
+ if (steps) {
1378
+ if (areCacheableResultSteps(steps) && followResultStyleSteps(void 0, style, false) !== null) return storeResult(steps, style);
1379
+ return computeResult(getResultProps(steps, style));
1380
+ }
1381
+ const end = style === void 0 ? node : followResultStyleSteps(getResultCacheChildren(node, resultPropKeysLength - firstIndex, false), style, false);
1382
+ const result = end?.result;
1383
+ if (result) return result;
1384
+ steps = [];
1385
+ pushResultCachePathSteps(node, steps);
1386
+ if (end === null) return computeResult(getResultProps(steps, style));
1387
+ return storeResult(steps, style);
1388
+ };
1389
+ const getOwnStyle = (result) => {
1390
+ return result.cached ? { ...result.style } : result.style;
1391
+ };
1197
1392
  const setDebugAttribute = (result, props, variants) => {
1198
1393
  result[DEBUG_ATTRIBUTE] = formatDebugAttribute({
1199
1394
  variantKeys,
@@ -1218,7 +1413,7 @@ function create(params = {}) {
1218
1413
  };
1219
1414
  const computedBaseClass = hasExtend ? clsx(extBaseClassesArr, baseClass) : baseClass;
1220
1415
  const classFn = (props = {}) => {
1221
- return computeResult(props).className;
1416
+ return getResult(props).className;
1222
1417
  };
1223
1418
  const meta = {
1224
1419
  identity,
@@ -1246,22 +1441,24 @@ function create(params = {}) {
1246
1441
  return recipe;
1247
1442
  };
1248
1443
  const defaultRecipe = ((props = {}) => {
1249
- const { className, style, variants } = computeResult(props);
1444
+ const computed = getResult(props);
1445
+ const { className, variants } = computed;
1250
1446
  const result = {
1251
1447
  class: className,
1252
- style
1448
+ style: getOwnStyle(computed)
1253
1449
  };
1254
1450
  if (debug) setDebugAttribute(result, props, variants);
1255
1451
  return result;
1256
1452
  });
1257
1453
  initRecipe(defaultRecipe, inputPropsKeys, (props = {}) => {
1258
- return computeResult(props).style;
1454
+ return getOwnStyle(getResult(props));
1259
1455
  });
1260
1456
  const jsxRecipe = ((props = {}) => {
1261
- const { className, style, variants } = computeResult(props);
1457
+ const computed = getResult(props);
1458
+ const { className, variants } = computed;
1262
1459
  const result = {
1263
1460
  className,
1264
- style
1461
+ style: getOwnStyle(computed)
1265
1462
  };
1266
1463
  if (debug) setDebugAttribute(result, props, variants);
1267
1464
  return result;
@@ -1270,9 +1467,9 @@ function create(params = {}) {
1270
1467
  "className",
1271
1468
  "style",
1272
1469
  ...variantKeys
1273
- ], (props = {}) => computeResult(props).style);
1470
+ ], (props = {}) => getOwnStyle(getResult(props)));
1274
1471
  const htmlRecipe = ((props = {}) => {
1275
- const { className, style, variants } = computeResult(props);
1472
+ const { className, style, variants } = getResult(props);
1276
1473
  const result = {
1277
1474
  class: className,
1278
1475
  style: styleValueToHTMLStyle(style)
@@ -1284,9 +1481,9 @@ function create(params = {}) {
1284
1481
  "class",
1285
1482
  "style",
1286
1483
  ...variantKeys
1287
- ], (props = {}) => styleValueToHTMLStyle(computeResult(props).style));
1484
+ ], (props = {}) => styleValueToHTMLStyle(getResult(props).style));
1288
1485
  const htmlObjRecipe = ((props = {}) => {
1289
- const { className, style, variants } = computeResult(props);
1486
+ const { className, style, variants } = getResult(props);
1290
1487
  const result = {
1291
1488
  class: className,
1292
1489
  style: styleValueToHTMLObjStyle(style)
@@ -1298,7 +1495,7 @@ function create(params = {}) {
1298
1495
  "class",
1299
1496
  "style",
1300
1497
  ...variantKeys
1301
- ], (props = {}) => styleValueToHTMLObjStyle(computeResult(props).style));
1498
+ ], (props = {}) => styleValueToHTMLObjStyle(getResult(props).style));
1302
1499
  defaultRecipe.jsx = jsxRecipe;
1303
1500
  defaultRecipe.html = htmlRecipe;
1304
1501
  defaultRecipe.htmlObj = htmlObjRecipe;