uazu / stakker

A lightweight low-level single-threaded actor runtime
https://uazu.github.io/stakker/
Apache License 2.0
167 stars 9 forks source link

Consider small-Ret optimisation to avoid allocation #6

Open uazu opened 3 years ago

uazu commented 3 years ago

Ret is boxed, which means it needs to allocate memory (except for ret_nop!). It would be nicer if small Ret instances could be handled without allocation. Then a whole round-trip to another actor (call! and ret!) could be done with no allocations at all.

A simple callback to an actor method with no captured data apart from the actor ref should fit in the same space as the current Ret. This should be possible by taking the FnOnce apart like is done to serialize FnOnce for the FnOnce queue. Upside is saving a malloc and free. Downside is needing unsafe code, and needing more code on creation and on calling through a Ret to switch between two alternatives (switching based on the size of the FnOnce). The existing safe implementation can be kept for the 'no-unsafe' feature.

Currently Ret is two usizes. It could be expanded to 3 or 4 usizes which would allow capturing more data without allocation, but at the cost of making all Ret instances bigger. 3 might be optimal if the application has a lot of callbacks that capture the actor ref and some index to represent the context the callback is related to. It might be worth it for some applications, so maybe it could be a cargo feature.