binaryen icon indicating copy to clipboard operation
binaryen copied to clipboard

Avoid SmallSetBase heap allocation in default constructor

Open Squareys opened this issue 3 years ago • 12 comments

Hi @kripken & @sbc100 !

As described in https://github.com/WebAssembly/binaryen/issues/4165#issuecomment-1176486775, this is the change that wraps the std::set in the SmallSetBase into a std::optional to avoid the heap allocation that creates the red-black-tree root on MSVC STL.

I made one drive-by change in a separate commit that avoids a copy per element in the for-each of the std::initializer_list constructor of SmallSetBase.

Best, Jonathan from Wonderland Engine.

Squareys avatar Aug 06 '22 20:08 Squareys

Thanks for the review! I'll have time to apply the requested changes and split the changes from effects.h tomorrow evening CEST.

Squareys avatar Aug 08 '22 20:08 Squareys

@kripken I applied the feedback and split off the changes to effects.h 👍 Opening a PR for that in a few moments, benchmarking a few alternatives to 10

Squareys avatar Aug 09 '22 14:08 Squareys

@kripken Please consider the benchmark and analysis here, on the other PR before merging this.

While it's a net-positive, it does pessimize the code without the EffectsAnalyzer change, which seems to be a special case that benefits massively from avoiding the allocation, possibly allocated especially often.

Squareys avatar Aug 09 '22 15:08 Squareys

@kripken Changed the comment and rebased. Sorry for the crazy long delay!

I'll be rebasing #4885 next, which is waiting for this to land.

Squareys avatar Sep 28 '22 13:09 Squareys

Looks like there are some errors on CI.

kripken avatar Sep 28 '22 16:09 kripken

Rebased onto latest main and fixed the failing test case.

Sorry for the crazy slow iteration cycle times on this, this project was done during a once-per-month "Workflow Improvement Day".

Squareys avatar Nov 23 '22 11:11 Squareys

Note, while rebasing #4885, I noticed, that SmallSet::erase() was returning void instead of size_t like std::set does. I added a commit to update that, such that the code which depends on it in effects analyzer compiles.

Squareys avatar Nov 23 '22 12:11 Squareys

Code lgtm, also ran a quick local benchmark and looks good. Once tests pass this will merge.

kripken avatar Nov 23 '22 18:11 kripken

Looks like there is a test error though.

kripken avatar Nov 23 '22 18:11 kripken

Looks like there is a test error though.

Indeed, will find time to have another look before end of the year.

Squareys avatar Nov 28 '22 09:11 Squareys

end of the year.

Oh well... sorry this is taking so long.

It's not a test failure, but likely a clang or GCC compiler difference, so I'd appreciate if you could approve the workflow as the previous logs expired. I just rebased to trigger the workflow run.

Squareys avatar Mar 28 '23 15:03 Squareys

I re-ran the workflow now. (It seems like it needs to be approved each time for first-time contributors, unfortunately.)

kripken avatar Mar 29 '23 17:03 kripken