amethyst / specs

Specs - Parallel ECS
https://amethyst.github.io/specs/
Apache License 2.0
2.49k stars 219 forks source link

Safety #765

Closed Imberflur closed 11 months ago

Imberflur commented 1 year ago

Fixes https://github.com/amethyst/specs/issues/647

See API changes section for description.

Checklist

Todo

API changes

This is a breaking change.

This PR fixes several soundness issues that I encountered when working on #737. These include:

Many (but not all) of these issues stem from artificially lengthening the lifetime of the &mut Join::Value within Join::get implementations. We now avoid this and leverage alternative mechanisms including interior mutability and lending iteration.

In more detail, we:

To try to catch any remaining UB, I ran the available tests under Miri . This also identified some issues in dependencies for which I have submitted PRs to fix:

(We need to publish new versions of these to crates.io)

This PR adds Miri to the CI.

Additionally, specs exposed a nightly cargo feature that enabled additional APIs using GATs. Since GATs are now stabilized, I bumped the MSRV to be able to eliminate this feature and remove a bunch of cfg complexity.

Also I introduced AccessMut trait which is similar to DerefMut except it requires explicit use. The associated typeUnprotectedStorage::AccessMut<'_> now requires AccessMut instead of DerefMut. This is to faciliate my work in https://github.com/amethyst/specs/pull/737 where I am exploring a flagged storage type that makes generating modification events more explicit. A blanket implementation of AccessMut for anything implementing DerefMut is included.

Imberflur commented 1 year ago

Exciting news! I finally got a chance to update veloren to use this and profile it and I don't see any particular regressions. Might try to post some tracy pictures later.

I think this should be ready to merge soon.

Imberflur commented 1 year ago

Here are some profiling results in veloren, "this trace" is before changes here and "external trace" is after. I focused on two systems. "character_behavior" which has a join over a lot of component types and "phys" which has multiple joins over fewer component types of which some joins are parallel ones. It seems like there are no regressions and potentially a slight improvement (the profiling conditions have room to be more strictly controlled so I would not trust that this improvement is as significant as it appears here).

image

image

Imberflur commented 12 months ago

@xMAC94x thanks for taking a look :heart:

I will wait till this weekend in case anyone else is interested in reviewing. If anyone interested needs more time just let me know.

zesterer commented 11 months ago

I just wanted to say: thanks for pushing this all the way to completion :) I would have reviewed, but I've been busy with other things recently. Nice work!