Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize name selection in wasmprinter
This commit updates how names are chosen for items in wasmprinter. Printing wasm has the problem where we want to provide human-readable names from the name section, but the printed wasm text must faithfully represent the binary itself. This means that the human-readable names from the name section, which have no restrictions on them beyond utf-8, cannot be used as-is to represent names of items in the printed text. Previously the solution that was implemented was to keep a set of names defined for each particular type and iteratively append integers to the end of a given name until something wasn't in the set. This technique was intended to provide human-readable names by basing identifiers off the names found in the name section, but it also guaranteed uniqueness by ensuring everything was in the original set. Fuzzing, however, is showing that this technique can be quite slow because looping can happen quite a lot and it was additionally implemented pretty inefficiently with lots of string allocations. This commit implements a new strategy for naming items based on names found in the name section. The original name from the name section is still prioritized if it works (e.g. it's a valid wasm text identifier and it's the first-of-its-kind), but the fallback is now much more efficient and involves no kinds of loops. Generated identifiers for an item are now guaranteed to be unique since they look like: #func10<original_name> would be generated for function index 10 if the original name was "original name" (because ' ' isn't a valid text identifier character). This retains the human-readability by having the original name in there, but uniqueness is guaranteed with the index being factored in. Additionally `wasmprinter` goes ahead and "reserves" all symbols that start with a `#` to ensure that wasm-provided names can't collide with wasmprinter-selected names. This further guarantees uniqueness of generated names by `wasmprinter`. This makes the fuzz test case I was looking at much faster, dropping the printing time from 20 seconds to 10 milliseconds. Additionally the code is much easier to read as well!
- Loading branch information