4 ms·
I've been doing Rust for a while now, but this is the first time I've heard about the niche concept[1]. Sounds really useful. I know that other languages have
by weinzierl 6y ago
I've been doing Rust for a while now, but this is the first time I've heard about the niche concept[1]. Sounds really useful.
I know that other languages have subranges or refinement types but niches seem to solve a different problem - namely to use the "holes" in a data type for optimization. Does any other language have some comparable concept to niches in Rust?
- KirillPanov 6y agoIn Standard ML the bitwidth of "int" is machine-specific, like in C's "int" and Rust's "isize". Most implementations of Standard ML choose the bitwidth to be one bit less than the machine's natural word width, so that the GC mark bit can be stashed in there. For example, 63-bit ints on a 64-bit machine. http://www.itu.dk/~sestoft/mosmllib/Int.html#precision-val http://www.itu.dk/~sestoft/mosmllib/Int.html#precision-val I recall that MLton would stash data in the lower log_2(alignment) bits of pointers for types with alignment requirements (or architectures with memory load alignment requirements), but can't find a reference. There was a paper (ICFP, I think?) about using these bits for reference-counting GC with a fallback to mark+sweep when the count hit the maximum value. Don't think it ever got implemented in a production compiler. I'm pretty sure the "None:Option<T> as null pointer when T is represented as a pointer" optimization happens under the hood in a lot of Haskell+ML compilers. Rust is the first language I've come across that managed to include pretty much all the cool features ML had.
- FuckButtons 6y agoThis sounds like a really interesting idea that I hadn’t really encountered before. Do you have any suggestions for further reading?
- masklinn 6y agoThe second paragraph is pointer tagging, it’s used quite a bit, including in the upper bits for some (e.g. objc stashes the ref count in the class pointer). I think Ocaml also does something interesting with integers, IIRC they’re 1 bit less than the normal range and a shift stores a flag in the freed low bit: if the bit is set it’s a shifted integer, otherwise it’s an unshifted pointer to an integer.