diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2019-01-21 22:44:08 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2019-01-21 22:44:08 +0800 |
commit | 9d9ab2f1ee926d74df00799705bbecb39b2b5d99 (patch) | |
tree | 2d177721dd023d4f99c6122fd72de9b344b06f11 /packages/sol-tracing-utils/src | |
parent | ce65ac0c1c34913027ff4ea9e512d01a5e20abdf (diff) | |
download | dexon-0x-contracts-9d9ab2f1ee926d74df00799705bbecb39b2b5d99.tar.gz dexon-0x-contracts-9d9ab2f1ee926d74df00799705bbecb39b2b5d99.tar.zst dexon-0x-contracts-9d9ab2f1ee926d74df00799705bbecb39b2b5d99.zip |
Cache bytecode lookup
Diffstat (limited to 'packages/sol-tracing-utils/src')
-rw-r--r-- | packages/sol-tracing-utils/src/utils.ts | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/packages/sol-tracing-utils/src/utils.ts b/packages/sol-tracing-utils/src/utils.ts index 89c158ee7..40aa3e98a 100644 --- a/packages/sol-tracing-utils/src/utils.ts +++ b/packages/sol-tracing-utils/src/utils.ts @@ -10,6 +10,8 @@ import { ContractData, LineColumn, SingleFileSourceRange } from './types'; const MIN_CONTRACT_BYTECODE_LENGTH = 88; const STATICCALL_GAS_COST = 40; +const bytecodeToContractDataIfExists: { [bytecode: string]: ContractData | undefined } = {}; + export const utils = { compareLineColumn(lhs: LineColumn, rhs: LineColumn): number { return lhs.line !== rhs.line ? lhs.line - rhs.line : lhs.column - rhs.column; @@ -47,6 +49,11 @@ export const utils = { if (!bytecode.startsWith('0x')) { throw new Error(`0x hex prefix missing: ${bytecode}`); } + // HACK(leo): We want to cache the values that are possibly undefined. + // That's why we can't check for undefined as we usually do, but need to use `hasOwnProperty`. + if (bytecodeToContractDataIfExists.hasOwnProperty(bytecode)) { + return bytecodeToContractDataIfExists[bytecode]; + } const contractData = _.find(contractsData, contractDataCandidate => { const bytecodeRegex = utils.bytecodeToBytecodeRegex(contractDataCandidate.bytecode); // If the bytecode is less than the minimum length, we are probably @@ -62,7 +69,7 @@ export const utils = { // collisions are practically impossible and it allows us to reuse that code return !_.isNull(bytecode.match(bytecodeRegex)) || !_.isNull(bytecode.match(runtimeBytecodeRegex)); }); - return contractData; + return (bytecodeToContractDataIfExists[bytecode] = contractData); }, isCallLike(op: OpCode): boolean { return _.includes([OpCode.CallCode, OpCode.StaticCall, OpCode.Call, OpCode.DelegateCall], op); |