172 points · by bubblehack3r · 3y ago · 130 comments
Different tools for different needs!
I appreciate this, as looking for better tools in embedded space is welcome. Just pity that so many of them come with so many dependencies and large library sizes.
https://esp-rs.github.io/book/ https://github.com/avr-rust/ruduino
I know several easy ways to boost ipnsort's performance by 10% but don't because they don't align with my binary-size and compile-time goals.
What about rust today makes it suitable for replacing some code but not all of it? What’s better, what still isn’t there yet?
I guess from my limited experience register fiddling ergonomics in rust were miserable, but I was blessed with working with an extremely safe and ergonomic set of c macros at work (you could do something like rmw(i2s, clockconfig, enable, set) and know if it compiled that such a value corresponded to a valid value in a field that existed in such a register in such a peripheral) and I know some vendors provided c “pac” equivalents that were pretty sloppy and error prone, if still nicer than all the punctuation needed to set a bit in a register in rust. How is HAL quality for popular platforms? How much does that matter? The stm provided c HAL is extremely limiting in my experience, anything fancy requires bypassing it, and I worked places without touching the vendor provided libraries ever but I guess having it as an option for popular platforms is important.
It seems to be getting better though. For example... https://tweedegolf.nl/en/blog/65/async-rust-vs-rtos-showdown
Support outside of ARM is a bit rough, but it's improving for RISC-V and Espressif.
Regarding `ValWithPtr` my goal was to make it as close semantically as I could to the Rust code while keeping the example to a minimum to avoid distracting from the main point. If you have a concrete idea how `ValWithPtr` could have been modeled better given these criteria, please let me know.
For ValWithPtr to make sense it needs its semantics defined. Correct C++ types can be deep copying, move only, reference counted, singleton instance etc. For example typed deep copy version without attempting any verification (link to compiler explorer): https://godbolt.org/z/3sesYY8of
If it is not helpful for you please feel free to ignore all of the above.
How does this differ from implementing GC in C++ then showing how much better Java does?
* The number of items being sorted must be a power of 2 (2^n).
* The number of comparisons it makes is larger than other sorting algorithms like merge sort, which will make it slower in non-parallel environments in many cases.
Bitonic sort has the advantage that it can be implemented to run in highly parallel environments (hardware, FPGAs, etc.), where the cost of comparisons is offset by them operating in parallel, so the sort completes faster even though more comparisons are occurring.
The power of 2 requirement only applies to the core kernel.
All CPUs in usage today are parallel (in 3 to 4 different ways), and bitonic sort is the best performing sort on both amd64 and aarch64.
The obvious thing is to use SIMD.
There is truth in this, but I'm not sure whether the reader can/should extrapolate this to larger situations (not that the author implied we should, but it was my first interpretation).
We know that in certain situations, borrow checking works really well and allows us to guarantee safety with minimal friction and no overhead.
But there are other cases where safety and performance _are_ in contention, and we must choose one or the other. Anyone who has been forced to satisfy the borrow checker by using a .clone(), using Rc, or refactoring objects into a hash map and referred to them by an ID (that must be hashed to exchange with a reference), has felt this contention. In https://verdagon.dev/blog/myth-zero-overhead-memory-safety, I concluded that there's no general approach that always has zero overhead, at least not yet.
So perhaps the best interpretation from this study is that often, for small enough programs/areas, there is no conflict between safety and performance.
For larger programs with more complex requirements and data interrelationships, the question becomes much more interesting.
> I see no reason why a straight port from Rust to C++ wouldn't have been possible while satisfying their requirements.
Like the author, I also don't see a reason for this, but I've never tried myself. I've always thought that with the restrict keyword, one could make any C++ as performant as any Rust code. Perhaps something else got in the way there.
However the current state of this is absolutely terrible. A lot of work needs to be done to improve the development experience with this model if it is to be used effectively in this way.
The borrow checker is known to be far in to “overly cautious” territory.
Rather, I think that the static analysis we see in today's languages just isn't powerful/flexible enough to reason about safety in a lot of the patterns that we know are safe. I'm also uncertain if it can _ever_ catch up to what we know to be safe, but I wouldn't be surprised if we get there in a few hundred years.
For example, borrow checking is a step forward and can guarantee safety, but does nothing about the other half of correctness, specifically liveness. [0]
Linear types (like in Austral [1] and Vale's higher RAII [2]) can help guarantee liveness, but we still have further to go.
Both are based on single-ownership (in the C++ sense) like Rust, which introduces errors that e.g. Haskell would not.
But even Haskell (and LiquidHaskell which has linear types) don't go far enough; Coq goes even further.
So yes, like you say, we have a long way to go w.r.t. correctness, even past the borrow checker though it is a big step forward.
To my original point though, even all of these tools put together will put restrictions on a program such that it sometimes won't be allowed to take the most optimal approach. Perhaps someday we'll get there!
[0] https://en.wikipedia.org/wiki/Safety_and_liveness_properties
Yes. You literally did assume that the borrow checker is an authority, as seen here.
To be frank with you, given that zig is 95% of the way there, I feel that you are “diving off the deep end” when stating things like “Haskell doesn’t go far enough”. Haskell typing system is a nice experiment, but I don’t believe to be good in any capacity, let alone “not going far enough”.
Haskell, in my opinion, a great case of “solving a problem before even asking what the problem really is”.
https://github.com/Voultapher/sort-research-rs/tree/main/ipn...
I mean, even if the sort function is implemented in such a way that it's impossible to use it incorrectly, the user is still programming in C/++. Yes, all else being equal, the harder it is to introduce bugs the better, but if the user is not careful they will shoot themselves in the foot one way or another.
If you try to sort with a function that's not a valid comparison operator, I don't know what to tell you. What should it do?
Semantics cannot be validated at compile time. The best that can be done is to annotate the function as "yes this should have the right semantics" as is done in Rust and C++ concepts, but that's still not a guarantee.
If I'm reading the article correctly, the user can be informed at runtime with little to no performance impact. That certainly sounds preferable to returning an unsorted list (that may not even match the input).
Almost every Rust sort does this, but no C/C++ sorts do. That appears to support that author's conclusion that this is a cultural thing.
It's not possible to verify a function is a strict weak ordering without enumerating the entire domain.
I think a sort implementation should return elements in an order consistent with a subset of the comparison calls it makes, and that subset should be such that it fully determines the order.
An even better guarantee is that the subset should be the whole set of comparison calls.
It is absolutely possible to write a comparator that evaluates things in a circle e.g. 1<2 && 2<3 && 3<1.
At that point it is impossible to "do your best" there is no correct answers only wrong ones. (You have to violate a comparison here)
Rather, it can ask for two comparisons and act according to the results (e.g. it learns that 1 < 2 and 2 < 3 and returns [1, 2, 3]).
This makes the output unspecified in general, but it will be consistent with the information received and the calls made even if the comparator doesn't form an ordering, which is what one would expect.
> An even better guarantee is that the subset should be the whole set of comparison calls.
For consistent comparator functions, all decent implementations “return elements in an order consistent with a subset of the comparison calls it makes, and that subset should be such that it fully determines the order”, because that’s the definition of sorting.
They also have the added feature that that subset is the full set of the comparison calls they make. Why would they make more calls than necessary?
For buggy comparator functions, once you hit even a single inconsistency it can’t be the whole set of comparison calls.
Also, for a buggy comparator function, you can’t count on the comparator function to be antisymmetric, so it may both say that a < b and b < a, and you can’t even count on it returning the same value when called twice with the same arguments (a comparator could return a coin flip, for example)
However, if you’re willing to make all the n × (n - 1) comparison calls, it seems reasonable to me that you can find a subset that defines an ordering.
I can only think of an heuristic argument for that, though, not of a proof. That argument is that there are n! possible orderings you can return, and each has a ‘chance’ of 1/2^(n-1) of only containing links that are consistent with the comparator function, and the former is way larger than the latter. Given that chances are at least one would be consistent, and define the order.
However, I don’t see what good that would do. If your code can detect that the comparator function is buggy, it’s better to signal that than to spend time finding some semi-random ordering that partially satisfies the comparator function.
I haven’t ever even thought about the issues laid out here in such detail.
At first I was scratching my head, but the strength of a sorting guarantee actually might matter a lot more than I first thought.
Assuming that something is sorted can have quite substantial effects on code. It’s a very strong assumption in a sense.
“ Overall no correlation between performance and safety could be found, nor whether safe or unsafe internal abstractions are used.”
> As seen in the benchmarks, the current Rust standard library unstable sort implementation outperforms the C++ standard library counterparts
And I can't find any generalizations like that from the author, though I may have missed it.
Hell everything only works by interoping with those languages given the OS is written in one of them.
C++ yes, there is no real reason to use C++ anymore outside of big libraries that require it.
Some of us like C++ because it gives us a the freedom to work at the lowest level when we need to, while also giving us plenty of higher level APIs for most day to day situations, while having great compatibility with tons of software that is already out there. It seems we are at peak Rust fanboyism these days. I have nothing against Rust but the idea that Rust has already replaced C++, or will certainly do so in the future, is ludicrous. C++ has a ton of activity in the standards committee and has very interesting developments like cppfront. It is a vibrant community that continues to reinvent itself and in all likelihood is a lot larger than the Rust community.
Mixing of these two is why nobody really takes C++ seriously anymore.
If C++ dissalowed C style memory accessors, removed <reinterpret_cast> and everything was done through stdlib and smart pointers, it would probably be above Rust right now. A good portion of Rust borrow semantics were already built to the smart pointer system.
But with mixing, you not only have to deal with all the typing syntax, you also have no idea if you are just going to segfault because there is a C style dereference somewhere to a null pointer.
No one will ever take it seriously.
The reason I responded to you though, is because comptime is not strictly for performing business logic at comptime. Most comptime uses are for the reification of code.
Fastest program ever.