Remove petgraph from bevy_ecs#15519
Conversation
Replaced with specialised solution based on original types.
|
You probably want to verify that |
I do agree, Additionally, adding a |
alice-i-cecile
left a comment
There was a problem hiding this comment.
I'm very pleased to see this trimmed down. I think there's a good chance we want a more prinicipled bevy_graph at some point (or just use systems-as-entities), but this is an excellent ECS code quality / maintainability change in its own right, independent of the no_std implications.
|
@DasLixou if you're around, I'd appreciate your eyes here :) |
There was a problem hiding this comment.
Interesting change, I wonder how it performs and how much is really stripped away.
Also good to know that a bevy graph crate could need such fine grained specialization, that's something I had in mind, but I'm currently a bit "frustrated" at coding, no bevy graph in near time so this is cool to have :D
|
|
||
| iter.copied() | ||
| .map(CompactNodeIdAndDirection::load) | ||
| .filter_map(|(n, dir)| (!DIRECTED || dir == Outgoing).then_some(n)) |
There was a problem hiding this comment.
Why do we only use Outgoing edges for directed graphs? aren't neighbors also incoming edges?
There was a problem hiding this comment.
This is a hangover from petgraph, but I believe the rationale is that a neighbour is something you can go to from your current node. So in a <-> b, both a and b are neighbours. Whereas, in a --> b, b is a neighbour of a, but not the other way around. I could change this, but I wanted to keep the API here as-close-as possible to petgraph to avoid subtle translation errors for contributors moving to the new type.
- Removed instances of `&NodeId` in function arguments since `NodeId` is copy and only two `usize`'s wide. - Changed documentation references to `O(V)` to `O(N)` (nodes instead of verticies) Co-Authored-By: Lixou <[email protected]>
Co-Authored-By: Lixou <[email protected]>
I haven't tested the performance myself (currently only have access to a pretty mediocre laptop!) but I suspect a marginal performance improvement at runtime, and a negligible compilation boost (I observed Packing the
I am hoping that bringing this type in-tree and specialising to just empty edges and |
Allows for a minimal-allocation iterator interface. Also switched to `SmallVec` to improve performance in sparse graphs (the more likely use-case)
|
Ok so I did some benchmarking and it appears that this change might've been more impactful than I had planned! cargo bench -- schedule --load-baseline NoPetgraph --baseline MainFor benchmarks that run in less a few milliseconds, I'd call that a wash (plus or minus 10% is within variance that I'd expect from the binary layout changing). However, for the |
|
Did some additional testing on my desktop (Ryzen 5950X, Windows 10, 64GB or RAM) to see if the benchmark results were repeatable: cargo bench -- schedule --load-baseline NoPetgraph --baseline MainLooks like that ~30% performance uplift is repeatable (in this case up to 45% even!), so I'm more confident in saying there's a fairly significant performance boost here. Looking at the
As to why there's a performance uplift, I can think of a couple changes I made which might be the reason:
Aside from the above, I'm not sure what else could really affect performance this drastically. Aside from the |
Will this effect the toposort? The current toposort isn't stable, but we do eventually want to make the topo sort stable. So users don't get their game randomly behaving differently in different |
No, actually! The reason is the nodes are all sorted in an |
The `nocase` naming was a hangover from the `petgraph` migration. Co-Authored-By: vero <[email protected]>
hymm
left a comment
There was a problem hiding this comment.
I didn't do a super close review of the graph algorithms, but otherwise looks good.
|
Waiting until 0.16 for stability reasons, but I will merge this. |
# Objective - Contributes to bevyengine#15460 ## Solution - Removed `petgraph` as a dependency from the `bevy_ecs` crate. - Replaced `TarjanScc` and `GraphMap` with specialised in-tree alternatives. ## Testing - Ran CI locally. - Added new unit tests to check ordering invariants. - Confirmed `petgraph` is no longer present in `cargo tree -p bevy_ecs` ## Migration Guide The `Dag::graph` method no longer returns a `petgraph` `DiGraph` and instead returns the new `DiGraph` type within `bevy_ecs`. Edge and node iteration methods are provided so conversion to the `petgraph` type should be trivial if required. ## Notes - `indexmap` was already in the dependency graph for `bevy_ecs`, so its inclusion here makes no difference to compilation time for Bevy. - The implementation for `Graph` is heavily inspired from the `petgraph` original, with specialisations added to simplify and improve the type. - `petgraph` does have public plans for `no_std` support, however there is no timeframe on if or when that functionality will be available. Moving to an in-house solution in the interim allows Bevy to continue developing its `no_std` offerings and further explore alternate graphing options. --------- Co-authored-by: Lixou <[email protected]> Co-authored-by: vero <[email protected]>

Objective
no_stdBevy #15460Solution
petgraphas a dependency from thebevy_ecscrate.TarjanSccandGraphMapwith specialised in-tree alternatives.Testing
petgraphis no longer present incargo tree -p bevy_ecsMigration Guide
The
Dag::graphmethod no longer returns apetgraphDiGraphand instead returns the newDiGraphtype withinbevy_ecs. Edge and node iteration methods are provided so conversion to thepetgraphtype should be trivial if required.Notes
indexmapwas already in the dependency graph forbevy_ecs, so its inclusion here makes no difference to compilation time for Bevy.Graphis heavily inspired from thepetgraphoriginal, with specialisations added to simplify and improve the type.petgraphdoes have public plans forno_stdsupport, however there is no timeframe on if or when that functionality will be available. Moving to an in-house solution in the interim allows Bevy to continue developing itsno_stdofferings and further explore alternate graphing options.