Live data from Hacker News

Rust Sucks If I Fail to Write X

llogiq.github.io

11–20 of 89 posts

Re: Rust Sucks If I Fail to Write X

#12
post #3

Okay first of, before I read the rest of the article, I want to complain about the statement: > While you can hack together a “list” that will be backed by a Vec of nodes with indices to the next / previous item, this approach is quite wasteful – and gains little compared to using the Vec directly. How is this wasteful or hacky?? This is THE proper way of writing data structures that are not strict trees. Sure, this…

When most people ask for a linked list data structure, they probably don't mean "a vector of elements and a linked list of indexes into that vector" because it totally defeats the purpose of using a linked list in the first place.

Re: Rust Sucks If I Fail to Write X

#13
post #2

> Rustaceans usually opt for continuous data layout (known in C/C++ lingua as array-of-struct or struct-of-array depending on priorities), which is more cache-friendly than reference-heavy data structures anyway. Yes! This is a point that I try to hammer home that's missed by many people who write C/C++ on a daily basis. Everyone wants to use the fanciest data structures when most of the times arrays will be faster a…

SOA is the widely accepted approach to performance design. Not an argument vs C/C++, though.

Sure, but maybe 10% of C/C++ developers I've worked with over my career was actually aware of it :).

I wasn't putting it out there as a dig against C/C++ but more a general comment on how rarely it's understood/appreciated in native code.

Re: Rust Sucks If I Fail to Write X

#14
post #2

> Rustaceans usually opt for continuous data layout (known in C/C++ lingua as array-of-struct or struct-of-array depending on priorities), which is more cache-friendly than reference-heavy data structures anyway. Yes! This is a point that I try to hammer home that's missed by many people who write C/C++ on a daily basis. Everyone wants to use the fanciest data structures when most of the times arrays will be faster a…

Isn't this a bit of a weird formulation? What matters isn't actually layout... what matters is access patterns. If you lay out a 32G array and access it randomly you're not much better of that just doing loads of pointer chasing. (Alright, prefetching might help a little bit, but...)

Re: Rust Sucks If I Fail to Write X

#15

> As an aside, remember that the only difference to C/c++ is that if you write a “basic linked list” in them, all of your code will be unsafe. I stopped reading here.

What he's saying is correct: all C/C++ code operates in semantics equivalent to Rust's "unsafe" blocks.

Re: Rust Sucks If I Fail to Write X

#16

> As an aside, remember that the only difference to C/c++ is that if you write a “basic linked list” in them, all of your code will be unsafe. I stopped reading here.

In the rust world "unsafe" is synonymous with "isn't proven to be safe by the compiler". Under this definition, every C/C++ program that uses pointers is "unsafe" because the languages make no memory safety guarantees.

Re: Rust Sucks If I Fail to Write X

#17
post #2

> Rustaceans usually opt for continuous data layout (known in C/C++ lingua as array-of-struct or struct-of-array depending on priorities), which is more cache-friendly than reference-heavy data structures anyway. Yes! This is a point that I try to hammer home that's missed by many people who write C/C++ on a daily basis. Everyone wants to use the fanciest data structures when most of the times arrays will be faster a…

C++ programmers usually prefer contiguous data layout as well. I mean std::vector is just a contiguous array that dynamically reallocates and copies/move-constructs everything as needed.

But many interesting data structures are hard to write in a memory-efficient manner without resorting to non-contiguous nodes. Even a hashtable often will use linked lists within each bucket for collision resolution. You can argue that an open-addressing scheme is more cache-friendly, but it also has downsides, i.e. performance degrades faster as the load factor gets higher.

Many other interesting data structures, especially some lock-free structures, are simply impractical to implement as single contiguous arrays.

Of course, any node-based structure can make use of a memory pool that allocates blocks from one or more larger contiguous buffers, but there will still be pointers interleaved throughout the structure.

All in all, it remains true that Rust doesn't really provide any safety above C++ in regard to writing these kinds of node-based data structures, and saying "don't ever write node-based data structures" is just pointless. Yes, Rust has some downsides and tradeoffs. Is it so bad to just say that out loud?

Re: Rust Sucks If I Fail to Write X

#18
post #3

Okay first of, before I read the rest of the article, I want to complain about the statement: > While you can hack together a “list” that will be backed by a Vec of nodes with indices to the next / previous item, this approach is quite wasteful – and gains little compared to using the Vec directly. How is this wasteful or hacky?? This is THE proper way of writing data structures that are not strict trees. Sure, this…

When most people ask for a linked list data structure, they probably don't mean "a vector of elements and a linked list of indexes into that vector" because it totally defeats the purpose of using a linked list in the first place.

Well when most people ask for a linked list my first response would be "why?"

Re: Rust Sucks If I Fail to Write X

#19
post #7

In my programming experience, there are two kinds of code: 1. Code where my objects form a tree. Rust's ownership model is great for this. 95% of my code looks this way naturally, and maybe another 3% can be rewritten to look like this. 2. Code where my objects form a complex graph. At this point, I need to make a choice between manual pointer management (C++, unsafe Rust) and a garbage collector (lots of languages).…

(2) is why I'm so happy for Gc.

Also, if my graph is going to be short-lived, I've had success just allocing up an Arena, making as many cycles as I want, and then collecting the whole thing at once after my computation is done.

https://github.com/Manishearth/rust-gc

Re: Rust Sucks If I Fail to Write X

#20
post #18

Earlier quoted context omitted.

When most people ask for a linked list data structure, they probably don't mean "a vector of elements and a linked list of indexes into that vector" because it totally defeats the purpose of using a linked list in the first place.

Well when most people ask for a linked list my first response would be "why?"

an LRU cache?
Post reply on HN