News for hackers. Stuff that matters.
Re:Unreal Rust by Animats on Sunday September 04, 2022 @05:56PM (#32718089)
This is great, because Unreal is a C++ project that's not so easy to just Rewrite In Rust.

True. Although rewriting Nanite in Rust would be useful. Nanite is a very clever data structure for meshes. It's brittle, and full of internal relative addresses within the data item, so it's very vulnerable to buffer overflows. If it becomes widely used outside of UE, it might become something that gets supported in GPU hardware.

Re:Unreal Rust by RcouF1uZ4gsC on Sunday September 04, 2022 @06:05PM (#32718164)
> It's brittle, and full of internal relative addresses within the data item,

I am not sure how much you would gain by doing this.

Internal references are one of the things where it is very hard to express in safe Rust what you want to do (see doubly linked lists and graphs). You can get around that by using vectors and indexes, but that is bypassing the borrow checker, and with it, much of your safety and mutability checks.

Re:Unreal Rust by oconnor663 on Sunday September 04, 2022 @06:27PM (#32718333)
I always want to be careful with phrases like "bypassing safety checks", because of course the Vec/index pattern is 100% safe and (like any 100% safe Rust program) will (approximately) never trigger undefined behavior. But it's true that what was formerly undefined behavior will still be a bug, where a stale index accidentally refers to a new object that happens to get allocated in the same spot. You can use slabs and generational indexes to catch these bugs, but I don't know whether the performance cost of generational indexes is acceptable in this case.
Re:Unreal Rust by ChickeNES on Sunday September 04, 2022 @07:45PM (#32718881)
> here a stale index accidentally refers to a new object that happens to get allocated in the same spot.

Given we're talking video games, you could also not worry and let speedrunners take advantage of it... ;)

Re:Unreal Rust by amelius on Sunday September 04, 2022 @06:22PM (#32718294)
Yes, Rust is great for tree-like data structures. However, add one non-tree edge to your graph and you're in big trouble.

This demonstrates the flexibility of GC'd languages, where you don't paint yourself into a corner so easily.

Re:Unreal Rust by Animats on Sunday September 04, 2022 @07:26PM (#32718777)
Yes, Rust is great for tree-like data structures. However, add one non-tree edge to your graph and you're in big trouble.

Good summary. Most of the headaches I have in Rust come from needing something that's not a simple tree. Parts that are simple trees are really easy.

Nanite is a DAG, by the way. The whole point of Nanite is that instances are combined, recursively. A mile of chain link fence would be highly detailed sections of wire, combined into full wires, combined into fence panels... But not just all simple repeats. If there's a hole in the fence somewhere, that can be represented.

Re:Unreal Rust by stouset on Monday September 05, 2022 @02:52AM (#32721309)
I really don’t get this perspective. People act like you need one self-referential struct and suddenly you’ve gotta switch languages.

Just use unsafe! That’s what it’s there for. You can use internally-unsafe semantics to expose an externally-safe API and nobody needs to care.

You get the Rust’s safety benefits everywhere else (not to mention all the other nice parts) and your unsafe code is limited only to the areas it’s truly necessary.

Re:Unreal Rust by amelius on Monday September 05, 2022 @05:12AM (#32722108)
You can't always hide the problem behind an API, because for instance an API may call back into your code and access data. Or it may want to do that in the future. The "unsafe" world will spread like a virus.
Re:Unreal Rust by stouset on Monday September 05, 2022 @03:05PM (#32728001)
Absolutely ridiculous.

Rust itself is written using plenty of unsafe code, all of which runs on top of fundamentally-unsafe CPU architectures. And yet it manages to expose a safe API to its users who somehow to write memory-safe Rust programs every day.

> You can't always hide the problem behind an API, because for instance an API may call back into your code and access data.

This is quite literally all that Cell and RefCell do and they hide this behavior behind a safe API.

Do memory-safety leaks in supposedly memory-safe abstractions crop up occasionally? Sure, nobody's perfect. But this idea that unsafety is uncontainable and spreads out everywhere until your whole program is infected and riddled with memory-safety bugs is completely detached from reality and regularly disproven by Rust developers on a daily basis.

Re:Unreal Rust by amelius on Tuesday September 06, 2022 @06:16AM (#32735104)
Rust is still in a research phase. People are still figuring out how to do certain very basic stuff without a garbage collector. See e.g. how long it has been taking to get a decent GUI library with callback functions that can be closures that reference external parts of data.

In my view it is silly to do very high-level stuff in a systems language that was never intended for that purpose, and when you can clearly see the downsides of that approach; you are thinking of memory issues a large part of the time instead of being able to focus on your high-level functionality or even high-level security issues. Of course you can try to do it, but then it is research, not practical or necessarily sustainable programming.

Re:Unreal Rust by stouset on Tuesday September 06, 2022 @08:01PM (#32744728)
> Rust is still in a research phase. People are still figuring out how to do certain very basic stuff without a garbage collector. See e.g. how long it has been taking to get a decent GUI library with callback functions that can be closures that reference external parts of data.

Are Golang, Ruby, Python et al still in the "research phase"? After all, none of them have native GUI frameworks written in them yet.

That said, you're moving the goalposts. The entire premise of my original reply was objecting to your claim that Rust paints you into a corner should you need a non-tree-like data structure. Which is absolutely ridiculous, there are countless examples of self-referential datastructures written in Rust. They use unsafe, which is what it's there for.

Re:Unreal Rust by twic on Monday September 05, 2022 @04:48AM (#32721948)
Original poster said of Nanite:

> It's brittle, and full of internal relative addresses within the data item, so it's very vulnerable to buffer overflows.

I think the hope was that rewriting it in Rust would make it more robust. But if the only way to write it efficiently is to unsafe your way to writing C++ in disguise, it's not clear it will be any more robust.

Re:Unreal Rust by stouset on Monday September 05, 2022 @05:20PM (#32729628)
You're casually ignoring the entire rest of your program, which can now be written in safe Rust.

Just because some part of your program needs to use features unavailable in safe rust doesn't mean your entire program might as well be written in asm.

Re:Unreal Rust by Animats on Monday September 05, 2022 @04:12PM (#32728898)
"Just use unsafe! That’s what it’s there for."

No, no. "Unsafe" should not be used because you can't figure out how to do some data structure in Rust. You'll get it wrong.

I have 27,000 lines of Rust in one complicated program with no "unsafe".

Re:Unreal Rust by stouset on Monday September 05, 2022 @05:19PM (#32729607)
First, please consider the context of my comment.

Second, Rust itself is implemented with unsafe. Self-referential data structures are necessary for some problem domains, and those must be implemented with unsafe. Unsafe exists and is a tool that is sometimes necessary. When it is necessary, it should be used carefully.

Just because you haven't needed unsafe personally doesn't mean others don't. And the Rust developers aren't superintelligences capable of using it correctly while the rest of us mere mortals aren't.

Re:Unreal Rust by newpavlov on Sunday September 04, 2022 @06:07PM (#32718178)
>Nanite is a very clever data structure for meshes. It's brittle, and full of internal relative addresses within the data item, so it's very vulnerable to buffer overflows.

Using bounds checking is likely to kill performance on such data structures, while using `get_unchecked` or raw pointers will keep the issues.

But even then, I would love to see Nanite implemented in Rust.

Re:Unreal Rust by pjmlp on Monday September 05, 2022 @02:15AM (#32721107)
It is a matter of how much the compiler is able to ellide.
Re:Unreal Rust by cfcosta on Monday September 05, 2022 @06:58AM (#32722927)
Kinda late to the party, but just a heads up: in rust, bounds checks are only available on debug builds. I'm not familiar with the internals of Nanite though, so I can't comment on how safe (or unsafe) it should be to implement.
Re:Unreal Rust by wmanley on Monday September 05, 2022 @08:54AM (#32723755)
> in rust, bounds checks are only available on debug builds

This is not true - bounds checks are always enforced: https://play.rust-lang.org/?version=stable&mode=release&edit...

Maybe you're confusing it with signed overflow behaviour in Rust or bounds checks in Zig?

Re:Unreal Rust by steveklabnik on Monday September 05, 2022 @07:35AM (#32723175)
I’m not sure what you mean here, bounds checks have nothing to do with debug builds, other than that optimizations may remove more of them if they’re determined to never fire, and release builds tend to have higher optimization levels. Semantically they’re always on unless you use the get_unchecked method.