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

Unified Diff: compiler/java/com/google/dart/compiler/backend/js/JavascriptBackend.java

Issue 8676041: JS tree shaking for incremental builds. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 9 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: compiler/java/com/google/dart/compiler/backend/js/JavascriptBackend.java
diff --git a/compiler/java/com/google/dart/compiler/backend/js/JavascriptBackend.java b/compiler/java/com/google/dart/compiler/backend/js/JavascriptBackend.java
index 18c9c8898ff593ae4fd9235ffa326afb0313f8b8..4e6f4f79f23bcba3acc8a8c9b5d5bfbebcfbde3e 100644
--- a/compiler/java/com/google/dart/compiler/backend/js/JavascriptBackend.java
+++ b/compiler/java/com/google/dart/compiler/backend/js/JavascriptBackend.java
@@ -12,6 +12,7 @@ import com.google.dart.compiler.DartSource;
import com.google.dart.compiler.LibrarySource;
import com.google.dart.compiler.ast.LibraryNode;
import com.google.dart.compiler.ast.LibraryUnit;
+import com.google.dart.compiler.backend.js.analysis.TreeShaker;
import com.google.dart.compiler.common.GenerateSourceMap;
import com.google.dart.compiler.metrics.CompilerMetrics;
import com.google.dart.compiler.resolver.CoreTypeProvider;
@@ -83,8 +84,6 @@ public class JavascriptBackend extends AbstractJsBackend {
private static class DepsWritingCallback implements DepsCallback {
private final DartCompilerContext context;
- private long charsWritten = 0;
- private long nativeCharsWritten = 0;
private CountingAppendable out;
private final List<SourceMapSection> appSections;
@@ -97,28 +96,12 @@ public class JavascriptBackend extends AbstractJsBackend {
this.appSections = appSections;
}
- /**
- * @return the charsWritten
- */
- public long getCharsWritten() {
- return charsWritten;
- }
-
- /**
- * @return the nativeCharsWritten
- */
- public long getNativeCharsWritten() {
- return nativeCharsWritten;
- }
-
@Override
public void visitNative(LibraryUnit libUnit, LibraryNode node)
throws IOException {
DartSource nativeSrc = libUnit.getSource().getSourceFor(node.getText());
Reader r = nativeSrc.getSourceReader();
long charsWrittenForFile = CharStreams.copy(r, out);
- nativeCharsWritten += charsWrittenForFile;
- charsWritten += charsWrittenForFile;
}
@Override
@@ -136,7 +119,6 @@ public class JavascriptBackend extends AbstractJsBackend {
boolean failed = true;
try {
partSize = CharStreams.copy(r, out);
- charsWritten += partSize;
failed = false;
} finally {
Closeables.close(r, failed);
@@ -158,12 +140,6 @@ public class JavascriptBackend extends AbstractJsBackend {
DepsWritingCallback callback = new DepsWritingCallback(context, out, appSections);
DependencyBuilder.build(context.getAppLibraryUnit(), callback);
-
- CompilerMetrics compilerMetrics = context.getCompilerMetrics();
- if (compilerMetrics != null) {
- compilerMetrics.packagedJsApplication(
- callback.getCharsWritten(), callback.getNativeCharsWritten());
- }
}
@Override
@@ -172,8 +148,13 @@ public class JavascriptBackend extends AbstractJsBackend {
DartCompilerContext context,
CoreTypeProvider typeProvider)
throws IOException {
+
+ LibraryUnit appLibraryUnit = context.getAppLibraryUnit();
+ boolean hasEntryPoint = appLibraryUnit.getElement().getEntryPoint() != null;
+
List<SourceMapSection> appSections = Lists.newArrayList();
- Writer out = context.getArtifactWriter(app, "", EXTENSION_APP_JS);
+ String completeArtifactName = EXTENSION_APP_JS + ".complete";
+ Writer out = context.getArtifactWriter(app, "", completeArtifactName);
boolean failed = true;
try {
// Emit the concatenated Javascript sources in dependency order.
@@ -184,7 +165,30 @@ public class JavascriptBackend extends AbstractJsBackend {
} finally {
Closeables.close(out, failed);
}
+
+ long fileSize = computeCompleteArtifactSize(app, context, completeArtifactName);
+ Reader artifactReader = context.getArtifactReader(app, "", completeArtifactName);
+ Writer artifactWriter = context.getArtifactWriter(app, "", EXTENSION_APP_JS);
+ try {
+ failed = true;
+ long outputFileSize;
+ if (hasEntryPoint) {
+ outputFileSize = TreeShaker.reduce(artifactReader, artifactWriter, fileSize);
+ } else {
+ outputFileSize = CharStreams.copy(artifactReader, artifactWriter);
fabiomfv 2011/12/01 15:37:40 could we just rename(move) the file instead of cop
mmendez 2011/12/01 21:31:30 I can't rename with the current API, but what I wi
+ }
+ failed = false;
+
+ CompilerMetrics compilerMetrics = context.getCompilerMetrics();
+ if (compilerMetrics != null) {
+ compilerMetrics.packagedJsApplication(outputFileSize, -1);
+ }
+ } finally {
+ Closeables.close(artifactWriter, failed);
+ Closeables.close(artifactReader, failed);
+ }
+
Writer srcMapOut = context.getArtifactWriter(app, "", EXTENSION_APP_JS_SRC_MAP);
failed = true;
try {
@@ -198,6 +202,19 @@ public class JavascriptBackend extends AbstractJsBackend {
}
}
+ private static long computeCompleteArtifactSize(LibrarySource app, DartCompilerContext context,
+ String completeArtifactName) throws IOException {
+ Reader artifactReader = context.getArtifactReader(app, "", completeArtifactName);
+ boolean failed = true;
+ try {
+ long fileSize = artifactReader.skip(Long.MAX_VALUE);
+ failed = false;
+ return fileSize;
+ } finally {
+ Closeables.close(artifactReader, failed);
+ }
+ }
+
@Override
public String getAppExtension() {
return EXTENSION_APP_JS;

Powered by Google App Engine
This is Rietveld 408576698