Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I <em>think</em> this is what's going on, but I am by no means certain...</p> <p>The code that causes the insertion of commas is <code>tryMinimizeStringArrayLiteral</code> in <a href="http://code.google.com/searchframe#l5BkQmivP-Y/trunk/src/com/google/javascript/jscomp/PeepholeSubstituteAlternateSyntax.java" rel="noreferrer">PeepholeSubstituteAlternateSyntax.java</a>.</p> <p>That method contains a list of characters that are likely to have a low <a href="http://en.wikipedia.org/wiki/Huffman_coding" rel="noreferrer">Huffman encoding</a>, and are therefore preferable to split on than other characters. You can see the result of this if you try something like this:</p> <pre><code>"a b c d e f g".split(" "); //Uncompiled, split on spaces "a,b,c,d,e,f,g".split(","); //Compiled, split on commas (same size) </code></pre> <p>The compiler will replace the character you try to split on with one it thinks is favourable. It does so by iterating over the characters of the string and finding the most favourable splitting character that does not occur within the string:</p> <pre><code>// These delimiters are chars that appears a lot in the program therefore // probably have a small Huffman encoding. NEXT_DELIMITER: for (char delimiter : new char[]{',', ' ', ';', '{', '}'}) { for (String cur : strings) { if (cur.indexOf(delimiter) != -1) { continue NEXT_DELIMITER; } } String template = Joiner.on(delimiter).join(strings); //... } </code></pre> <p>In the above snippet you can see the array of characters the compiler claims to be optimal to split on. The comma is first (which is why in my space example above, the spaces have been replaced by commas).</p> <p>I believe the insertion of commas in the case where the string to split on is the empty string may simply be an oversight. There does not appear to be any special treatment of this case, so it's treated like any other <code>split</code> call and each character is joined with the first appropriate character from the array shown in the above snippet.</p> <hr> <p>Another example of how the compiler deals with the <code>split</code> method:</p> <pre><code>"a,;b;c;d;e;f;g".split(";"); //Uncompiled, split on semi-colons "a, b c d e f g".split(" "); //Compiled, split on spaces </code></pre> <p>This time, since the original string already contains a comma (and we don't want to split on the comma character), the comma can't be chosen from the array of low-Huffman-encoded characters, so the next best choice is selected (the space).</p> <hr> <p><strong>Update</strong></p> <p>Following some further research into this, it is definitely not a bug. This behaviour is actually by design, and in my opinion it's a very clever little optimisation, when you bear in mind that the Closure compiler tends to favour the <em>speed</em> of the compiled code over size.</p> <p>Above I mentioned Huffman encoding a couple of times. The Huffman coding algorithm, explained very simply, assigns a weight to each character appearing the the text to be encoded. The weight is based on the frequency with which each character appears. These frequencies are used to build a binary tree, with the most common character at the root. That means the most common characters are quicker to decode, since they are closer to the root of the tree.</p> <p>And since the Huffman algorithm is a large part of the DEFLATE algorithm used by gzip. So if your web server is configured to use gzip, your users will be benefiting from this clever optimisation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload