tomaka / hlua

Rust library to interface with Lua
MIT License
507 stars 48 forks source link

[Long-term] Specialize wrapper functions for 0-sized closures #108

Open tomaka opened 7 years ago

tomaka commented 7 years ago

Very long term issue. Requires specialization to be stable and a way to specialize for functions and closures that don't borrow any environment.

tomaka commented 7 years ago

In order to be a bit more specific, in Rust a closure is composed of two things: a function and an "environment". The environment contains all the local variables that were captured by the closures.

For example if you do let a = 5; move || a + 3 then the environment of the closure is the variable a.

In order to make this work, hlua asks lua to allocate some memory, copies the environment inside it, pushes the environment as a "hidden parameter" for the C callback, and when the C callback is called the environment is loaded and the closure is recreated.

However many closures and all freestanding functions don't have any environment at all. In this situation hlua still asks lua to allocate 0 byte of memory, pushes the hidden parameter, and when the C callback is called it asks lua to load the zero-sized data. This issue is about removing all these useless steps.

jonas-schievink commented 7 years ago

rust-lang/rust#39817 can possibly help here