Skip to content

Commit

Permalink
perf: use zero-based numeric indices for much faster instrumented code (
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner authored and bcoe committed Sep 8, 2016
1 parent 71134b6 commit 5b401f5
Show file tree
Hide file tree
Showing 21 changed files with 289 additions and 289 deletions.
6 changes: 3 additions & 3 deletions src/source-coverage.js
Expand Up @@ -35,15 +35,14 @@ class SourceCoverage extends classes.FileCoverage {
}

newStatement(loc) {
this.meta.last.s += 1;
var s = this.meta.last.s;
this.data.statementMap[s] = cloneLocation(loc);
this.data.s[s] = 0;
this.meta.last.s += 1;
return s;
}

newFunction(name, decl, loc) {
this.meta.last.f += 1;
var f = this.meta.last.f;
name = name || '(anonymous_' + f + ')';
this.data.fnMap[f] = {
Expand All @@ -52,18 +51,19 @@ class SourceCoverage extends classes.FileCoverage {
loc: cloneLocation(loc)
};
this.data.f[f] = 0;
this.meta.last.f += 1;
return f;
}

newBranch(type, loc) {
this.meta.last.b += 1;
var b = this.meta.last.b;
this.data.b[b] = [];
this.data.branchMap[b] = {
loc: cloneLocation(loc),
type: type,
locations: []
};
this.meta.last.b += 1;
return b;
}

Expand Down
8 changes: 4 additions & 4 deletions src/visitor.js
Expand Up @@ -91,7 +91,7 @@ class VisitState {
return;
}
// else check custom node attribute set by a prior visitor
if (this.getAttr(path.node, 'skip-all')) {
if (this.getAttr(path.node, 'skip-all') !== null) {
this.nextIgnore = n;
}
}
Expand Down Expand Up @@ -134,7 +134,7 @@ class VisitState {
wrap(
T.memberExpression(
T.memberExpression(T.identifier(this.varName), T.identifier(type)),
T.stringLiteral(String(id)),
T.numericLiteral(id),
true
)
)
Expand Down Expand Up @@ -354,7 +354,7 @@ function coverSwitchCase(path) {
const T = this.types;
const b = this.getAttr(path.parentPath.node, 'branchName');
/* istanbul ignore if: paranoid check */
if (!b) {
if (b === null) {
throw new Error('Unable to get switch branch name');
}
const increment = this.getBranchIncrement(b, path.node.loc);
Expand Down Expand Up @@ -430,7 +430,7 @@ const codeVisitor = {
// the template to insert at the top of the program.
const coverageTemplate = template(`
var COVERAGE_VAR = (function () {
var path = PATH,
var path = PATH,
hash = HASH,
global = (new Function('return this'))(),
gcv = GLOBAL_COVERAGE_VAR,
Expand Down
20 changes: 10 additions & 10 deletions test/specs/arrow-fn.yaml
Expand Up @@ -9,15 +9,15 @@ tests:
args: [1, 2, 3, 4]
out: [1, 4, 9, 16]
lines: {'1': 1, '2': 4}
statements: {'1': 1, '2': 1, '3': 4}
functions: {'1': 4}
statements: {'0': 1, '1': 1, '2': 4}
functions: {'0': 4}

- name: function not called
args: []
out: []
lines: {'1': 1, '2': 1}
statements: {'1': 1, '2': 1, '3': 0}
functions: {'1': 0}
statements: {'0': 1, '1': 1, '2': 0}
functions: {'0': 0}

---
name: es6 arrow function block
Expand All @@ -30,15 +30,15 @@ tests:
args: [1, 2, 3, 4]
out: [1, 4, 9, 16]
lines: {'1': 1, '2': 4}
statements: {'1': 1, '2': 1, '3': 4}
functions: {'1': 4}
statements: {'0': 1, '1': 1, '2': 4}
functions: {'0': 4}

- name: function not called
args: []
out: []
lines: {'1': 1, '2': 1}
statements: {'1': 1, '2': 1, '3': 0}
functions: {'1': 0}
statements: {'0': 1, '1': 1, '2': 0}
functions: {'0': 0}

---
name: complex arrow fn
Expand All @@ -57,6 +57,6 @@ tests:
args: [1, 2, 3, 4]
out: 10
lines: { '1': 1, '2': 1, '3': 1, '4': 1, '8': 1}
functions: { '1': 1, '2': 1, '3': 1, '4': 1 }
statements: { '1': 1, '2': 1, '3': 1, '4': 1, '5': 1, '6':1 }
functions: { '0': 1, '1': 1, '2': 1, '3': 1 }
statements: { '0': 1, '1': 1, '2': 1, '3': 1, '4': 1, '5':1 }

18 changes: 9 additions & 9 deletions test/specs/default-args.yaml
Expand Up @@ -11,22 +11,22 @@ tests:
args: [10, 20, 30, 40]
out: 100
lines: { '2': 1, 4: 1}
statements: {'1': 1, '2': 1 }
functions: {'1': 1}
branches: { '1': [0], '2': [0], '3': [0], '4': [0] }
statements: {'0': 1, '1': 1 }
functions: {'0': 1}
branches: { '0': [0], '1': [0], '2': [0], '3': [0] }

- name: 2 of 4 specified
args: [3, 4 ]
out: 14
lines: { '2': 1, 4: 1}
statements: {'1': 1, '2': 1 }
functions: {'1': 1}
branches: { '1': [0], '2': [0], '3': [1], '4': [1] }
statements: {'0': 1, '1': 1 }
functions: {'0': 1}
branches: { '0': [0], '1': [0], '2': [1], '3': [1] }

- name: nothing specified
args: []
out: 10
lines: { '2': 1, 4: 1}
statements: {'1': 1, '2': 1 }
functions: {'1': 1}
branches: { '1': [1], '2': [1], '3': [1], '4': [1] }
statements: {'0': 1, '1': 1 }
functions: {'0': 1}
branches: { '0': [1], '1': [1], '2': [1], '3': [1] }
10 changes: 5 additions & 5 deletions test/specs/do-while.yaml
Expand Up @@ -9,13 +9,13 @@ tests:
args: [10]
out: 10
lines: {'1': 1, '2': 10, '3': 1}
statements: {'1': 1, '2': 1, '3': 1, '4': 10, '5': 1}
statements: {'0': 1, '1': 1, '2': 1, '3': 10, '4': 1}

- name: single entry into while
args: [-1]
out: 1
lines: {'1': 1, '2': 1, '3': 1}
statements: {'1': 1, '2': 1, '3': 1, '4': 1, '5': 1}
statements: {'0': 1, '1': 1, '2': 1, '3': 1, '4': 1}

---
name: block do-while on separate line
Expand All @@ -30,13 +30,13 @@ tests:
args: [10]
out: 10
lines: {'1': 1, '2': 1, '3': 10, '5': 1}
statements: {'1': 1, '2':1, '3': 1, '4': 10, '5': 1}
statements: {'0': 1, '1':1, '2': 1, '3': 10, '4': 1}

- name: single entry into while
args: [-1]
out: 1
lines: {'1': 1, '2': 1, '3': 1, '5': 1}
statements: {'1': 1, '2': 1, '3': 1, '4': 1, '5': 1}
statements: {'0': 1, '1': 1, '2': 1, '3': 1, '4': 1}

---
name: ignore inside do-while
Expand All @@ -50,4 +50,4 @@ tests:
- args: []
out: 10
lines: {'1': 1, '5': 1}
statements: {'1': 1, '2': 1 }
statements: {'0': 1, '1': 1 }
36 changes: 18 additions & 18 deletions test/specs/expressions.yaml
Expand Up @@ -8,22 +8,22 @@ tests:
args: [-1]
out: false
lines: {'1': 1, '2': 1}
branches: {'1': [1, 0]}
statements: {'1': 1, '2': 1}
branches: {'0': [1, 0]}
statements: {'0': 1, '1': 1}

- name: covers line, both branches, returns false
args: [10]
out: false
lines: {'1': 1, '2': 1}
branches: {'1': [1, 1]}
statements: {'1': 1, '2': 1}
branches: {'0': [1, 1]}
statements: {'0': 1, '1': 1}

- name: covers line, both branches, returns true
args: [3]
out: true
lines: {'1': 1, '2': 1}
branches: {'1': [1, 1]}
statements: {'1': 1, '2': 1}
branches: {'0': [1, 1]}
statements: {'0': 1, '1': 1}

---
name: complex expression
Expand All @@ -35,22 +35,22 @@ tests:
args: [-1]
out: false
lines: {'1': 1, '2': 1}
branches: {'1': [1, 0, 0]}
statements: {'1': 1, '2': 1}
branches: {'0': [1, 0, 0]}
statements: {'0': 1, '1': 1}

- name: covers line, both branches, returns false
args: [9]
out: false
lines: {'1': 1, '2': 1}
branches: {'1': [1, 1, 1]}
statements: {'1': 1, '2': 1}
branches: {'0': [1, 1, 1]}
statements: {'0': 1, '1': 1}

- name: covers line, both branches, returns true
args: [3]
out: true
lines: {'1': 1, '2': 1}
branches: {'1': [1, 1, 0]}
statements: {'1': 1, '2': 1}
branches: {'0': [1, 1, 0]}
statements: {'0': 1, '1': 1}

---
name: array expression with empty positions
Expand All @@ -62,8 +62,8 @@ tests:
args: [1,5]
out: true
lines: {'1': 1, '2': 1}
branches: { 1: [1, 1] }
statements: {'1': 1, '2': 1}
branches: { '0': [1, 1] }
statements: {'0': 1, '1': 1}

---
name: or with object expression (bug track)
Expand All @@ -75,8 +75,8 @@ tests:
args: [ false ]
out: 2
lines: {'1': 1, '2': 1}
branches: { 1: [0, 1] }
statements: {'1': 1, '2': 1}
branches: { '0': [0, 1] }
statements: {'0': 1, '1': 1}

---
name: or with object expression (part 2)
Expand All @@ -88,5 +88,5 @@ tests:
args: [ false ]
out: 2
lines: {'1': 1, '2': 1}
branches: { 1: [1, 1] }
statements: {'1': 1, '2': 1}
branches: { '0': [1, 1] }
statements: {'0': 1, '1': 1}
8 changes: 4 additions & 4 deletions test/specs/for-in.yaml
Expand Up @@ -12,8 +12,8 @@ tests:
- args: [10, 0]
out: 10
lines: {'1': 1, '2': 1, '3': 1, '4': 2, '5': 1}
branches: {'1': [1, 1], '2': [2, 2]}
statements: {'1': 1, '2': 1, '3': 1, '4': 2, '5': 1}
branches: {'0': [1, 1], '1': [2, 2]}
statements: {'0': 1, '1': 1, '2': 1, '3': 2, '4': 1}

---
name: for-in with loop initializer
Expand All @@ -29,5 +29,5 @@ tests:
- args: [10, 0]
out: 10
lines: {'1': 1, '2': 1, '3': 1, '4': 2, '5': 1}
branches: {'1': [1, 1], '2': [2, 2]}
statements: {'1': 1, '2': 1, '3': 1, '4': 2, '5': 1}
branches: {'0': [1, 1], '1': [2, 2]}
statements: {'0': 1, '1': 1, '2': 1, '3': 2, '4': 1}
8 changes: 4 additions & 4 deletions test/specs/for-of.yaml
Expand Up @@ -12,8 +12,8 @@ tests:
- args: []
out: 3
lines: {'1': 1, '3': 1, '4': 1, '5': 2}
functions: {'1': 1}
statements: {'1': 1, '2': 1, '3': 1, '4': 1, '5': 2}
functions: {'0': 1}
statements: {'0': 1, '1': 1, '2': 1, '3': 1, '4': 2}
---

name: for-of with loop initializer
Expand All @@ -28,5 +28,5 @@ tests:
- args: []
out: 3
lines: {'1': 1, '2': 1, '3': 1, '4': 2}
functions: {'1': 1}
statements: {'1': 1, '2': 1, '3': 1, '4': 1, '5': 2}
functions: {'0': 1}
statements: {'0': 1, '1': 1, '2': 1, '3': 1, '4': 2}

0 comments on commit 5b401f5

Please sign in to comment.