mozilla / cubeb-rs

ISC License
61 stars 19 forks source link

Extend `cubeb_log_internal!()` macro and helper functions to avoid heap allocation in `format!()` #73

Closed kinetiknz closed 2 years ago

kinetiknz commented 2 years ago

This builds on the work in PR #70, refactoring cubeb_log_internal_buf_fmt() slightly to accommodate for a variable length list of formatting arguments. The format!() call (which heap allocates a new String) cubeb_log!() uses when invoked with a format string can now be handled by passing the format string and argument list to cubeb_log_internal_buf_fmt() to be formatted via std::fmt::write() into the temporary stack buffer already used by the non-formatting log code path.

I changed the signature of cubeb_async_log() and cubeb_async_log_reset_threads() because a void return in C is () in Rust. It looks like c_void is intended for use with unsized pointers (and that odd C-vs-C++ behaviour with (void) vs () parameters that cubeb_async_log_reset_threads() also demonstrated). The new signature also happens to match the type inside the g_cubeb_log_callback when unwrapped, giving a consistent signature for cubeb_log_internal_buf_fmt() to accept.

Also added MaybeUninit in an attempt to reduce the overhead of cubeb_log!() slightly by avoiding memset()ing 1024 bytes of temporary stack space to 0 on every log call.