- 
                Notifications
    
You must be signed in to change notification settings  - Fork 3.4k
 
Optimize binaryEncode() string quoting #25613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize binaryEncode() string quoting #25613
Conversation
This is an automatic change generated by tools/maint/rebaseline_tests.py. The following (1) test expectation files were updated by running the tests with `--rebaseline`: ``` codesize/test_codesize_hello_single_file.json: 5404 => 5366 [-38 bytes / -0.70%] Average change: -0.70% (-0.70% - -0.70%) ```
4757d23    to
    b9068ad      
    Compare
  
    | 
           Ping, any thoughts here?  | 
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if its worth the extra complexity here in the code generatation.
Is it worth those 7 bytes?
BTW, are you making use of this in Unity?  Do you, or you users, use the -sSINGLE_FILE setting a lot?   (And why?)
          
 The 7 bytes is only in a test case that itself is only 5400 bytes in size. Running the encoder on one of the smallest .wasm files from Unity output: 
 So the wins do scale with the size of the .wasm file, so not just a constant 7 bytes. 
 Yes. The SINGLE_FILE output from Unity is for use in the MRAID standard. What they do is standardize on the CDN storage and lifecycle of an interactive (playable) ad. The restrictions that the CDN people state is that the ads need to be a single .html file containing everything embedded in it (no extra XHRs or Fetches allowed), and the size limit is <= 5MB uncompressed.  | 
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation.
One of the optimizations of Closure compiler is to choose the string quote character that results in fewer escaping in the string itself.
I.e. Closure compiler will turn
into
by selecting the string quote char that allows the contents to have fewer escapse.
However, in the SINGLE_FILE mode, we emit the Wasm code inside the .js file after Closure has run. So all Closure sees is
when optimizing.
This PR implements the same smart string quote selection optimization directly into the
binaryEncode()function, by checking if there are fewer's or"s in the binary content that is to be encoded.