| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 | |
| 6 /** | 5 /** |
| 7 * The track map is a simple wrapper around a map that keeps track | 6 * The track map is a simple wrapper around a map that keeps track |
| 8 * of the 'final' size of maps grouped by description. It allows | 7 * of the 'final' size of maps grouped by description. It allows |
| 9 * determining the distribution of sizes for a specific allocation | 8 * determining the distribution of sizes for a specific allocation |
| 10 * site and it can be used like this: | 9 * site and it can be used like this: |
| 11 * | 10 * |
| 12 * Map<String, int> map = new TrackMap<String, int>("my-map"); | 11 * Map<String, int> map = new TrackMap<String, int>("my-map"); |
| 13 * | 12 * |
| 14 * After finishing the compilaton, the histogram of track map sizes | 13 * After finishing the compilaton, the histogram of track map sizes |
| 15 * is printed but only when running in verbose mode. | 14 * is printed but only when running in verbose mode. |
| 16 */ | 15 */ |
| 17 class TrackMap<K, V> implements Map<K, V> { | 16 class TrackMap<K, V> implements Map<K, V> { |
| 18 final Map _map; | 17 final Map _map; |
| 19 final List _counts; | 18 final List _counts; |
| 20 static final Map<String, List<int>> _countsMap = {}; | 19 static final Map<String, List<int>> _countsMap = {}; |
| 21 | 20 |
| 22 TrackMap._internal(this._counts) : _map = new Map<K, V>(); | 21 TrackMap._internal(this._counts) : _map = new Map<K, V>(); |
| 23 | 22 |
| 24 factory TrackMap(String description) { | 23 factory TrackMap(String description) { |
| 25 List counts = _countsMap.putIfAbsent(description, () => [ 0 ]); | 24 List counts = _countsMap.putIfAbsent(description, () => [0]); |
| 26 Map result = new TrackMap<K, V>._internal(counts); | 25 Map result = new TrackMap<K, V>._internal(counts); |
| 27 counts[0]++; | 26 counts[0]++; |
| 28 return result; | 27 return result; |
| 29 } | 28 } |
| 30 | 29 |
| 31 static void printHistogram() { | 30 static void printHistogram() { |
| 32 _countsMap.forEach((description, counts) { | 31 _countsMap.forEach((description, counts) { |
| 33 print('$description -- ${counts.length} maps'); | 32 print('$description -- ${counts.length} maps'); |
| 34 | 33 |
| 35 // Count the total number of maps. | 34 // Count the total number of maps. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 54 int get length => _map.length; | 53 int get length => _map.length; |
| 55 bool get isEmpty => _map.isEmpty; | 54 bool get isEmpty => _map.isEmpty; |
| 56 bool get isNotEmpty => _map.isNotEmpty; | 55 bool get isNotEmpty => _map.isNotEmpty; |
| 57 | 56 |
| 58 Iterable<K> get keys => _map.keys; | 57 Iterable<K> get keys => _map.keys; |
| 59 Iterable<V> get values => _map.values; | 58 Iterable<V> get values => _map.values; |
| 60 | 59 |
| 61 bool containsKey(K key) => _map.containsKey(key); | 60 bool containsKey(K key) => _map.containsKey(key); |
| 62 bool containsValue(V value) => _map.containsValue(value); | 61 bool containsValue(V value) => _map.containsValue(value); |
| 63 | 62 |
| 64 V operator[](K key) => _map[key]; | 63 V operator [](K key) => _map[key]; |
| 65 String toString() => _map.toString(); | 64 String toString() => _map.toString(); |
| 66 | 65 |
| 67 void forEach(void action(K key, V value)) { | 66 void forEach(void action(K key, V value)) { |
| 68 _map.forEach(action); | 67 _map.forEach(action); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void operator[]=(K key, V value) { | 70 void operator []=(K key, V value) { |
| 72 if (!_map.containsKey(key)) { | 71 if (!_map.containsKey(key)) { |
| 73 _notifyLengthChanged(1); | 72 _notifyLengthChanged(1); |
| 74 _map[key] = value; | 73 _map[key] = value; |
| 75 } | 74 } |
| 76 } | 75 } |
| 77 | 76 |
| 78 V putIfAbsent(K key, V ifAbsent()) { | 77 V putIfAbsent(K key, V ifAbsent()) { |
| 79 if (containsKey(key)) return this[key]; | 78 if (containsKey(key)) return this[key]; |
| 80 V value = ifAbsent(); | 79 V value = ifAbsent(); |
| 81 this[key] = value; | 80 this[key] = value; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 103 int newLength = oldLength + delta; | 102 int newLength = oldLength + delta; |
| 104 _counts[oldLength]--; | 103 _counts[oldLength]--; |
| 105 if (newLength < _counts.length) { | 104 if (newLength < _counts.length) { |
| 106 _counts[newLength]++; | 105 _counts[newLength]++; |
| 107 } else { | 106 } else { |
| 108 _counts.add(1); | 107 _counts.add(1); |
| 109 assert(newLength == _counts.length - 1); | 108 assert(newLength == _counts.length - 1); |
| 110 } | 109 } |
| 111 } | 110 } |
| 112 } | 111 } |
| OLD | NEW |