Skip to content

Commit d2c2e1d

Browse files
authored
Speed up recipe sync (#610)
* Do not re-create merkle ref every time we try to load a recipe We already know the ID! * Refactor addRecipe to registerNewRecipe and registerRecipe Be clear about when we do and do not wish to generate a reference * Export registerNewRecipe
1 parent f9b078d commit d2c2e1d

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

charm/src/iterate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { addRecipe, Cell, EntityId } from "@commontools/runner";
1+
import { registerNewRecipe, Cell, EntityId } from "@commontools/runner";
22
import { LLMClient } from "@commontools/llm-client";
33
import { createJsonSchema, JSONSchema } from "@commontools/builder";
44

@@ -178,7 +178,7 @@ export async function compileRecipe(
178178
return;
179179
}
180180
const parentsIds = parents?.map((id) => id.toString());
181-
addRecipe(recipe, recipeSrc, spec, parentsIds);
181+
registerNewRecipe(recipe, recipeSrc, spec, parentsIds);
182182
return recipe;
183183
}
184184

runner/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export {
3333
getEntityId,
3434
} from "./doc-map.ts";
3535
export {
36-
addRecipe,
36+
registerRecipe,
37+
registerNewRecipe,
3738
allRecipesByName,
3839
getRecipe,
3940
getRecipeId,

runner/src/recipe-map.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,30 @@ const srcById = new Map<string, string>();
99
const specById = new Map<string, string>();
1010
const parentsById = new Map<string, string[]>();
1111

12-
export function addRecipe(
13-
recipe: Recipe | Module,
12+
export function registerNewRecipe(
13+
recipe: Recipe,
1414
src?: string,
1515
spec?: string,
16-
parents?: string[],
16+
parents?: string[]
1717
): string {
1818
if (idByRecipe.has(recipe)) return idByRecipe.get(recipe)!;
1919

2020
const id = src
2121
? createRef(src, "recipe source").toString()
2222
: createRef(recipe, "recipe").toString();
23+
24+
return registerRecipe(id, recipe, src, spec, parents);
25+
}
26+
27+
export function registerRecipe(
28+
id: string,
29+
recipe: Recipe,
30+
src?: string,
31+
spec?: string,
32+
parents?: string[]
33+
): string {
34+
if (idByRecipe.has(recipe)) return idByRecipe.get(recipe)!;
35+
2336
recipeById.set(id, recipe);
2437
idByRecipe.set(recipe, id);
2538

runner/src/recipe-sync.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {
2-
addRecipe,
2+
registerRecipe,
33
getRecipe,
44
getRecipeName,
55
getRecipeParents,
@@ -49,11 +49,8 @@ export async function syncRecipeBlobby(id: string) {
4949
const { recipe, errors } = await buildRecipe(src);
5050
if (errors) throw new Error(errors);
5151

52-
const recipeId = addRecipe(recipe!, src, spec, parents);
53-
if (id !== recipeId) {
54-
throw new Error(`Recipe ID mismatch: ${id} !== ${recipeId}`);
55-
}
56-
recipesKnownToStorage.add(recipeId);
52+
registerRecipe(id, recipe!, src, spec, parents);
53+
recipesKnownToStorage.add(id);
5754
}
5855

5956
function saveRecipe(

runner/src/runner.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
import { getModuleByRef } from "./module.ts";
3939
import { type AddCancel, type Cancel, useCancelGroup } from "./cancel.ts";
4040
import "./builtins/index.ts";
41-
import { addRecipe, getRecipe, getRecipeId } from "./recipe-map.ts";
41+
import { registerRecipe, getRecipe, getRecipeId, registerNewRecipe } from "./recipe-map.ts";
4242
import { isCell } from "./cell.ts";
4343
import { isQueryResultForDereferencing } from "./query-result-proxy.ts";
4444
import { getDocLinkOrThrow } from "./query-result-proxy.ts";
@@ -138,7 +138,7 @@ export function run<T, R = any>(
138138
recipe = recipeOrModule as Recipe;
139139
}
140140

141-
recipeId ??= addRecipe(recipe);
141+
recipeId ??= registerNewRecipe(recipe);
142142

143143
if (cancels.has(resultCell)) {
144144
// If it's already running and no new recipe or argument are given,
@@ -175,10 +175,10 @@ export function run<T, R = any>(
175175
const ref = isDocLink(argument)
176176
? argument
177177
: isCell(argument)
178-
? argument.getAsDocLink()
179-
: isQueryResultForDereferencing(argument)
180-
? getDocLinkOrThrow(argument)
181-
: ({ cell: argument, path: [] } satisfies DocLink);
178+
? argument.getAsDocLink()
179+
: isQueryResultForDereferencing(argument)
180+
? getDocLinkOrThrow(argument)
181+
: ({ cell: argument, path: [] } satisfies DocLink);
182182

183183
// Get value, but just to get the keys. Throw if it isn't an object.
184184
const value = ref.cell.getAsQueryResult(ref.path);
@@ -464,11 +464,11 @@ function instantiateJavaScriptNode(
464464
resultRecipe,
465465
undefined,
466466
resultCell ??
467-
getDoc(
468-
undefined,
469-
{ resultFor: { inputs, outputs, fn: fn.toString() } },
470-
processCell.space,
471-
),
467+
getDoc(
468+
undefined,
469+
{ resultFor: { inputs, outputs, fn: fn.toString() } },
470+
processCell.space,
471+
),
472472
);
473473
addCancel(cancels.get(resultCell));
474474

0 commit comments

Comments
 (0)