Skip to content

Commit

Permalink
createHitmap: refactored to make it more efficient
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo committed May 19, 2015
1 parent 5cbfe9e commit fca0713
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions lib/src/hitmap.dart
Expand Up @@ -8,24 +8,21 @@ import 'dart:isolate';
/// Creates a single hitmap from a raw json object. Throws away all entries that
/// are not resolvable.
Map createHitmap(List<Map> json) {
Map<String, Map<int, int>> hitMap = {};
Map<String, Map<int, int>> hitMap = <String, Map<int, int>>{};

This comment has been minimized.

Copy link
@cbracken

cbracken May 20, 2015

Member

var hitMap = ...

This comment has been minimized.

Copy link
@cbracken

cbracken May 20, 2015

Member

While you're here, can you add a comment above the map:
// Map of source file to map of line to hit count for that line.

This comment has been minimized.

Copy link
@kevmoo

kevmoo May 20, 2015

Author Member

done


addToMap(source, line, count) {
if (!hitMap[source].containsKey(line)) {
hitMap[source][line] = 0;
}
hitMap[source][line] += count;
void addToMap(Map<int, int> map, int line, int count) {
var oldCount = map.putIfAbsent(line, () => 0);
map[line] = count + oldCount;
}

json.forEach((Map e) {
for (Map e in json) {
var source = e['source'];
if (source == null) {
// Couldnt resolve import, so skip this entry.
return;
}
if (!hitMap.containsKey(source)) {
hitMap[source] = {};
// Couldn't resolve import, so skip this entry.
continue;
}

var childHitMap = hitMap.putIfAbsent(source, () => <int, int>{});

This comment has been minimized.

Copy link
@cbracken

cbracken May 20, 2015

Member

What do you think about sourceHitMap here and globalHitMap to replace hitMap?

This comment has been minimized.

Copy link
@kevmoo

kevmoo May 20, 2015

Author Member

sure

var hits = e['hits'];
// hits is a flat array of the following format:
// [ <line|linerange>, <hitcount>,...]
Expand All @@ -35,19 +32,19 @@ Map createHitmap(List<Map> json) {
var k = hits[i];
if (k is num) {
// Single line.
addToMap(source, k, hits[i + 1]);
addToMap(childHitMap, k, hits[i + 1]);
}
if (k is String) {

This comment has been minimized.

Copy link
@cbracken

cbracken May 20, 2015

Member

Worth making this an } else if (k is String) { while you're at it.

This comment has been minimized.

Copy link
@kevmoo

kevmoo May 20, 2015

Author Member

Make it an } else { with a type assert – since we don't expect anything else

// Linerange. We expand line ranges to actual lines at this point.
var splitPos = k.indexOf('-');
int start = int.parse(k.substring(0, splitPos));
int end = int.parse(k.substring(splitPos + 1, k.length));

This comment has been minimized.

Copy link
@cbracken

cbracken May 20, 2015

Member

Let's also kill the k.length here:
int end = int.parse(k.substring(splitPos + 1));

This comment has been minimized.

Copy link
@kevmoo

kevmoo May 20, 2015

Author Member

done

for (var j = start; j <= end; j++) {
addToMap(source, j, hits[i + 1]);
addToMap(childHitMap, j, hits[i + 1]);
}
}
}
});
}
return hitMap;
}

Expand Down

0 comments on commit fca0713

Please sign in to comment.