News for hackers. Stuff that matters.
Re:Shopify invests in research for Ruby at scale by bin_bash on Monday May 16, 2022 @04:21PM (#31402714)
We gonna double-blind developers or something? I know Go and Ruby very well. I know what it takes to write in one vs the other.

Ruby is faster to write and more concise by a long shot—certainly if we're talking back-end web services. I prefer Go to Ruby generally but it's weird that Go developers tend not to even see how much yak shaving is involved with it. Or really any downside with it.

Try writing this in Go:

    records = User.limit(10)
    records.select { |user| user.is_admin? }.map{|user| "#{user.id} : #{user.name}"}
Re:Shopify invests in research for Ruby at scale by pjmlp on Tuesday May 17, 2022 @03:00AM (#31406921)
Thanks to generics, assuming there is some stream/sequence like type

    records := Stream.Limit(User, 10)
    records.Select(func(user User) bool {return user.IsAdmin()}).Map{func(user User) string { return fmt.Sprintf("%d %s", user.id, user.name) }
Yes a bit more verbose, that is the price to pay for static typing without full type inference as in the ML language family.
Re:Shopify invests in research for Ruby at scale by bin_bash on Tuesday May 17, 2022 @04:24AM (#31407476)
It blows my mind why Go didn’t have generics at launch. I suppose they just didn’t want people to be able to have abstractions like this at all?
Re:Shopify invests in research for Ruby at scale by pjmlp on Tuesday May 17, 2022 @04:33AM (#31407537)
> In retrospect, we were biased too much by experience with C++ without concepts and Java generics. We would have been well-served to spend more time with CLU and C++ concepts earlier.

https://go.googlesource.com/proposal/+/master/design/go2draf...

Re:Shopify invests in research for Ruby at scale by bin_bash on Tuesday May 17, 2022 @05:03AM (#31407728)
oh wow thanks for this. I haven't been actively using Go since around the time this was written so I've missed it
Re:Shopify invests in research for Ruby at scale by AzzieElbab on Tuesday May 17, 2022 @08:18AM (#31409394)
Stream.limit and Stream.select(…).limit are not the same thing :)

records.collect({case u if u.isAdmin => s”${u.id} ${u.name}”}).take(10) guess the language

Re:Shopify invests in research for Ruby at scale by pjmlp on Tuesday May 17, 2022 @09:23AM (#31410177)
Well you can argue semantics, it doesn't change the fact that it is possible to implement the same concepts in Go with generics.

That line of code could be Scala, or something else.

In any case, the point is that there are compiled languages that offer similar productivity like Ruby, with AOT/JIT compilers out of the box, and great IDE experiences.

Re:Shopify invests in research for Ruby at scale by AzzieElbab on Tuesday May 17, 2022 @11:49AM (#31412450)
It is Scala. I meant to demo type inference and pattern matching. I know pattern matching(something I cant live without) does not exist in Go and Ts, not sure how well type inference works in Go
Re:Shopify invests in research for Ruby at scale by howeyc on Monday May 16, 2022 @04:43PM (#31402921)
I may be wrong, but aren't you doing two loops by doing it that way? One for the select, and another for the map? Or does ruby compile out the select loop with smarts.

In Go, the intuitive way would be one loop with an if inside it. So it would be easy to spot this performance issue if you did it the ruby way with two loops.

https://go.dev/play/p/ixDKuosxPNp

I bet I wrote my for loop as quickly as you wrote your map. The character count isn't that much higher.

Re:Shopify invests in research for Ruby at scale by bin_bash on Monday May 16, 2022 @05:20PM (#31403257)
The amount of times in my career where the performance would be even measurable between these 2 styles is vanishingly few. If I cared I could just use #filter_map or a classic for-each loop. If we're assuming the project is written in Ruby in the first place, clearly we're not valuing performance that highly.

The more important thing is the ruby version is chainable and more readable—and that matters far more often.

Re:Shopify invests in research for Ruby at scale by gkop on Tuesday May 17, 2022 @12:19AM (#31405989)
You had me until

> more readable

Without having more context, User in your code is possibly an ActiveRecord class, so to read the #select, you have to consider that it could be radically different and heavier than Enumerable#select (eg. it may execute a DB query as a side effect). So I like your example still, because it’s representative, and certainly expressive, but not readable per se. IMO this sin here is simply that ActiveRecord shouldn’t have overloaded select.

Re:Shopify invests in research for Ruby at scale by bin_bash on Tuesday May 17, 2022 @04:23AM (#31407462)
That’s more of an argument against ORMs which I realize my example used. I was mostly trying to convey using map/filter against POROs though.

Using an ORM was a poor choice on my part. I agree the magic there does harm readability since it makes it a lot harder to understand what it really is doing.

Re:Shopify invests in research for Ruby at scale by Lio on Wednesday May 18, 2022 @10:13AM (#31423040)
Ruby has had lazy iteration[1] for quite a while but as n here is only 10 there's really not much to loose in not using it.

In fact given that only a subset of users will be admin users for the second loop n is going to < 10 anyway.

I believe it was Rob Pike that actually said something along the lines of not bothering to optimise until you know that n is large[2].

1. https://ruby-doc.org/core-2.5.0/Enumerator/Lazy.html

2. https://users.ece.utexas.edu/~adnan/pike.html

Re:Shopify invests in research for Ruby at scale by kaba0 on Tuesday May 17, 2022 @05:14AM (#31407830)
Not sure about Ruby, but Java’s streams would do one loop only here, as they are lazy. Also, both solutions are O(n), so unless this is the hot loop of your program, or it iterates over some insane amount of entities, it is a completely meaningless microoptimization.