This is a very welcome metric. I'd love to see this land (assuming the algorithm is correct, which I didn't actually check :-)
On Thu, Feb 12, 2009 at 12:21 PM, Lex Spoon <sp...@google.com> wrote: > Code splitting adds a small challenge for anyone monitoring their > overall code size: "overall code size" becomes hard to define. > > Since many people will face this problem, it seems like the compiler > may as well include some reasonable measure for convenience. Based on > that reasoning, the attached patch modifies the compiler to emit, for > each permutation, a reasonable measure of overall code size. > > Thoughts are welcome on what a "reasonable" measure would be. What > the current version does is average the total amount of code > downloaded forr all the possible sequences of split points that will > be reached at run time. The precise implementation is appended, > because the comments are pretty detailed. > > For Showcase in pretty mode, the log messages look like this: > > Average total script size of permutation 0 is 875725.8 > > There's a decimal place included in the output, to emphasize that this > is an average and not a simple sum. > > Note, this calculation is done before linkers modify the output code > in any way. It's just the size of the raw compiled code. Doing > better would seem to require cooperation from the linker, which would > mean the linker API has to get larger, so the patch uses the > pre-modified version instead. The pre-modified version would seem > like a good enough measure for the usual usage of this measure. > > -Lex > > /** > * <p> > * Computes and logs the "average total script size" for this permutation. > The > * total script size for one sequence of split points reached is the sum > of > * the scripts that are downloaded for that sequence. The average total > script > * size is the average of these for all possible sequences of split > points. > * </p> > */ > private static void logScriptSize(TreeLogger logger, int permId, > StandardCompilationResult compilation) { > /* > * Let m be the number of split points. Only m sequences of split points > * need to be averaged, because only the first split point in the > sequence > * affects which fragments will eventually be loaded. Choose as those m > * sequences the sequences starting with each split point and then > hitting > * all other split point in numerical order. > * > * The average can be computed as a weighted sum of all the fragment > sizes, > * divided by m. The weight on each fragment is the number of different > * sequences that fragment is used in: 0 is used m times, the exclusives > are > * used m-1 times, and each base and leftovers fragment is used 1 time. > */ > > String[] javaScript = compilation.getJavaScript(); > int numSplitPoints = (javaScript.length - 1) / 3; > float averageTotalSize; > > if (numSplitPoints == 0) { > averageTotalSize = javaScript[0].length(); > } else { > > int weightedSum = 0; > > // The initial fragment is used m times > weightedSum += numSplitPoints * javaScript[0].length(); > > // The exclusives are used m-1 times > for (int i = 1; i <= numSplitPoints; i++) { > weightedSum += (numSplitPoints - 1) * javaScript[i].length(); > } > > // The base and leftovers are used 1 time > for (int i = numSplitPoints + 1; i < javaScript.length; i++) { > weightedSum += javaScript[i].length(); > } > > averageTotalSize = ((float) weightedSum) / numSplitPoints; > } > > logger.log(TreeLogger.DEBUG, "Average total script size of permutation " > + permId + " is " + String.format("%.1f", averageTotalSize)); > } > > > > --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---