News for hackers. Stuff that matters.
Re:Ruby 3.3's YJIT: Faster While Using Less Memory by ksec on Monday December 18, 2023 @03:38PM (#38687862)
My question is what's next? Without sacrificing (much) memory. We know from TruffleRuby that given enough warmups and lots of memory it could be multiple times faster than CRuby. And compared to Ruby 3.3 YJIT may be 2-3x faster. But it seems the communities as a whole cares a lot about memory usage.

We know in order to make JIT more useful we need to move some of the Gems from C to Ruby. But other than that do we have anything on the table that will significantly speed up Ruby Rails?

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by byroot on Monday December 18, 2023 @04:39PM (#38688701)
There is still lots of optimizations to make. On the JIT side, there is still no inlining, which opens the door to much more aggressive optimizations.

But also on memory as well. There was a lot of improvements done on the Ruby garbage collector for 3.3, but there's still a lot of room for improvement, and a few fine folks at Shopify are looking into making the GC swappable so we'd be able to use mttk GCs with MRI.

As for the community caring about memory usage, I wonder how much of that has to do with the default Heroku dynos. Because it's an issue for small scale deployment. Once you scale more, you recoup a lot with CoW etc, so it's much less of a worry.

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by claudiug on Monday December 18, 2023 @05:15PM (#38689150)
can you share on this? :)
Re:Ruby 3.3's YJIT: Faster While Using Less Memory by byroot on Monday December 18, 2023 @05:39PM (#38689416)
On what, mmTk?

Matt's talk at latest Ruby Kaigi is a good intro: https://www.youtube.com/watch?v=chhNDhyPbyc

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by claudiug on Monday December 18, 2023 @06:12PM (#38689715)
thx u!
Re:Ruby 3.3's YJIT: Faster While Using Less Memory by ksec on Tuesday December 19, 2023 @02:01AM (#38692711)
>On the JIT side, there is still no inlining,

Oh wow. Had no idea there was no inlining. That is potentially another 2-3x performance.

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by byroot on Tuesday December 19, 2023 @02:04AM (#38692728)
To be slightly more correct, there is inlining but only of trivial methods that simply return an immediate value or a constant, e.g.

    class NilClass
      def blank?
        true
      end

      def present?
        false
      end
    end
Re:Ruby 3.3's YJIT: Faster While Using Less Memory by schneems on Monday December 18, 2023 @04:15PM (#38688411)
> But it seems the communities as a whole cares a lot about memory usage.

Matz very much sees Ruby as still a general purpose language that still supports scripting as a first tier concern. From that vantage bootup and memory are hugely important. Also providers like Heroku (I work there) charge for memory and the primary way to get parallelism with the GIL/GVL is using multiple processes where memory use can multiply rapidly (literally).

> speed up Ruby on Rails?

You can look at other communities to get a preview of what might come. Ruby now has “object shapes” like node.js which will benefit the community more once people start using it, enabling warnings and fixing hotspots where instance variables are not declared optimally.

Personally, I would like to see rails embrace the power of database backed constraints and fallback to using validation queries only when the database throws an error on commit. You can already saturate a CPU with processes and threads, the biggest bottleneck (IMHO) for most rails apps is still the database. Counterintuitively, pushing those constraints to the database can mean a net decrease in DB computing needed and more overall throughput.

Aside from that I think JIT will help do more with fewer resources. Which makes sense when the main YJIT sponsor has a huge server bill and being able to slash it in half more than justifies such long term gambles.

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by dalyons on Monday December 18, 2023 @04:43PM (#38688766)
I’m curious why you think constraints are such a problem? I’ve worked on quite a few large rails apps, and whilst you’re not wrong, the extra traffic from checking constraints has always been a tiny negligible part of overall db traffic.
Re:Ruby 3.3's YJIT: Faster While Using Less Memory by schneems on Monday December 18, 2023 @05:21PM (#38689220)
It takes two seconds to add a “validated uniqueness” and now for every save or update instead of one SQL command to commit the data you’ve got 2 (multiplied by the number of validations). Plus this validation is weak in that it cannot protect from actual duplicate data.

These check queries are individually fast but put a tremendous load on a running app’s database. If you halve the number of queries the database has to process you’ll see more resources to process the slow queries in addition to each commit being faster.

It ultimately depends on the read/write patterns of the app, but most non-trivial rails apps do a bunch of writing AND reading.

My suggestion is less a “this will change the world forever” and more a “fairly clear win, we just have to do it”.

In a prior life I talked Sgrif into implementing it before David caused them to drop Active Record maintenance work.

I know the broad strokes but don’t have the need or mandate to implement it, plus the active record internals are pretty gnarly. Tons of modules, very little encapsulation. Very hard to know the correct place to put stuff, so stuff goes wherever. It’s gotten better from some of the work of Yehudah, Aaron, and Sage but AFAIK there’s no one on core who wants to own it right now. Eileen has done a lot of work recently, but I don’t think it’s her primary focus.

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by byroot on Monday December 18, 2023 @05:37PM (#38689400)
> but AFAIK there’s no one on core who wants to own it right now.

Active Record is probably the "most owned" part of Rails, that's the one with the most core members and committers with deep knowledge of it.

As for your feature request, there's https://github.com/Shopify/activerecord-rescue_from_duplicat..., and we'd like to attempt to upstream that capability in Rails next year (but as everything it may or may not happen).

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by dalyons on Monday December 18, 2023 @05:40PM (#38689426)
I guess I’ve just never seen the combination of lots of writes AND lots of validates_uniqueness(or whatever db validation) AND the writes are updating the unique fields (due to dirty attributes / partial updates). So it’s never really been an issue.

If that’s been a problem for you I totally believe you, and everything you say about a fix all makes sense.

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by schneems on Monday December 18, 2023 @06:42PM (#38689935)
I work at Heroku and see a lot of different apps.
Re:Ruby 3.3's YJIT: Faster While Using Less Memory by dalyons on Monday December 18, 2023 @08:07PM (#38690548)
That makes sense! Sorry, you said that but I didnt connect the dots
Re:Ruby 3.3's YJIT: Faster While Using Less Memory by parentheses on Monday December 18, 2023 @04:02PM (#38688222)
It does make sense in the web server context to optimize for memory first over compute. This could explain the dynamic you called out. Furthermore, the propensity for many popular gems to have native portions makes the trade off even more useful.

Regarding JIT being faster, types are the next effective tool IMO.

The two drivers I see are:

- JIT has more stack frames to optimize away, allowing more optimization - you addressed this one

- JIT has more information to limit possible outcomes, therefore finding more opportunities

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by alberth on Monday December 18, 2023 @03:57PM (#38688153)
Truffle Ruby is 50-100% faster in most tests.

But uses 5-10x more memory.

Still has way more upside left to gain.

https://railsatscale.com/2023-11-07-yjit-is-the-most-memory-...

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by uticus on Monday December 18, 2023 @04:55PM (#38688912)
Not to mention the warm-up time - the graph in the referenced article is measured in seconds, with max > 20!

Can attest, just pulled TruffleRuby (both native and JVM), it is over ten seconds to start a hello world script cold. As in, script being run from command line as needed.

These are gains that are only going to make sense for the long-running process crowd.

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by uticus on Monday December 18, 2023 @05:06PM (#38689017)
Re:Ruby 3.3's YJIT: Faster While Using Less Memory by mlinksva on Monday December 18, 2023 @06:35PM (#38689882)
> We know in order to make JIT more useful we need to move some of the Gems from C to Ruby.

Just curious, how well do we know this, e.g., theory, experiments, it's being done, or ?

It'd be fun if RiiR[uby] were a small part of the overall memory saftey glacier (e.g., https://www.cisa.gov/case-memory-safe-roadmaps)

Re:Ruby 3.3's YJIT: Faster While Using Less Memory by TobiasBales on Tuesday December 19, 2023 @03:15AM (#38693121)
Re:Ruby 3.3's YJIT: Faster While Using Less Memory by gvkhna on Monday December 18, 2023 @07:23PM (#38690246)
I added jemalloc to my rails server and it took avg process mem usage of 1-1.5gb down to 400mb.