News for hackers. Stuff that matters.
Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by sebstefan on Friday April 12, 2024 @05:12AM (#40010732)
I got confused through it ; is the resulting code still technically safe?
Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by v0y4g3r on Friday April 12, 2024 @05:17AM (#40010768)
It's technically `safe` as long as you never access the decoded struct once the original bytes is dropped.

I believe that how `unsafe` works, the programmer, instead of compiler, ensures safety.

Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by pflanze on Friday April 12, 2024 @06:05AM (#40010980)
Yes, but their code appears to actually be `unsafe` (in the Rust terminology sense) without specifying that in their function declarations. They use `unsafe` inside their `slice` function, but return a value that is unsafe to use, hence `slice` should be marked `unsafe`, as should `copy_to_bytes` and then `merge_bytes`. Same for PromLabel::merge_field and PromTimeSeries::merge_field as far as I can see, and maybe higher up in their actual app. This is definitely not how Rust code is supposed to work, if a function isn't marked unsafe, it should not be allowed to introduce UB; they violate that. This approach is on par security wise with C/C++ code iff programmers are aware of the pitfalls, which normally isn't the case since Rust programmers expect non-unsafe functions to be safe (i.e. not require additional care to avoid undefined behaviour).

They either need to mark their functions `unsafe`, or use lifetimes (which may require changes in some APIs, which may be the reason they didn't).

Re:Fivefold Slower Compared to Go? Optimizing Rust's Protobu... by pflanze on Friday April 12, 2024 @12:51PM (#40015003)
I was looking at the main branch, and described the situation there. They have a different branch for the optimization work; in that branch, they do mark those functions as `unsafe` (and already did when I posted).