Cache code parsing

This commit is contained in:
Leonid Logvinov
2018-03-09 14:36:19 +01:00
parent a7d8f6599a
commit 62f45f7b41
3 changed files with 22 additions and 7 deletions

View File

@@ -23,6 +23,7 @@
"@0xproject/subproviders": "^0.7.0", "@0xproject/subproviders": "^0.7.0",
"@0xproject/utils": "^0.3.4", "@0xproject/utils": "^0.3.4",
"glob": "^7.1.2", "glob": "^7.1.2",
"ethereumjs-util": "^5.1.1",
"istanbul": "^0.4.5", "istanbul": "^0.4.5",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"solidity-coverage": "^0.4.10", "solidity-coverage": "^0.4.10",

View File

@@ -1,3 +1,4 @@
import * as ethUtil from 'ethereumjs-util';
import * as fs from 'fs'; import * as fs from 'fs';
import * as _ from 'lodash'; import * as _ from 'lodash';
import * as path from 'path'; import * as path from 'path';
@@ -6,11 +7,19 @@ import * as SolidityParser from 'solidity-parser-sc';
import { ASTVisitor, CoverageEntriesDescription } from './ast_visitor'; import { ASTVisitor, CoverageEntriesDescription } from './ast_visitor';
import { getLocationByOffset } from './source_maps'; 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) => { export const collectCoverageEntries = (contractSource: string, fileName: string) => {
const ast = SolidityParser.parse(contractSource); const time = Date.now();
const locationByOffset = getLocationByOffset(contractSource); const sourceHash = ethUtil.sha3(contractSource).toString('hex');
const astVisitor = new ASTVisitor(locationByOffset); if (_.isUndefined(coverageEntriesBySourceHash[sourceHash])) {
astVisitor.walkAST(ast); const ast = SolidityParser.parse(contractSource);
const coverageEntries = astVisitor.getCollectedCoverageEntries(); const locationByOffset = getLocationByOffset(contractSource);
return coverageEntries; const astVisitor = new ASTVisitor(locationByOffset);
astVisitor.walkAST(ast);
coverageEntriesBySourceHash[sourceHash] = astVisitor.getCollectedCoverageEntries();
}
const coverageEntriesDescription = coverageEntriesBySourceHash[sourceHash];
return coverageEntriesDescription;
}; };

View File

@@ -3,5 +3,10 @@
"compilerOptions": { "compilerOptions": {
"outDir": "lib" "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"
]
} }