News for hackers. Stuff that matters.
Re:Zero Cost Abstractions by choeger on Thursday May 16, 2019 @03:26PM (#19932178)
Good luck with the trait object. I doubt you're going to significantly improve on what the Haskell community has done in 20 years. If you do manage it though, kudos in advance.
Re:Zero Cost Abstractions by moomin on Thursday May 16, 2019 @03:31PM (#19932212)
If Rust does manage it, I confidently predict someone will be submitting a GHC proposal within the week.
Re:Zero Cost Abstractions by jadbox on Thursday May 16, 2019 @03:54PM (#19932443)
Noob here: why is the trait object hard to optimize in principle?
Re:Zero Cost Abstractions by uryga on Thursday May 16, 2019 @04:56PM (#19933116)
(disclosure: i know Haskell but not Rust, and haven't actually implemented anything like this in a compiler. please let me know if i'm wrong!)

best guesses:

- dynamic dispatch usually involves carrying around¹ some function pointers (to your type's implementations of the trait methods). this means that when calling `x.foo()` on a trait object `x` of some trait `Foo`, the actual function/method being called isn't known until runtime, so the compiler can't optimize the call through things like inlining (which require knowing what's being called). also AFAIK jumping to some unknown function pointer is relatively slow on the CPU level

- since the compiler doesn't know the actual type (and hence layout) of your object, it has to be passed via reference, so there's some indirection overhead and it's probably impossible to e.g. optimize away intermediate values or be smart about the way they're kept in registers.

the problem is that the nature of trait objects (i.e. "i know nothing about this value except that it implements the `Foo` trait) kind of seems to require these things :(

¹ via a vtable or "dictionary passing"

Re:Zero Cost Abstractions by tathougies on Thursday May 16, 2019 @04:30PM (#19932856)
I don't think rust has polymorphic recursion. GHC is already pretty fast on non-polymorphic recursion.
Re:Zero Cost Abstractions by withoutboats on Thursday May 16, 2019 @06:09PM (#19933932)
This is the sort of comment on hacker news I absolutely love: I know one of us is sitting in a place of ignorant hubris, and I doubt its me.

The problems I refer to are, as I stated in the post, UX problems, and they arise ultimately from Rust's guarantees generics will be monomorphized, that allocations will be explicit, that arguments are passed by value, and so on. None of this applies to Haskell. We don't need to "improve on" Haskell (though I think our coherence and instance resolution rules are superior), we have different design constrants resulting in different problems.