Skip to content

Conversation

@tlively
Copy link
Member

@tlively tlively commented Aug 29, 2024

Use the new TopologicalSort and MinTopologicalSortOf utilities instead
of the old CRTP topological sort utility and a bespoke heap-based
topological sort in ReorderGlobals. Since there is no longer a heap to
pop from, the direction of the custom comparator is now much more
intuitive.

Further simplify the code by switching from tracking the new order of
globals using a sequence of new indices to tracking the order using a
sequence of old indices.

This change also makes the pass about 20% faster on a large real-world
module.

Reuse the code implementing Kahn's topological sort algorithm with a new
configuration that uses a min-heap to always choose the best available
element.

Also add wrapper utilities that can find topological sorts of graphs
with arbitrary element types, not just indices.
@tlively tlively requested a review from kripken August 29, 2024 03:36
Copy link
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.

Nice!

Sounds like you may have already done this, but it would be good to verify that this produces identical output (compared to before this PR) on a large J2Wasm program, as those are the main real-world files that have tons of globals, including globals with dependencies (vtables/itables).

@tlively
Copy link
Member Author

tlively commented Aug 29, 2024

I'll verify that the output is the same on j2wasm output. Your thorough tests for this pass were super helpful in ironing out all the bugs.

Use the new TopologicalSort and MinTopologicalSortOf utilities instead
of the old CRTP topological sort utility and a bespoke heap-based
topological sort in ReorderGlobals. Since there is no longer a heap to
pop from, the direction of the custom comparator is now much more
intuitive.

Further simplify the code by switching from tracking the new order of
globals using a sequence of new indices to tracking the order using a
sequence of old indices.

This change also makes the pass about 20% faster on a large real-world
module.
@tlively tlively force-pushed the min-topological-order-of branch from 85702ce to 946e597 Compare August 29, 2024 20:40
Base automatically changed from heap-toposort to main August 29, 2024 22:07
@tlively tlively merged commit 871ff0d into main Aug 29, 2024
@tlively tlively deleted the min-topological-order-of branch August 29, 2024 22:08
@gkdn gkdn mentioned this pull request Aug 31, 2024
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