From 62f45f7b41fdd984025ba70008c682eeded2fadb Mon Sep 17 00:00:00 2001 From: Leonid Logvinov Date: Fri, 9 Mar 2018 14:36:19 +0100 Subject: Cache code parsing --- packages/sol-cov/package.json | 1 + packages/sol-cov/src/instrument_solidity.ts | 21 +++++++++++++++------ packages/sol-cov/tsconfig.json | 7 ++++++- 3 files changed, 22 insertions(+), 7 deletions(-) (limited to 'packages/sol-cov') diff --git a/packages/sol-cov/package.json b/packages/sol-cov/package.json index 5aaec4a38..a01dc9bcd 100644 --- a/packages/sol-cov/package.json +++ b/packages/sol-cov/package.json @@ -23,6 +23,7 @@ "@0xproject/subproviders": "^0.7.0", "@0xproject/utils": "^0.3.4", "glob": "^7.1.2", + "ethereumjs-util": "^5.1.1", "istanbul": "^0.4.5", "lodash": "^4.17.4", "solidity-coverage": "^0.4.10", diff --git a/packages/sol-cov/src/instrument_solidity.ts b/packages/sol-cov/src/instrument_solidity.ts index ac58270cb..163e3c7b9 100644 --- a/packages/sol-cov/src/instrument_solidity.ts +++ b/packages/sol-cov/src/instrument_solidity.ts @@ -1,3 +1,4 @@ +import * as ethUtil from 'ethereumjs-util'; import * as fs from 'fs'; import * as _ from 'lodash'; import * as path from 'path'; @@ -6,11 +7,19 @@ import * as SolidityParser from 'solidity-parser-sc'; import { ASTVisitor, CoverageEntriesDescription } from './ast_visitor'; import { getLocationByOffset } from './source_maps'; +// Parsing source code for each transaction/code is slow and therefore we cache it +const coverageEntriesBySourceHash: { [sourceHash: string]: CoverageEntriesDescription } = {}; + export const collectCoverageEntries = (contractSource: string, fileName: string) => { - const ast = SolidityParser.parse(contractSource); - const locationByOffset = getLocationByOffset(contractSource); - const astVisitor = new ASTVisitor(locationByOffset); - astVisitor.walkAST(ast); - const coverageEntries = astVisitor.getCollectedCoverageEntries(); - return coverageEntries; + const time = Date.now(); + const sourceHash = ethUtil.sha3(contractSource).toString('hex'); + if (_.isUndefined(coverageEntriesBySourceHash[sourceHash])) { + const ast = SolidityParser.parse(contractSource); + const locationByOffset = getLocationByOffset(contractSource); + const astVisitor = new ASTVisitor(locationByOffset); + astVisitor.walkAST(ast); + coverageEntriesBySourceHash[sourceHash] = astVisitor.getCollectedCoverageEntries(); + } + const coverageEntriesDescription = coverageEntriesBySourceHash[sourceHash]; + return coverageEntriesDescription; }; diff --git a/packages/sol-cov/tsconfig.json b/packages/sol-cov/tsconfig.json index 3d967d05f..bdf315d59 100644 --- a/packages/sol-cov/tsconfig.json +++ b/packages/sol-cov/tsconfig.json @@ -3,5 +3,10 @@ "compilerOptions": { "outDir": "lib" }, - "include": ["./src/**/*", "../../node_modules/web3-typescript-typings/index.d.ts"] + "include": [ + "./src/**/*", + "../../node_modules/types-bn/index.d.ts", + "../../node_modules/web3-typescript-typings/index.d.ts", + "../../node_modules/types-ethereumjs-util/index.d.ts" + ] } -- cgit v1.2.3