Skip to content

Refactor removing module elements#2489

Merged
aheejin merged 2 commits intoWebAssembly:masterfrom
aheejin:remove_util
Dec 3, 2019
Merged

Refactor removing module elements#2489
aheejin merged 2 commits intoWebAssembly:masterfrom
aheejin:remove_util

Conversation

@aheejin
Copy link
Copy Markdown
Member

@aheejin aheejin commented Dec 2, 2019

This creates utility functions for removing module elements: removing
one element by name, and removing multiple elements using a predicate
function. And makes other parts of code use it. I think this is a
light-handed approach than calling Module::updateMaps after removing
only a part of module elements.

This also fixes a bug in the inlining pass: it didn't call
Module::updateMaps after removing functions. After this patch callers
don't need to additionally call it anyway.

This creates utility functions for removing module elements: removing
one element by name, and removing multiple elements using a predicate
function. And makes other parts of code use it. I think this is a
light-handed approach than calling `Module::updateMaps` after removing
only a part of module elements.

This also fixes a bug in the inlining pass: it didn't call
`Module::updateMaps` after removing functions. After this patch callers
don't need to additionally call it anyway.
@aheejin aheejin requested review from kripken and tlively December 2, 2019 18:37
Copy link
Copy Markdown
Member

@tlively tlively left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Just a couple questions.

Comment thread src/wasm/wasm.cpp
if (functionTypes[i]->name == name) {
functionTypes.erase(functionTypes.begin() + i);
template<typename Vector, typename Map>
void removeModuleElement(Vector& v, Map& m, Name name) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to implement this in terms of removeModuleElements?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might be slower, I worry...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the vector thing the effect would be more or less the same, for the deletion from the map, this can be done in constant time whereas we iterate through all elements in the map in removeModuleElements. Given that a lot of usage in the codebase is removing a single element, I guess it'd be better to keep this in the way it is?

Comment thread src/wasm/wasm.cpp Outdated
std::function<bool(Elem* elem)> pred) {
v.erase(
std::remove_if(v.begin(), v.end(), [&](auto& e) { return pred(e.get()); }),
v.end());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do a remove with iterators and then also do the remove with a loop?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first one is deleting from a vector and the second is from a map. Actually I think I should swap the order of these two, because deleting unique_ptr destroys the object so the second map thing can segfault.

Copy link
Copy Markdown
Member

@kripken kripken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice idea and implementation!

@aheejin aheejin merged commit 9d4acee into WebAssembly:master Dec 3, 2019
@aheejin aheejin deleted the remove_util branch December 3, 2019 00:34
aheejin added a commit to aheejin/binaryen that referenced this pull request Dec 21, 2019
This does something similar to WebAssembly#2489 for more functions, removing
boilerplate code for each module element using template functions.
aheejin added a commit to aheejin/binaryen that referenced this pull request Dec 21, 2019
This does something similar to WebAssembly#2489 for more functions, removing
boilerplate code for each module element using template functions.
aheejin added a commit to aheejin/binaryen that referenced this pull request Dec 23, 2019
This does something similar to WebAssembly#2489 for more functions, removing
boilerplate code for each module element using template functions.
aheejin added a commit that referenced this pull request Dec 23, 2019
This does something similar to #2489 for more functions, removing
boilerplate code for each module element using template functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants