r/cpp
•
u/steveklabnik1
•
Oct 24 '24
•
1
Why Safety Profiles Failed
https://www.circle-lang.org/draft-profiles.html
180
Upvotes
View all comments
Show parent comment
3
u/duneroadrunner Oct 26 '24 edited Oct 26 '24
I'm not sure I understand exactly what you're asking here. Aliasing can certainly be "banned". Rust and the Circle extensions impose a complete ban on mutable aliasing of (raw) references. As I mentioned, scpptool only prevents it when it affects lifetime safety.
The main reason that scpptool doesn't universally restrict mutable aliasing is because the goal of the scpptool solution is to, as much as possible, remain compatible with traditional C++ and maintain performance while ensuring memory safety.
This high degree of compatibility with traditional C++ means that existing code bases can be converted to the scpptool-enforced safe subset with much less effort than some of the alternatives which require the code to be essentially rewritten.
There is also a theoretical performance argument for not universally banning mutable aliasing. If, for example, you consider a scenario where you have a function,
foo, that takes two arguments of typebar_type, each by mutable/non-constreference. Now say you want to call that function with two (different) elements in an array (ofbar_types). In C++ (and the scpptool-enforced safe subset), you can simply obtain a (non-const) reference to each of the desired array elements and pass them to the function.In Rust, for example, this would be an aliasing violation. My understanding is that you would either have to slice the array (which incurs at least a bounds check), or move/copy one of the elements out of the array into a local copy and pass a (
mut) reference to the local copy to the function, and then move/copy the (possibly modified) value of the local copy back to the array element.The first option is presumably generally cheaper than the second option, but theoretically still not free. But not all Rust containers support slicing. If, for example, it had been a hash map instead of an array, then you'd be stuck with the generally more expensive option. (There are other workarounds, but I'm not sure they'd be any better.) (Can someone out there correct or verify this?)
Of course the universal prohibition of aliasing also has performance advantages in some cases. But overall, I think it's a theoretical net performance negative. But presumably, in practice, smart optimizers would often be able to minimize the gap.
I'm not sure if this answers your question, but based on its goals, the scpptool solution deems it preferable to prohibit mutable aliasing selectively rather than universally.
edit: changed the example type to make it clearer