News for hackers. Stuff that matters.
Re:Moving from TypeScript to Rust / WebAssembly by drewm1980 on Thursday July 09, 2020 @01:47AM (#23777284)
Rust never has to run a garbage collector, because it already figured out the lifetimes of all the values in your program statically, with relatively minor help from the programmer. This may make your code faster or more likely to be correct than code written in some of those languages.
Re:Moving from TypeScript to Rust / WebAssembly by blub on Thursday July 09, 2020 @02:10AM (#23777411)
It didn't figure them out at all, the programmer had to figure them out the hard way and then code them in so that the compiler understands them.

GC is figuring out things mostly correctly and the programmer can focus on other things, at least until they hit a performance problem :)

Re:Moving from TypeScript to Rust / WebAssembly by Lorkki on Thursday July 09, 2020 @04:54AM (#23778246)
For the most part, lifetime is inferred from scope, so there isn't anything special to figure out; objects live exactly for as long as you can see and use them. This also includes such things as lock handles, or references to refcounted objects.
Re:Moving from TypeScript to Rust / WebAssembly by jashmatthews on Thursday July 09, 2020 @06:17AM (#23778628)
Only in the most trivial cases. Tons of real Rust uses copying or RC/ARC or even unsafe to avoid dealing with complex lifetimes because it's... complex.
Re:Moving from TypeScript to Rust / WebAssembly by Lorkki on Thursday July 09, 2020 @07:31AM (#23779021)
Copying, refcounting and locking work within the exact same semantics. They're among the most typical tools that are used to transmit or share data, so I'm not sure why it should be strange to see them used in most real programs?
Re:Moving from TypeScript to Rust / WebAssembly by Karrot_Kream on Thursday July 09, 2020 @03:15AM (#23777746)
That doesn't change the reality of allocation though. Programmers in languages like C++ and Rust choose when and where to allocate memory, and if there's one thing we know about performant code, it's that we shouldn't be allocating and deallocating repeatedly. Nothing about lifetimes helps or hurts about this programming model. Lifetimes (Rust) and RAII (C++) are just semantic models to help reign in the cognitive load of memory allocation.
Re:Moving from TypeScript to Rust / WebAssembly by rcxdude on Thursday July 09, 2020 @10:22AM (#23780544)
I don't think you've understood the nature of the borrow checker in Rust. It doesn't add any extra runtime behaviour, in fact you could compile Rust code without running these checks (though I don't think rustc provides you with a way to do so). Rust's memory management tools are no different from C++: you have stack allocated structures with destructors, which the standard library uses to implement heap-allocated smart pointers, shared pointers, mutex guards, etc, and you can hand out pointers to any of these. The only difference in rust is it checks how these pointers get handed out so you don't get use-after-free, data races, etc.
Re:Moving from TypeScript to Rust / WebAssembly by adrianN on Thursday July 09, 2020 @04:15AM (#23778042)
I doubt that you'll write a large Rust program without relying on refcounting at least for some of your objects. Refcounting is not fundamentally different than a GC.
Re:Moving from TypeScript to Rust / WebAssembly by zozbot234 on Thursday July 09, 2020 @05:32AM (#23778437)
Reference counting is a kind of garbage collection (taken broadly), but it's quite different from tracing GC.
Re:Moving from TypeScript to Rust / WebAssembly by jashmatthews on Thursday July 09, 2020 @06:24AM (#23778664)
Naive use of RC doesn't look like tracing much but as soon as you need cycle-aware RC like the CactusRef the algo looks a lot like tracing GC https://github.com/artichoke/cactusref#cycle-detection
Re:Moving from TypeScript to Rust / WebAssembly by laurencerowe on Thursday July 09, 2020 @02:09AM (#23777408)
That's a fair point, I should distinguish heap allocation from reference counting. It still seems somewhat tricky to replicate the well proven arena allocation approach of Apache and Nginx which avoids memory fragmentation in current stable Rust though.
Re:Moving from TypeScript to Rust / WebAssembly by kd5bjo on Thursday July 09, 2020 @03:44AM (#23777887)
For general areana allocation, you can use something like the Bumpalo crate: https://docs.rs/bumpalo/3.4.0/bumpalo/