aboutsummaryrefslogtreecommitdiffstats
path: root/packages/sol-cov/src
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2018-03-09 21:36:19 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2018-03-12 10:37:28 +0800
commit62f45f7b41fdd984025ba70008c682eeded2fadb (patch)
tree0c324f0b06a08047ba56dc4fabab6041399471d9 /packages/sol-cov/src
parenta7d8f6599a2da7cf0e45f8a11960ee434b4d8da3 (diff)
downloaddexon-sol-tools-62f45f7b41fdd984025ba70008c682eeded2fadb.tar
dexon-sol-tools-62f45f7b41fdd984025ba70008c682eeded2fadb.tar.gz
dexon-sol-tools-62f45f7b41fdd984025ba70008c682eeded2fadb.tar.bz2
dexon-sol-tools-62f45f7b41fdd984025ba70008c682eeded2fadb.tar.lz
dexon-sol-tools-62f45f7b41fdd984025ba70008c682eeded2fadb.tar.xz
dexon-sol-tools-62f45f7b41fdd984025ba70008c682eeded2fadb.tar.zst
dexon-sol-tools-62f45f7b41fdd984025ba70008c682eeded2fadb.zip
Cache code parsing
Diffstat (limited to 'packages/sol-cov/src')
-rw-r--r--packages/sol-cov/src/instrument_solidity.ts21
1 files changed, 15 insertions, 6 deletions
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;
};