Skip to content

Commit

Permalink
feat: cache type model (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstallenberg committed Dec 11, 2023
1 parent 4afd4ed commit 5174f1b
Show file tree
Hide file tree
Showing 3 changed files with 473 additions and 529 deletions.
57 changes: 25 additions & 32 deletions libraries/analysis-javascript/lib/type/resolving/TypeModel.ts
Expand Up @@ -289,23 +289,19 @@ export class TypeModel {
randomTypeProbability: number,
id: string
): string {
const probabilities = this.calculateProbabilitiesForElement(
incorporateExecutionScore,
id
);

// const x = new Map();
// for (const [type, probability] of probabilities.entries()) {
// const typeEnum = type.includes("<>") ? type.split("<>")[1] : type;

// if (!x.has(typeEnum)) {
// x.set(typeEnum, 0);
// }
let probabilities;

// x.set(typeEnum, x.get(typeEnum) + probability);
// }
// console.log(id);
// console.log(x);
// This allows caching of type scores. The only problem is that when a score of a relation changes it will not recalculate for the current element (only for the relation element)
if (this._scoreHasChangedMap.get(id)) {
probabilities = this.calculateProbabilitiesForElement(
incorporateExecutionScore,
id
);
this._scoreHasChangedMap.set(id, false);
} else {
// prevent recalculation of probabilities without score changes
probabilities = this._elementTypeProbabilityMap.get(id);
}

const genericTypes = [
TypeEnum.ARRAY,
Expand Down Expand Up @@ -357,10 +353,19 @@ export class TypeModel {
randomTypeProbability: number,
id: string
): string {
const probabilities = this.calculateProbabilitiesForElement(
incorporateExecutionScore,
id
);
let probabilities;

// This allows caching of type scores. The only problem is that when a score of a relation changes it will not recalculate for the current element (only for the relation element)
if (this._scoreHasChangedMap.get(id)) {
probabilities = this.calculateProbabilitiesForElement(
incorporateExecutionScore,
id
);
this._scoreHasChangedMap.set(id, false);
} else {
// prevent recalculation of probabilities without score changes
probabilities = this._elementTypeProbabilityMap.get(id);
}

const genericTypes = [
TypeEnum.ARRAY,
Expand Down Expand Up @@ -421,16 +426,6 @@ export class TypeModel {
id: string,
relationPairsVisited?: Map<string, Set<string>>
): Map<string, number> {
// if (!this._scoreHasChangedMap.has(element)) {
// throw new ImplementationError(`Element ${element} does not exist`);
// }
// if (this._scoreHasChangedMap.get(element) === false) {
// // prevent recalculation of probabilities without score changes
// return this._elementTypeProbabilityMap.get(element);
// }

// this._scoreHasChangedMap.set(element, false);

let probabilityMap = new Map<string, number>();

if (id === "anon") {
Expand All @@ -446,8 +441,6 @@ export class TypeModel {

if (!relationPairsVisited) {
relationPairsVisited = new Map();
// this._scoreHasChangedMap.set(element, false);
// this._elementTypeProbabilityMap.set(element, probabilityMap);
}

let totalScore = this._sum(typeScoreMap.values());
Expand Down
Expand Up @@ -548,7 +548,6 @@ export class JavaScriptRandomSampler extends JavaScriptTestCaseSampler {
this.randomTypeProbability,
id
);

break;
}
case "ranked": {
Expand Down

0 comments on commit 5174f1b

Please sign in to comment.