Fix @memoize and make it more efficient
This commit is contained in:
parent
aa1893918b
commit
9415f7a2e7
2 changed files with 22 additions and 19 deletions
|
@ -142,7 +142,6 @@ export class TypeChecker {
|
||||||
node: arg,
|
node: arg,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -159,7 +158,7 @@ export class TypeChecker {
|
||||||
return this.getTypeOfNode(sym.declarations[0]);
|
return this.getTypeOfNode(sym.declarations[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@memoize
|
@memoize(node => node.id)
|
||||||
private getTypeOfNode(node: BoltSyntax): Type {
|
private getTypeOfNode(node: BoltSyntax): Type {
|
||||||
switch (node.kind) {
|
switch (node.kind) {
|
||||||
case SyntaxKind.BoltReferenceTypeExpression:
|
case SyntaxKind.BoltReferenceTypeExpression:
|
||||||
|
@ -193,8 +192,8 @@ export class TypeChecker {
|
||||||
type = this.resolveType('String', node)!;
|
type = this.resolveType('String', node)!;
|
||||||
} else if (typeof node.value === 'boolean') {
|
} else if (typeof node.value === 'boolean') {
|
||||||
type = this.resolveType('bool', node)!;
|
type = this.resolveType('bool', node)!;
|
||||||
} else if (typeof node.value === 'number') {
|
} else if (typeof node.value === 'bigint') {
|
||||||
type = this.resolveType('int32', node)!;
|
type = this.resolveType('i32', node)!;
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Could not derive type of constant expression.`);
|
throw new Error(`Could not derive type of constant expression.`);
|
||||||
}
|
}
|
||||||
|
|
34
src/util.ts
34
src/util.ts
|
@ -123,22 +123,26 @@ class DeepMap {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function memoize(target: any, key: PropertyKey) {
|
export function memoize(hasher: (...args: any[]) => string) {
|
||||||
const origMethod = target[key];
|
return function (target: any, key: PropertyKey) {
|
||||||
target[key] = function wrapper(...args: any[]) {
|
const origMethod = target[key];
|
||||||
if (this.__MEMOIZE_CACHE === undefined) {
|
target[key] = function wrapper(...args: any[]) {
|
||||||
this.__MEMOIZE_CACHE = Object.create(null);
|
if (this.__MEMOIZE_CACHE === undefined) {
|
||||||
|
this.__MEMOIZE_CACHE = Object.create(null);
|
||||||
|
}
|
||||||
|
if (this.__MEMOIZE_CACHE[key] === undefined) {
|
||||||
|
this.__MEMOIZE_CACHE[key] = Object.create(null);
|
||||||
|
}
|
||||||
|
const hashed = hasher(...args);
|
||||||
|
const cache = this.__MEMOIZE_CACHE[key];
|
||||||
|
if (hashed in cache) {
|
||||||
|
return cache[hashed];
|
||||||
|
}
|
||||||
|
const result = origMethod.apply(this, args);
|
||||||
|
cache[hashed] = result;
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
if (this.__MEMOIZE_CACHE[key] === undefined) {
|
return target;
|
||||||
this.__MEMOIZE_CACHE[key] = new DeepMap();
|
|
||||||
}
|
|
||||||
const cache = this.__MEMOIZE_CACHE[key];
|
|
||||||
if (cache.has(args)) {
|
|
||||||
return cache.get(args);
|
|
||||||
}
|
|
||||||
const result = origMethod.apply(this, args);
|
|
||||||
cache.set(args, result);
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue