You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2019/06/27 21:51:13 UTC

[royale-compiler] branch develop updated: GoogDepsWriter: more use of StringBuilders to avoid creation of too many Strings

This is an automated email from the ASF dual-hosted git repository.

joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git


The following commit(s) were added to refs/heads/develop by this push:
     new 9602fe4  GoogDepsWriter: more use of StringBuilders to avoid creation of too many Strings
9602fe4 is described below

commit 9602fe41849d54c6dd65e1ed7b3258aca10051e4
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Thu Jun 27 14:10:51 2019 -0700

    GoogDepsWriter: more use of StringBuilders to avoid creation of too many Strings
---
 .../compiler/internal/graph/GoogDepsWriter.java    | 81 ++++++++++++++--------
 1 file changed, 51 insertions(+), 30 deletions(-)

diff --git a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/graph/GoogDepsWriter.java b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/graph/GoogDepsWriter.java
index 8ddcff8..728734c 100644
--- a/compiler-jx/src/main/java/org/apache/royale/compiler/internal/graph/GoogDepsWriter.java
+++ b/compiler-jx/src/main/java/org/apache/royale/compiler/internal/graph/GoogDepsWriter.java
@@ -47,8 +47,8 @@ import org.apache.royale.compiler.internal.projects.CompilerProject;
 import org.apache.royale.compiler.internal.projects.DefinitionPriority;
 import org.apache.royale.compiler.internal.projects.DependencyGraph;
 import org.apache.royale.compiler.internal.projects.RoyaleJSProject;
-import org.apache.royale.compiler.problems.MainDefinitionQNameProblem;
 import org.apache.royale.compiler.problems.FileNotFoundProblem;
+import org.apache.royale.compiler.problems.MainDefinitionQNameProblem;
 import org.apache.royale.compiler.problems.UnexpectedExceptionProblem;
 import org.apache.royale.compiler.units.ICompilationUnit;
 import org.apache.royale.swc.ISWC;
@@ -167,17 +167,23 @@ public class GoogDepsWriter {
 				{
 					ArrayList <String> deps = new ArrayList<String>();
 					computeDeps(deps, gd, usedDeps);
-					sb.append("goog.addDependency('").append(relativePath(gd.filePath)).append("', ['")
-						.append(gd.className).append("'], [")
-						.append(getDependencies(deps))
-						.append("]);\n");
+					sb.append("goog.addDependency('")
+						.append(relativePath(gd.filePath))
+						.append("', ['")
+						.append(gd.className)
+						.append("'], [");
+					appendDependencies(deps, sb);
+					sb.append("]);\n");
 				}
 				else
 				{
-					sb.append("goog.addDependency('").append(relativePath(gd.filePath)).append("', ['")
-					.append(gd.className).append("'], [")
-					.append(getDependencies(gd.deps))
-					.append("]);\n");
+					sb.append("goog.addDependency('")
+						.append(relativePath(gd.filePath))
+						.append("', ['")
+						.append(gd.className)
+						.append("'], [");
+					appendDependencies(gd.deps, sb);
+					sb.append("]);\n");
 				}
 			}
 		}
@@ -234,7 +240,8 @@ public class GoogDepsWriter {
 						restOfDeps.add(gd.className);
 				}
 			}
-			mainDeps.append(getDependencies(restOfDeps)).append("]);\n");
+			appendDependencies(restOfDeps, mainDeps);
+			mainDeps.append("]);\n");
 			sb.insert(0, mainDeps);
 			sb.insert(0, "// generated by Royale\n");
 			for (String dep : restOfDeps)
@@ -251,10 +258,13 @@ public class GoogDepsWriter {
 					continue;
 				ArrayList<String> deps = new ArrayList<String>();
 				computeDeps(deps, gd, usedDeps);
-				sb.append("goog.addDependency('").append(relativePath(gd.filePath)).append("', ['")
-				.append(gd.className).append("'], [")
-				.append(getDependencies(deps))
-				.append("]);\n");
+				sb.append("goog.addDependency('")
+					.append(relativePath(gd.filePath))
+					.append("', ['")
+					.append(gd.className)
+					.append("'], [");
+				appendDependencies(deps, sb);
+				sb.append("]);\n");
 			}
 			addRestOfDeps(mainDep, restOfDeps);
 		}
@@ -423,7 +433,9 @@ public class GoogDepsWriter {
 		visited.put(current.className, current);
 		
 		if (removeCirculars)
+		{
 			removeRequires(current);
+		}
 		if (verbose)
 		{
 			System.out.println("Dependencies calculated for '" + current.className + "'");
@@ -571,9 +583,13 @@ public class GoogDepsWriter {
 			for (int i = n - 1; i >= 0; i--)
 			{
 				String dep = restOfDeps.get(i);
-				//if (!main.deps.contains(dep))
-					fileLines.add(main.fileInfo.googProvideLine + 1, JSGoogEmitterTokens.GOOG_REQUIRE.getToken() + "('" + dep + "');");
-					sourceMapConsumer = addLineToSourceMap(sourceMapConsumer, mainFile.getName(), main.fileInfo.googProvideLine + 1);
+				StringBuilder lineBuilder = new StringBuilder();
+				lineBuilder.append(JSGoogEmitterTokens.GOOG_REQUIRE.getToken())
+					.append("('")
+					.append(dep)
+					.append("');");
+				fileLines.add(main.fileInfo.googProvideLine + 1, lineBuilder.toString());
+				sourceMapConsumer = addLineToSourceMap(sourceMapConsumer, mainFile.getName(), main.fileInfo.googProvideLine + 1);
 			}
 
 			PrintWriter out = new PrintWriter(new FileWriter(mainFile));  
@@ -581,7 +597,7 @@ public class GoogDepsWriter {
             {
                 out.println(s);
             }
-            out.close();
+			out.close();
 
 			if (sourceMapConsumer != null)
 			{
@@ -749,21 +765,24 @@ public class GoogDepsWriter {
             if (gd.fileInfo.staticDeps != null)
             {
     			if (lastRequireLine == -1)
-    				lastRequireLine = gd.fileInfo.googProvideLine + 1;
+					lastRequireLine = gd.fileInfo.googProvideLine + 1;
             	for (String dep : gd.fileInfo.staticDeps)
             	{
             		if (!writtenRequires.contains(dep) && !isExternal(dep))
             		{
-            			String line = JSGoogEmitterTokens.GOOG_REQUIRE.getToken();
-            			line += "('" + dep + "');";
-            			finalLines.add(lastRequireLine++, line);
+						StringBuilder lineBuilder = new StringBuilder();
+						lineBuilder.append(JSGoogEmitterTokens.GOOG_REQUIRE.getToken())
+							.append("('")
+							.append(dep)
+							.append("');");
+            			finalLines.add(lastRequireLine++, lineBuilder.toString());
 						sourceMapConsumer = addLineToSourceMap(sourceMapConsumer, new File(gd.filePath).getName(), lastRequireLine);
             			if (verbose)
 						{
 							System.out.println("adding require for static dependency " + dep + " to " + className);
 						}
             		}
-            	}
+				}
             }
             //if (suppressCount > 0)
             //{
@@ -844,7 +863,7 @@ public class GoogDepsWriter {
 			sourceMapConsumer = addLineToSourceMap(sourceMapConsumer, depFile.getName(), gd.fileInfo.googProvideLine + 1);
 
 			PrintWriter out = new PrintWriter(new FileWriter(depFile));  
-            for (String s : finalLines)
+			for (String s : finalLines)
             {
                 out.println(s);
             }
@@ -1466,18 +1485,20 @@ public class GoogDepsWriter {
 	{		
 	}
 	
-	private String getDependencies(ArrayList<String> deps)
+	private void appendDependencies(ArrayList<String> deps, StringBuilder builder)
 	{
-		String s = "";
+		boolean hasDeps = false;
 		for (String dep : deps)
 		{
-			if (s.length() > 0)
+			if (hasDeps)
 			{
-				s += ", ";
+				builder.append(", ");
 			}
-			s += "'" + dep + "'";			
+			builder.append("''");
+			builder.append(dep);
+			builder.append("''");
+			hasDeps = true;
 		}
-		return s;
 	}
 
 	String relativePath(String path)