Aliasing-xor-mutation WITHOUT borrow-checker

It is not a “good recommendation” in the sense that it’s good practice, it’s a recommendation in the sense that the primary goal is to get the code to compile and work. Once that is done, beginners can focus on performance optimizations. You may view it differently, but I know for a fact (from my teaching activities) that beginners are easily confused that mutating shared data (pointers, references) indeed mutates shared data and not just the perceived separate instance. Cloning gives you a new object to handle as you see fit to start out with, matching this early mental model. From there, you can build a mental model of sharing.

Strictly better? Some frameworks (I think .NET nowadays?) even mix the two, trying to leverage each technique’s strengths when they’re expected to apply. When it was about Mojo’s return behaviour, atomic counters were still lightweight, now suddenly in Rust they should be replaced for inefficiency.

Also, you previously critiqued Rust waits till the end of the scope to drop objects, while Mojo did so immediately via dataflow analysis. That’s basically “maximum ARC” semantics. Now you claim a generational GC, which inherently and intentionally delays deallocations (which has advantages!), is strictly better?

Yes, I’m not saying that’s a wrong goal, I’m saying you’re not very successful at accomplishing that goal. Moving goal posts from post to post and comparing arbitrary things (explicit clone is not the same as implicit copy/move behaviour) is not going to convince anyone of anything. If you believe the community communication around cloning is wrong, that’s a separate issue and unrelated to the language.

So you should be in favour of the Rust variant with lifetimes and critique Mojo for defaulting to refcounts.

I have no idea what this is supposed to mean. RefCell is borrow checking at runtime and that’s done for the same reasons it’s done at compile time. “Simply” using UnsafeCell or raw pointers requires you to manually uphold the safety invariants, which are otherwise safely abstracted by RefCell. What is that API supposed to do? If you want to simply “re-export” unsafe (UnsafeCell, pointers) as safe (references, slices) types, that’s obviously unsound.

Mojo can trivially support them because it does not work the way you initially claimed it did. I said that if it did work the way you initially claimed (and which you also deemed superior), that would make supporting these things nontrivial.

3 Likes

Just set the max pause to microseconds. High quality GC implementations are configurable, as I mentioned earlier, there are configurable max pause parameters that can be passed by the user, and concurrent. Java for example has a GC tuned for latency, a GC balanced between throughput and latency

Reference counted memory management doesn't use Rc because it can't share objects across threads, instead it uses atomics, which in Rust is Arc. Arc turns memory read operations into write operations because it has to update the counter, which invalidates the cache of other threads. This ping pong cache invalidation makes performance bad

Reference counting also causes heap fragmentation when using a global allocator, which makes future allocation speed increasingly slow. In contrast, high quality generational GCs compact the heap so objects sit close together, it makes the access becomes cache friendly

On top of that, in Rust without an arena allocator, allocation speed is slower than Java's GC because it has to search a free list or make a syscall to the OS. In high quality GCs like Java's, it preallocates big enough memory, then allocation is just a pointer increment, and pointer increment is the fastest possible allocation strategy. So for code with heavy heap allocation, most Java code is faster than typical Rust user code, for example in tree like data structures (when not using an arena allocator)

Modern GC algorithms reclaim memory very quickly, and their parameters are configurable, things like max stop the world time, minimum heap occupancy threshold to trigger a collection, and more

Stop-the-world GC (to compact heap) requires ability to
a) stop the world, including all other threads, including threads from any shared library loaded in,
b) update object pointers either upon move or upon subsequent access. The first option requires to know location of each pointer to the object, including those converted to integers, or stored within dyn-objects, or given away via shared libraries' APIs.

That would make Rust less suitable as a systems language, wouldn't it?

Rust beginners don't come to Rust with zero programming experience. Most have already used other languages, typically C/C++ (even if just the basics taught at university, they've already touched pointers), Java, Go, Python, PHP, JavaScript, C#. I myself came to Rust already knowing basic C/C++ including pointers, Go, Python, PHP, JavaScript, and Java. Shared references are actually expected because those other languages work in that way. Cloning into an independent value would actually introduce silent bugs where updates don't reflect on the value, of course they don't, they're independent values now. This already happens with Java strings.

Mojo's reference counter on return only happens when copying a string, whereas in Rust, clone creates a new allocation and copies the value. If you return a reference, it's just returning a memory address with no ref counter. If you return a move, it's just a move. Interestingly, that reference counter in Mojo only happens when copying a string, for vectors it doesn't work, it have to be explicit (I guess it is bug, because yes for String but no for Vector). Where in Rust, Arc gets misused, things that don't need Arc get wrapped in it anyway, like using Arc to fix borrow errors and threads that are waited in place. So codebases end up with Arc everywhere unnecessarily

Mojo's ASAP drop does not use an atomic counter like Arc, it's zero cost. A GC can be configured to control how often it runs, for example a maximum memory threshold like "always collect when unused memory exceeds 64MB" or whatever value you want. And you still get the benefits of faster allocation speed, cache friendliness, and far less cross thread cache invalidation

No, that's not moving the goalposts, that's continuing through a list. If the previous points have been addressed and there are still more items on the list, of course continue to mention the next one. Why is bringing up the remaining items treated as a problem?

Mojo doesn't default to ref counting. It only replaces the string copy operation, which in Rust would become a new allocation and a value copy, becomes a ref counter wrapped. Everything else is the same as Rust, writing a reference gives you a memory address, a move moves the value. Vectors, for instance, are not ref counted. The real big gap is ASAP drop

My point is thatno need to go to the extreme of forbidding unsafe to the point where even hot path data structures use non zero cost constructs that not only has runtime overhead but also prevent optimizations like auto SIMD. It goes even further, code that contains unsafe gets immediately labeled as bad code, even when it can be verified correct. That's why learning unsafe to replace non zero cost constructs matters, it lets you write code that is genuinely on par with other systems programming languages

Take RefCell as an example. People use Rc<RefCell<T>> in data structures because the borrow checker rejects their code even when it's actually correct. But using raw pointers or UnsafeCell directly is both simpler and zero cost, it can even have simultaneous mutable access, and then you can make the methods return references and slices just fine. Neither emits noalias information anyway, RefCell doesn't either, but the pointer/UnsafeCell approach is zero cost, more flexible, and easier to work with. It's sad that because of the "unsafe = bad code" culture, people reach for the runtime overhead option that even prevents optimizations rather than the zero cost, flexible one

Not all Rust users use it for systems programming. Big usage I see is for backend development. If you're doing systems programming, it is simply don't import std::mem::GC, or pass the --disable-gc flag. Those who want convenience or want to replace Arc can use std::mem::GC. Looking at Java's GC performance today, it's certainly possible to have a super high performance GC

Rust will never include a GC. It just won't happen, it's against the ethos of Rust, and it's also pretty much unimplementable (and to the extent it is implementable, there are external crates doing that fine). C++, which had a never-implemented GC in the spec, has finally dropped it (in C++ 20 AFAIK).

Rust will also never have implicit Rc or Arc like Mojo, because it is also against the ethos. While it might be more convenient, Rust favors control and performance over ease of use, and this is worse in both.

Rust might have some form of autoclone like Mojo for Rc/Arc, but even that is deeply contentious inside the community.

If you really want to make Rust better, you should understand its core philosophy and explain why the changes you want will help it. Because as I see them now, they harm it instead.

1 Like

Rust is for system programming first and foremost, and that won't change. Some people are using it besides that, especially when needing extreme performance but also because it has a lot of niceties (unrelated to its low-level features) that are missing in most mainstream languages. It makes sense that such people (the second kind) will benefit more from a Rust-like but simpler language. Many people have discussed that, and how that language should look, in the past. Perhaps that language is Mojo! But Rust won't change its core philosophy for them.

1 Like

You're treating an optional GC in the standard library as if it's a GC baked into the language itself, but I said GC in the std. Those two are very different things. The standard library is not part of the language core. It can be discarded as simply as just not using it, and it won't be included in the compiled output, the compiler only includes code that is actually used. Third party crates tend to get abandoned and their quality improvements are very slow

I never said implicit either. What I said was, add a .clone_ref() method, and when a beginner hits a borrow checker error, the compiler's suggestion should be changed to say "use .clone_ref() if it is acceptable", not the regular .clone(), because that creates a new allocation and does a deep copy

None of these proposals violate Rust's philosophy, because they sit in the standard library, not attached to the language itself. If you want a GC free version, just don't use it. The benefit is that this becomes a selling point to users coming from Python, JavaScript, Java, Golang, etc. If they're struggling with the borrow checker, you can tell them Rust has a GC mode, that it's super flexible and you can pick what you need, and that Rust's GC is extremely high performance combined with its zero cost generic and trait system making it has higher performance than other GC languages, even bad written manual memory management code, with great comptime features (after there is comptime)

Both of these ideas can, and are, implemented in external crates. The first will very probably (I'd say 95%) never be included in std, because it's not common, important or useful enough. The second might, especially if the autoclone proposals are not accepted.

That doesn't change the core philosophy, because it's optional. If you don't use std::mem::GC in your code, you won't be using the GC at all. Code in the standard library only gets included in the compiled output if it's actually used, if it's not used, it won't be included

The problem is that external crates develop slowly, can get abandoned, and are hard for people to discover. It is important and useful too, just look at the user base of Java or Go, it's massive. The reason is that people don't want to deal with tiny details like manual memory management or the borrow checker. So if Rust can introduce an optional GC that outperforms both of them, that would be a massive attraction. After the large number of new users who prefer that convenience adopt it, it will naturally become common

Not autoclone, but a .clone_ref() method as a replacement for the .clone() suggestion

And I just found there's still new room for further improvement that isn't present in Rust yet whether in language or std. I think it would be better to create a separate thread to gather quick improvement ideas for Rust. Allow me to create one

Ok? Why should Rust cater to every preference out there? Java and Go already exist after all… Rust doesn't need to be or do everything.

1 Like

So, sounds like that was incorrect -- Mojo uses a reference-counting-based garbage collector. That seems to be baked quite deep into the language, similar to Swift. That's a totally valid implementation strategy and works very well for many situations, but it's quite distinct from Rust which can work without having any GC at all, not even refcounting -- while still making it easy to opt-in to recounting when that is truly needed.

4 Likes

Are you sure? From what I've been able to find, it uses reference counting, like wrapping everything in Arc or Rc. Does that count as a garbage collector?

Reference counting is essentially a very simplistic form of garbage collection

In the academic literature, "reference counting" is often not considered to be the same thing as "garbage collection" because it has several major differences from all other "automatic memory management" algorithms: the program's code can't be neatly divided into "mutator" and "collector", object lifetimes are much more predictable, and cyclic data structures require special handling.

I thought what was going on here was that Rust makes you say Rc<T> or Arc<T> when you want a reference-counted handle to a T, whereas Mojo bakes this into its basic reference type, but then I skimmed the Mojo documentation and ref T appears not to be a first-class type, just an annotation you can write in a function head that alters calling convention. Mojo might not really have reference-counted handles at all.

My understanding is that String in Mojo is implicitly refcounted, i.e., it's kind of like Arc<str>.

And yeah ref is like & but it's not a first-lass type, more of a binding mode. There's an interesting space of languages that only have second-class "references" (some, like Hylo, don't even use that term).

1 Like

Yeah I think mojo's String is essentially smol_str

This is true with the code you provided, but using &[Cell<i32>] and &mut Cow<'_, [i32]> rather than &[RefCell<i32>] and &mut [Cow<'_, i32>] results in rustc being able to autovectorize the code still. The Cow example doesn't make much sense due modification being guaranteed to be required, but in general &mut Cow<'_, [T]> is a much more common type than &mut [Cow<'_, T>]. However, in real code where you can't hoist the Cow::to_mut call, it is quite likely that autovectorization would not happen without rewriting the code to be easier to autovectorize, or it might simply not be possible.

New code for the first two functions:

use std::cell::Cell;
use std::borrow::Cow;

#[unsafe(no_mangle)]
pub fn with_cell(data: &[Cell<i32>]) -> i32 {
    for cell in data {
        cell.update(|x| x + 10);
    }
    data.first().map(|c| unsafe { *c.as_ptr() }).unwrap_or(0)
}

#[unsafe(no_mangle)]
pub fn with_cow(data: &mut Cow<'_, [i32]>) -> i32 {
    let data = data.to_mut();
    for value in data.iter_mut() {
        *value += 10;
    }
    data.first().copied().unwrap_or(0)
}

Assembly (rustc 1.98, -C opt-level=3):

.LCPI0_0:
        .long   10
        .long   10
        .long   10
        .long   10
with_cell:
        test    rsi, rsi
        je      .LBB0_1
        lea     rcx, [4*rsi]
        add     rcx, -4
        mov     rax, rdi
        cmp     rcx, 28
        jb      .LBB0_6
        shr     rcx, 2
        inc     rcx
        mov     rdx, rcx
        and     rdx, -8
        lea     rax, [rdi + 4*rdx]
        xor     r8d, r8d
        movdqa  xmm0, xmmword ptr [rip + .LCPI0_0]
.LBB0_4:
        movdqu  xmm1, xmmword ptr [rdi + 4*r8]
        movdqu  xmm2, xmmword ptr [rdi + 4*r8 + 16]
        paddd   xmm1, xmm0
        paddd   xmm2, xmm0
        movdqu  xmmword ptr [rdi + 4*r8], xmm1
        movdqu  xmmword ptr [rdi + 4*r8 + 16], xmm2
        add     r8, 8
        cmp     rdx, r8
        jne     .LBB0_4
        cmp     rcx, rdx
        je      .LBB0_8
.LBB0_6:
        lea     rcx, [rdi + 4*rsi]
.LBB0_7:
        add     dword ptr [rax], 10
        add     rax, 4
        cmp     rax, rcx
        jne     .LBB0_7
.LBB0_8:
        mov     eax, dword ptr [rdi]
        ret
.LBB0_1:
        xor     eax, eax
        ret

.LCPI1_0:
        .long   10
        .long   10
        .long   10
        .long   10
with_cow:
        push    r15
        push    r14
        push    r13
        push    r12
        push    rbx
        mov     r15, qword ptr [rdi + 8]
        mov     r12, qword ptr [rdi + 16]
        lea     rbx, [4*r12]
        cmp     qword ptr [rdi], -1
        je      .LBB1_3
        test    r12, r12
        je      .LBB1_15
        add     rbx, r15
        mov     r14, r15
        jmp     .LBB1_7
.LBB1_3:
        test    r12, r12
        je      .LBB1_14
        mov     r13, rdi
        call    qword ptr [rip + __rustc[4b1ee1ef74718a7b]::__rust_no_alloc_shim_is_unstable_v2@GOTPCREL]
        mov     esi, 4
        mov     rdi, rbx
        call    qword ptr [rip + __rustc[4b1ee1ef74718a7b]::__rust_alloc@GOTPCREL]
        test    rax, rax
        je      .LBB1_18
        mov     r14, rax
        mov     rdi, rax
        mov     rsi, r15
        mov     rdx, rbx
        call    qword ptr [rip + memcpy@GOTPCREL]
        mov     qword ptr [r13], r12
        mov     qword ptr [r13 + 8], r14
        cmp     r12, -1
        je      .LBB1_17
        lea     rbx, [r14 + 4*r12]
.LBB1_7:
        mov     rcx, rbx
        sub     rcx, r14
        add     rcx, -4
        mov     rax, r14
        cmp     rcx, 28
        jb      .LBB1_11
        shr     rcx, 2
        inc     rcx
        mov     rdx, rcx
        and     rdx, -8
        lea     rax, [r14 + 4*rdx]
        xor     esi, esi
        movdqa  xmm0, xmmword ptr [rip + .LCPI1_0]
.LBB1_9:
        movdqu  xmm1, xmmword ptr [r14 + 4*rsi]
        movdqu  xmm2, xmmword ptr [r14 + 4*rsi + 16]
        paddd   xmm1, xmm0
        paddd   xmm2, xmm0
        movdqu  xmmword ptr [r14 + 4*rsi], xmm1
        movdqu  xmmword ptr [r14 + 4*rsi + 16], xmm2
        add     rsi, 8
        cmp     rdx, rsi
        jne     .LBB1_9
        cmp     rcx, rdx
        je      .LBB1_12
.LBB1_11:
        add     dword ptr [rax], 10
        add     rax, 4
        cmp     rax, rbx
        jne     .LBB1_11
.LBB1_12:
        mov     eax, dword ptr [r14]
        jmp     .LBB1_16
.LBB1_14:
        mov     qword ptr [rdi], 0
        mov     qword ptr [rdi + 8], 4
.LBB1_15:
        xor     eax, eax
.LBB1_16:
        pop     rbx
        pop     r12
        pop     r13
        pop     r14
        pop     r15
        ret
.LBB1_17:
        lea     rdi, [rip + .Lanon.e000f6f464dd9393db07351cb3eac3f5.0]
        lea     rdx, [rip + .Lanon.e000f6f464dd9393db07351cb3eac3f5.2]
        mov     esi, 40
        call    qword ptr [rip + core[8c5ac2f034688f9a]::panicking::panic@GOTPCREL]
.LBB1_18:
        mov     edi, 4
        mov     rsi, rbx
        call    qword ptr [rip + alloc[93e694a8365d93dd]::raw_vec::handle_error@GOTPCREL]
2 Likes