| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /// Command-line tool presenting how much each function contributes to the total | 5 /// Command-line tool presenting how much each function contributes to the total |
| 6 /// code. | 6 /// code. |
| 7 library compiler.tool.live_code_size_analysis; | 7 library compiler.tool.live_code_size_analysis; |
| 8 | 8 |
| 9 import 'dart:math' as math; | 9 import 'dart:math' as math; |
| 10 | 10 |
| 11 import 'package:dart2js_info/info.dart'; | 11 import 'package:dart2js_info/info.dart'; |
| 12 import 'package:dart2js_info/src/graph.dart'; | 12 import 'package:dart2js_info/src/graph.dart'; |
| 13 import 'package:dart2js_info/src/util.dart'; | 13 import 'package:dart2js_info/src/util.dart'; |
| 14 | 14 |
| 15 main(args) async { | 15 main(args) async { |
| 16 var info = await infoFromFile(args.first); | 16 var info = await infoFromFile(args.first); |
| 17 showCodeDistribution(info); | 17 showCodeDistribution(info); |
| 18 } | 18 } |
| 19 | 19 |
| 20 showCodeDistribution(AllInfo info, | 20 showCodeDistribution(AllInfo info, |
| 21 {bool filter(Info info), bool showLibrarySizes: false}) { | 21 {bool filter(Info info), bool showLibrarySizes: false}) { |
| 22 var realTotal = info.program.size; | 22 var realTotal = info.program.size; |
| 23 if (filter == null) filter = (i) => true; | 23 if (filter == null) filter = (i) => true; |
| 24 var reported = [] | 24 var reported = <BasicInfo>[] |
| 25 ..addAll(info.functions.where(filter)) | 25 ..addAll(info.functions.where(filter)) |
| 26 ..addAll(info.fields.where(filter)); | 26 ..addAll(info.fields.where(filter)); |
| 27 | 27 |
| 28 // Compute a graph from the dependencies in [info]. | 28 // Compute a graph from the dependencies in [info]. |
| 29 Graph<Info> graph = graphFromInfo(info); | 29 Graph<Info> graph = graphFromInfo(info); |
| 30 | 30 |
| 31 // Compute the strongly connected components and calculate their size. | 31 // Compute the strongly connected components and calculate their size. |
| 32 var components = graph.computeTopologicalSort(); | 32 var components = graph.computeTopologicalSort(); |
| 33 print('total elements: ${graph.nodes.length}'); | 33 print('total elements: ${graph.nodes.length}'); |
| 34 print('total strongly connected components: ${components.length}'); | 34 print('total strongly connected components: ${components.length}'); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 62 helper(n) { | 62 helper(n) { |
| 63 int size = n.size; | 63 int size = n.size; |
| 64 assert(!dominatedSize.containsKey(n)); | 64 assert(!dominatedSize.containsKey(n)); |
| 65 dominatedSize[n] = 0; | 65 dominatedSize[n] = 0; |
| 66 dominatorTree.targetsOf(n).forEach((m) { | 66 dominatorTree.targetsOf(n).forEach((m) { |
| 67 size += helper(m); | 67 size += helper(m); |
| 68 }); | 68 }); |
| 69 dominatedSize[n] = size; | 69 dominatedSize[n] = size; |
| 70 return size; | 70 return size; |
| 71 } | 71 } |
| 72 |
| 72 helper(mainMethod); | 73 helper(mainMethod); |
| 73 reported.forEach((n) => dominatedSize.putIfAbsent(n, () => n.size)); | 74 reported.forEach((n) => dominatedSize.putIfAbsent(n, () => n.size)); |
| 74 reported.sort((a, b) => (dominatedSize[b] + nodeData[b].maxSize) - | 75 reported.sort((a, b) => |
| 76 (dominatedSize[b] + nodeData[b].maxSize) - |
| 75 (dominatedSize[a] + nodeData[a].maxSize)); | 77 (dominatedSize[a] + nodeData[a].maxSize)); |
| 76 | 78 |
| 77 if (showLibrarySizes) { | 79 if (showLibrarySizes) { |
| 78 print(' --- Results per library ---'); | 80 print(' --- Results per library ---'); |
| 79 var totals = {}; | 81 var totals = {}; |
| 80 var longest = 0; | 82 var longest = 0; |
| 81 reported.forEach((info) { | 83 reported.forEach((info) { |
| 82 var size = info.size; | 84 var size = info.size; |
| 83 while (info != null && info is! LibraryInfo) { | 85 while (info != null && info is! LibraryInfo) { |
| 84 info = info.parent; | 86 info = info.parent; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 | 124 |
| 123 void compute() { | 125 void compute() { |
| 124 if (_maxSize != null) return; | 126 if (_maxSize != null) return; |
| 125 var max = 0; | 127 var max = 0; |
| 126 var seen = new Set(); | 128 var seen = new Set(); |
| 127 helper(n) { | 129 helper(n) { |
| 128 if (!seen.add(n)) return; | 130 if (!seen.add(n)) return; |
| 129 max += n.size; | 131 max += n.size; |
| 130 n.deps.forEach(helper); | 132 n.deps.forEach(helper); |
| 131 } | 133 } |
| 134 |
| 132 helper(this); | 135 helper(this); |
| 133 _maxSize = max; | 136 _maxSize = max; |
| 134 } | 137 } |
| 135 } | 138 } |
| 136 | 139 |
| 137 _showLibHeader(int namePadding) { | 140 _showLibHeader(int namePadding) { |
| 138 print(' ${pad("Library", namePadding, right: true)}' | 141 print(' ${pad("Library", namePadding, right: true)}' |
| 139 ' ${pad("bytes", 8)} ${pad("%", 6)}'); | 142 ' ${pad("bytes", 8)} ${pad("%", 6)}'); |
| 140 } | 143 } |
| 141 | 144 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 154 | 157 |
| 155 _showElement(String name, int size, int dominatedSize, int maxSize, int total) { | 158 _showElement(String name, int size, int dominatedSize, int maxSize, int total) { |
| 156 var percent = (size * 100 / total).toStringAsFixed(2); | 159 var percent = (size * 100 / total).toStringAsFixed(2); |
| 157 var minPercent = (dominatedSize * 100 / total).toStringAsFixed(2); | 160 var minPercent = (dominatedSize * 100 / total).toStringAsFixed(2); |
| 158 var maxPercent = (maxSize * 100 / total).toStringAsFixed(2); | 161 var maxPercent = (maxSize * 100 / total).toStringAsFixed(2); |
| 159 print('${pad(size, 8)} ${pad(percent, 6)}% ' | 162 print('${pad(size, 8)} ${pad(percent, 6)}% ' |
| 160 '${pad(dominatedSize, 10)} ${pad(minPercent, 6)}% ' | 163 '${pad(dominatedSize, 10)} ${pad(minPercent, 6)}% ' |
| 161 '${pad(maxSize, 10)} ${pad(maxPercent, 6)}% ' | 164 '${pad(maxSize, 10)} ${pad(maxPercent, 6)}% ' |
| 162 '$name'); | 165 '$name'); |
| 163 } | 166 } |
| OLD | NEW |