Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(194)

Side by Side Diff: pkg/kernel/test/treeshaker_dump.dart

Issue 2644543004: Revert "Improvements to the kernel tree shaker." (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/kernel/test/treeshaker_bench.dart ('k') | pkg/kernel/test/treeshaker_membench.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 library kernel.treeshaker_dump; 4 library kernel.treeshaker_dump;
5 5
6 import 'dart:io'; 6 import 'dart:io';
7 import 'package:kernel/kernel.dart'; 7 import 'package:kernel/kernel.dart';
8 import 'package:kernel/transformations/treeshaker.dart'; 8 import 'package:kernel/transformations/treeshaker.dart';
9 import 'package:args/args.dart'; 9 import 'package:args/args.dart';
10 import 'package:path/path.dart' as pathlib; 10 import 'package:path/path.dart' as pathlib;
11 import 'package:kernel/text/ast_to_text.dart'; 11 import 'package:kernel/text/ast_to_text.dart';
12 12
13 ArgParser parser = new ArgParser(allowTrailingOptions: true) 13 ArgParser parser = new ArgParser(allowTrailingOptions: true)
14 ..addFlag('used', help: 'Print used members', negatable: false) 14 ..addFlag('used', help: 'Print used members', negatable: false)
15 ..addFlag('unused', help: 'Print unused members', negatable: false) 15 ..addFlag('unused', help: 'Print unused members', negatable: false)
16 ..addFlag('instantiated', 16 ..addFlag('instantiated',
17 help: 'Print instantiated classes', negatable: false) 17 help: 'Print instantiated classes', negatable: false)
18 ..addFlag('types', help: 'Print classes used as a type', negatable: false) 18 ..addFlag('types', help: 'Print classes used as a type', negatable: false)
19 ..addFlag('summary', 19 ..addFlag('summary',
20 help: 'Print short summary of tree shaking results', defaultsTo: true) 20 help: 'Print short summary of tree shaking results', defaultsTo: true)
21 ..addFlag('diff', 21 ..addFlag('diff',
22 help: 'Print textual output before and after tree shaking.\n' 22 help: 'Print textual output before and after tree shaking.\n'
23 'Files are written to FILE.before.txt and FILE.after.txt', 23 'Files are written to FILE.before.txt and FILE.after.txt',
24 negatable: false) 24 negatable: false)
25 ..addOption('output', 25 ..addOption('output',
26 help: 'The --diff files are written to the given directory instead of ' 26 help: 'The --diff files are written to the given directory instead of '
27 'the working directory') 27 'the working directory');
28 ..addFlag('strong', help: 'Run the tree shaker in strong mode');
29 28
30 String usage = ''' 29 String usage = '''
31 Usage: treeshaker_dump [options] FILE.dill 30 Usage: treeshaker_dump [options] FILE.dill
32 31
33 Runs tree shaking on the given program and prints information about the results. 32 Runs tree shaking on the given program and prints information about the results.
34 33
35 Example: 34 Example:
36 treeshaker_dump --instantiated foo.dill 35 treeshaker_dump --instantiated foo.dill
37 36
38 Example: 37 Example:
(...skipping 15 matching lines...) Expand all
54 print('Exactly one file should be given.'); 53 print('Exactly one file should be given.');
55 exit(1); 54 exit(1);
56 } 55 }
57 String filename = options.rest.single; 56 String filename = options.rest.single;
58 57
59 if (options['output'] != null && !options['diff']) { 58 if (options['output'] != null && !options['diff']) {
60 print('--output must be used with --diff'); 59 print('--output must be used with --diff');
61 exit(1); 60 exit(1);
62 } 61 }
63 62
64 bool strong = options['strong'];
65
66 Program program = loadProgramFromBinary(filename); 63 Program program = loadProgramFromBinary(filename);
67 TreeShaker shaker = new TreeShaker(program, strongMode: strong); 64 TreeShaker shaker = new TreeShaker(program);
68 int totalClasses = 0; 65 int totalClasses = 0;
69 int totalInstantiationCandidates = 0; 66 int totalInstantiationCandidates = 0;
70 int totalMembers = 0; 67 int totalMembers = 0;
71 int usedClasses = 0; 68 int usedClasses = 0;
72 int instantiatedClasses = 0; 69 int instantiatedClasses = 0;
73 int usedMembers = 0; 70 int usedMembers = 0;
74 71
75 void visitMember(Member member) { 72 void visitMember(Member member) {
76 if (member.isAbstract) return; // Abstract members are not relevant. 73 if (member.isAbstract) return; // Abstract members are not relevant.
77 ++totalMembers; 74 ++totalMembers;
78 bool isUsed = shaker.isMemberBodyUsed(member); 75 bool isUsed = shaker.isMemberUsed(member);
79 if (isUsed) { 76 if (isUsed) {
80 ++usedMembers; 77 ++usedMembers;
81 } 78 }
82 if (isUsed && options['used'] || !isUsed && options['unused']) { 79 if (isUsed && options['used'] || !isUsed && options['unused']) {
83 String prefix = (options['used'] && options['unused']) 80 String prefix = (options['used'] && options['unused'])
84 ? (isUsed ? 'USED ' : 'UNUSED ') 81 ? (isUsed ? 'USED ' : 'UNUSED ')
85 : ''; 82 : '';
86 print('$prefix$member'); 83 print('$prefix$member');
87 } 84 }
88 } 85 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 117
121 if (options['diff']) { 118 if (options['diff']) {
122 String name = pathlib.basenameWithoutExtension(filename); 119 String name = pathlib.basenameWithoutExtension(filename);
123 String outputDir = options['output'] ?? ''; 120 String outputDir = options['output'] ?? '';
124 String beforeFile = pathlib.join(outputDir, '$name.before.txt'); 121 String beforeFile = pathlib.join(outputDir, '$name.before.txt');
125 String afterFile = pathlib.join(outputDir, '$name.after.txt'); 122 String afterFile = pathlib.join(outputDir, '$name.after.txt');
126 NameSystem names = new NameSystem(); 123 NameSystem names = new NameSystem();
127 StringBuffer before = new StringBuffer(); 124 StringBuffer before = new StringBuffer();
128 new Printer(before, syntheticNames: names).writeProgramFile(program); 125 new Printer(before, syntheticNames: names).writeProgramFile(program);
129 new File(beforeFile).writeAsStringSync('$before'); 126 new File(beforeFile).writeAsStringSync('$before');
130 new TreeShaker(program, strongMode: strong).transform(program); 127 new TreeShaker(program).transform(program);
131 StringBuffer after = new StringBuffer(); 128 StringBuffer after = new StringBuffer();
132 new Printer(after, syntheticNames: names).writeProgramFile(program); 129 new Printer(after, syntheticNames: names).writeProgramFile(program);
133 new File(afterFile).writeAsStringSync('$after'); 130 new File(afterFile).writeAsStringSync('$after');
134 print('Text written to $beforeFile and $afterFile'); 131 print('Text written to $beforeFile and $afterFile');
135 } 132 }
136 } 133 }
137 134
138 String ratio(num x, num total) { 135 String ratio(num x, num total) {
139 return '$x / $total (${percent(x, total)})'; 136 return '$x / $total (${percent(x, total)})';
140 } 137 }
141 138
142 String percent(num x, num total) { 139 String percent(num x, num total) {
143 return total == 0 ? '0%' : ((100 * x / total).toStringAsFixed(0) + '%'); 140 return total == 0 ? '0%' : ((100 * x / total).toStringAsFixed(0) + '%');
144 } 141 }
OLDNEW
« no previous file with comments | « pkg/kernel/test/treeshaker_bench.dart ('k') | pkg/kernel/test/treeshaker_membench.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698