Skip to content

Commit

Permalink
add memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
frangio committed Oct 17, 2019
1 parent 97cb3a4 commit ef7bfc7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 16 deletions.
36 changes: 21 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -30,6 +30,7 @@
"handlebars": "^4.4.0",
"json5": "^2.1.0",
"lodash": "^4.17.15",
"mem": "^5.1.1",
"minimatch": "^3.0.4",
"semver": "^6.3.0",
"solc": "^0.5.12",
Expand Down
17 changes: 17 additions & 0 deletions src/memoize.ts
@@ -0,0 +1,17 @@
import mem from 'mem';

export function memoize<T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T> {

if (descriptor.value instanceof Function) {
const func = descriptor.value;
// Did not find a way to avoid this cast to any.
// See https://github.com/microsoft/TypeScript/issues/34540.
descriptor.value = mem(func as any) as any;

} else if (descriptor.get instanceof Function) {
const func = descriptor.get;
descriptor.get = mem(func);
}

return descriptor;
}
8 changes: 8 additions & 0 deletions src/solidity.ts
@@ -1,6 +1,7 @@
import { flatten, uniqBy, groupBy } from 'lodash';
import path from 'path';
import execall from 'execall';
import { memoize } from './memoize';

type ContractTemplate = (contract: SolidityContract) => string;

Expand All @@ -23,6 +24,7 @@ export class SoliditySource {
.map(fileName => this.file(fileName));
}

@memoize
file(fileName: string): SolidityFile {
return new SolidityFile(
this,
Expand All @@ -49,6 +51,7 @@ class SolidityFile {
readonly path: string,
) { }

@memoize
get contracts(): SolidityContract[] {
const astNodes = this.ast.nodes.filter(n =>
n.nodeType === 'ContractDefinition'
Expand Down Expand Up @@ -106,6 +109,7 @@ export class SolidityContract implements Linkable {
);
}

@memoize
get ownFunctions(): SolidityFunction[] {
return this.astNode.nodes
.filter(isFunctionDefinition)
Expand Down Expand Up @@ -133,6 +137,7 @@ export class SolidityContract implements Linkable {
);
}

@memoize
get ownEvents(): SolidityEvent[] {
return this.astNode.nodes
.filter(isEventDefinition)
Expand All @@ -146,6 +151,7 @@ export class SolidityContract implements Linkable {
);
}

@memoize
get ownModifiers(): SolidityModifier[] {
return this.astNode.nodes
.filter(isModifierDefinition)
Expand Down Expand Up @@ -185,6 +191,7 @@ abstract class SolidityContractItem implements Linkable {
return `${this.contract.name}-${slug(this.signature)}`
}

@memoize
get args(): SolidityTypedVariable[] {
return SolidityTypedVariableArray.fromParameterList(
this.astNode.parameters
Expand Down Expand Up @@ -218,6 +225,7 @@ class SolidityFunction extends SolidityContractItem {
return isRegularFunction ? name : kind;
}

@memoize
get outputs(): SolidityTypedVariable[] {
return SolidityTypedVariableArray.fromParameterList(
this.astNode.returnParameters
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Expand Up @@ -2,7 +2,8 @@
"extends": "code-style/tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"allowJs": true
"allowJs": true,
"experimentalDecorators": true
},
"include": [
"src/**/*"
Expand Down

0 comments on commit ef7bfc7

Please sign in to comment.