Skip to content

Conversation

james7132
Copy link
Contributor

@james7132 james7132 commented Aug 28, 2025

Partially fixes #78.

This attempts to minimize the amount of monomorphized codgen created while using this crate.

  • Header<M> has been split into Header and HeaderWithMetadata<M>, using repr(C) to keep pointers trivially castable between each.
  • Move any header-only operations onto the Header type.
  • Move out multiple entries of the TaskVtable to operate without every generic argument on RawTask, ,just the ones they need.
  • Make functions like get_output a function on the vtable itself instead of put in the vtable, both avoids an extra dynamic dispatch call, and shrinks the vtable.
  • Embed the RawWakerVtable in the TaskVtable and pass it into polymorphized functions.
  • Use #[inline(never)] on these polymorphized functions.

This PR also shifts to use byte_add, which does bump the MSRV to 1.75. If this is too much of a bump for such a gain, I can revert that.

Benchmarks:

master
test task_create ... bench:          35.94 ns/iter (+/- 0.53)
test task_run    ... bench:          40.06 ns/iter (+/- 0.55)

polymorphization
test task_create ... bench:          37.36 ns/iter (+/- 1.69)
test task_run    ... bench:          40.63 ns/iter (+/- 1.14)

@james7132 james7132 marked this pull request as ready for review September 7, 2025 01:42
@james7132 james7132 requested a review from notgull September 7, 2025 21:01
Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

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

Looks good to me overall. Sorry for the delay.

Do we have evidence that this actually reduces polymorphization in practice? Like, if we compile Bevy before and after this patch, how much does the code size decrease.

src/raw.rs Outdated

// Finally, deallocate the memory reserved by the task.
alloc::alloc::dealloc(ptr as *mut u8, task_layout.layout);
fn header<'a>(ptr: *const ()) -> &'a Header {
Copy link
Member

Choose a reason for hiding this comment

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

Mark this as unsafe

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just went ahead and deleted the function. It's was only used in one location.

@james7132
Copy link
Contributor Author

@notgull there is the (admittedly artificial) benchmark results seen here: #66 (comment).

Bevy actually uses surprisingly few new instances of tasks (generally not used in generic code), and has other compilation bottlenecks.

@james7132
Copy link
Contributor Author

Went ahead and reverted the byte_add changes and instead chose to polyfill it. I don't think it's worth bumping the MSRV of the entire crate for.

Copy link
Member

@notgull notgull left a comment

Choose a reason for hiding this comment

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

Looks good to me!

@james7132 james7132 merged commit 20afb72 into smol-rs:master Sep 13, 2025
8 checks passed
@james7132 james7132 deleted the polymorphization branch September 16, 2025 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Duplicated raw function monomorphizations
2 participants