-
Notifications
You must be signed in to change notification settings - Fork 829
Minify import and export names #1719
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f4e80c6
initial work [ci skip]
kripken baaf94d
exports too
kripken 4de4122
typo
kripken 8fdf16c
fix
kripken f406b04
better
kripken e248bb7
don't minify special stuff
kripken ecf2aa9
only minify env.*
kripken f80f99b
fix js build
kripken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| /* | ||
| * Copyright 2018 WebAssembly Community Group participants | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| // | ||
| // Minifies import and export names, renaming them to short versions, | ||
| // and prints out a mapping to the new short versions. That mapping | ||
| // can then be used to minify the JS calling the wasm, together enabling | ||
| // minification of the identifiers on the JS/wasm boundary. | ||
| // | ||
| // For example, this may minify | ||
| // (import "env" "longname" (func $internal)) | ||
| // to | ||
| // (import "env" "a" (func $internal)) | ||
| // "a" is the minified name (note that we only minify the base, | ||
| // not the module). | ||
| // | ||
|
|
||
| #include <map> | ||
| #include <string> | ||
| #include <unordered_set> | ||
|
|
||
| #include <wasm.h> | ||
| #include <pass.h> | ||
| #include <shared-constants.h> | ||
| #include <asmjs/shared-constants.h> | ||
| #include <ir/import-utils.h> | ||
| #include <ir/module-utils.h> | ||
|
|
||
| namespace wasm { | ||
|
|
||
| struct MinifyImportsAndExports : public Pass { | ||
|
|
||
| // Generates minified names that are valid in JS. | ||
| // Names are computed lazily. | ||
| class MinifiedNames { | ||
| public: | ||
| MinifiedNames() { | ||
| // Reserved words in JS up to size 4 - size 5 and above would mean we use an astronomical | ||
| // number of symbols, which is not realistic anyhow. | ||
| reserved.insert("do"); | ||
| reserved.insert("if"); | ||
| reserved.insert("in"); | ||
| reserved.insert("for"); | ||
| reserved.insert("new"); | ||
| reserved.insert("try"); | ||
| reserved.insert("var"); | ||
| reserved.insert("env"); | ||
| reserved.insert("let"); | ||
| reserved.insert("case"); | ||
| reserved.insert("else"); | ||
| reserved.insert("enum"); | ||
| reserved.insert("void"); | ||
| reserved.insert("this"); | ||
| reserved.insert("void"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| reserved.insert("with"); | ||
|
|
||
| validInitialChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$"; | ||
| validLaterChars = validInitialChars + "0123456789"; | ||
|
|
||
| minifiedState.push_back(0); | ||
| } | ||
|
|
||
| // Get the n-th minified name. | ||
| std::string getName(size_t n) { | ||
| ensure(n + 1); | ||
| return names[n]; | ||
| } | ||
|
|
||
| private: | ||
| // Reserved words we must not emit. | ||
| std::unordered_set<std::string> reserved; | ||
|
|
||
| // Possible initial letters. | ||
| std::string validInitialChars; | ||
|
|
||
| // Possible later letters. | ||
| std::string validLaterChars; | ||
|
|
||
| // The minified names we computed so far. | ||
| std::vector<std::string> names; | ||
|
|
||
| // Helper state for progressively computing more minified names - | ||
| // a stack of the current index. | ||
| std::vector<size_t> minifiedState; | ||
|
|
||
| // Make sure we have at least n minified names. | ||
| void ensure(size_t n) { | ||
| while (names.size() < n) { | ||
| // Generate the current name. | ||
| std::string name; | ||
| auto index = minifiedState[0]; | ||
| assert(index < validInitialChars.size()); | ||
| name += validInitialChars[index]; | ||
| for (size_t i = 1; i < minifiedState.size(); i++) { | ||
| auto index = minifiedState[i]; | ||
| assert(index < validLaterChars.size()); | ||
| name += validLaterChars[index]; | ||
| } | ||
| if (reserved.count(name) == 0) { | ||
| names.push_back(name); | ||
| } | ||
| // Increment the state. | ||
| size_t i = 0; | ||
| while (1) { | ||
| minifiedState[i]++; | ||
| if (minifiedState[i] < (i == 0 ? validInitialChars : validLaterChars).size()) { | ||
| break; | ||
| } | ||
| // Overflow. | ||
| minifiedState[i] = 0; | ||
| i++; | ||
| if (i == minifiedState.size()) { | ||
| minifiedState.push_back(-1); // will become 0 after increment in next loop head | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| void run(PassRunner* runner, Module* module) override { | ||
| // Minify the imported names. | ||
| MinifiedNames names; | ||
| size_t soFar = 0; | ||
| std::map<Name, Name> oldToNew; | ||
| auto process = [&](Name& name) { | ||
| // do not minifiy special imports, they must always exist | ||
| if (name == MEMORY_BASE || name == TABLE_BASE) { | ||
| return; | ||
| } | ||
| auto newName = names.getName(soFar++); | ||
| oldToNew[newName] = name; | ||
| name = newName; | ||
| }; | ||
| auto processImport = [&](Importable* curr) { | ||
| if (curr->module == ENV) { | ||
| process(curr->base); | ||
| } | ||
| }; | ||
| ModuleUtils::iterImportedGlobals(*module, processImport); | ||
| ModuleUtils::iterImportedFunctions(*module, processImport); | ||
| // Minify the exported names. | ||
| for (auto& curr : module->exports) { | ||
| process(curr->name); | ||
| } | ||
| module->updateMaps(); | ||
| // Emit the mapping. | ||
| for (auto& pair : oldToNew) { | ||
| std::cout << pair.second.str << " => " << pair.first.str << '\n'; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| Pass *createMinifyImportsAndExportsPass() { | ||
| return new MinifyImportsAndExports(); | ||
| } | ||
|
|
||
| } // namespace wasm | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hi @kripken. Shouldn't this also reserve
null,evalandtruekeywords?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.
Hmm yeah, the list is incomplete - I just copied it from the emscripten js minifier code. Which is incomplete too I guess ;)
I think in practice, to get to 4 letters is very unlikely. With three letters you can get to 221,184 minified ids. But, would be worth adding the rest of the words too, I agree.