Skip to content

Commit

Permalink
refactor(daemon): Memoize formulas (merge #2171)
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal authored Mar 23, 2024
2 parents 2ca4788 + 573768f commit 3739c40
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions packages/daemon/src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,28 +196,32 @@ const makeDaemonCore = async (
const controllerForId = new Map();

/**
* Forward look-up, for answering "what is the formula type of this id".
* @type {Map<string, string>}
* Forward look-up, for answering "what is the formula for this id".
* @type {Map<string, import('./types.js').Formula>}
*/
const typeForId = new Map();
const formulaForId = new Map();

/** @param {string} id */
const getTypeForId = async id => {
const getFormulaForId = async id => {
await null;

const formulaType = typeForId.get(id);
if (formulaType !== undefined) {
return formulaType;
let formula = formulaForId.get(id);
if (formula !== undefined) {
return formula;
}

formula = await persistencePowers.readFormula(parseId(id).number);
formulaForId.set(id, formula);
return formula;
};

/** @param {string} id */
const getTypeForId = async id => {
if (parseId(id).node !== ownNodeIdentifier) {
typeForId.set(id, 'remote');
return 'remote';
}

const formula = await persistencePowers.readFormula(parseId(id).number);
typeForId.set(id, formula.type);
return formula.type;
const { type } = await getFormulaForId(id);
return type;
};

/**
Expand Down Expand Up @@ -691,17 +695,15 @@ const makeDaemonCore = async (
if (isRemote) {
// eslint-disable-next-line no-use-before-define
const peerIdentifier = await getPeerIdForNodeIdentifier(formulaNode);
typeForId.set(id, 'remote');
// Behold, forward reference:
// eslint-disable-next-line no-use-before-define
return provideRemoteValue(peerIdentifier, id);
}

const formula = await persistencePowers.readFormula(formulaNumber);
console.log(`Making ${formula.type} ${formulaNumber}`);
const formula = await getFormulaForId(id);
console.log(`Reincarnating ${formula.type} ${id}`);
assertValidFormulaType(formula.type);
// TODO further validation
typeForId.set(id, formula.type);

return makeControllerForFormula(id, formulaNumber, formula, context);
};
Expand All @@ -713,6 +715,9 @@ const makeDaemonCore = async (
node: ownNodeIdentifier,
});

formulaForId.has(id) && assert.Fail`Formula already exists for id ${id}`;
formulaForId.set(id, formula);

// Memoize for lookup.
console.log(`Making ${id}`);
const { promise: partial, resolve: resolvePartial } =
Expand All @@ -735,7 +740,6 @@ const makeDaemonCore = async (
internal: E.get(partial).internal,
});
controllerForId.set(id, controller);
typeForId.set(id, formula.type);

// The controller _must_ be constructed in the synchronous prelude of this function.
const controllerValue = makeControllerForFormula(
Expand Down Expand Up @@ -1561,9 +1565,7 @@ const makeDaemonCore = async (
throw new Error(`Unknown pet name ${petName}`);
}
const { number: formulaNumber } = parseId(id);
// TODO memoize formulas at the root of the
// id->formula->controller->value tree.
const formula = await persistencePowers.readFormula(formulaNumber);
const formula = await getFormulaForId(id);
if (
!['eval', 'lookup', 'make-unconfined', 'make-bundle', 'guest'].includes(
formula.type,
Expand Down

0 comments on commit 3739c40

Please sign in to comment.