News for hackers. Stuff that matters.
Re:Diggin’ and Fetchin’ with TruffleRuby by ljm on Wednesday August 25, 2021 @01:26PM (#28304285)
I imagine the premise of this post is basically to just kick off an exploration into TruffleRuby, which is cool! And an interesting read re: the benchmarking and performance of the implementation.

A shame that refinements don't really get much love, because the third option (in between monkey patching and extending the core library), is to use one of those.

    module DigWithFallback
      refine Hash do
        def dig_fetch(*keys)
          # ... impl
        end
      end
    end

    class SomeService
      using DigWithFallback

      def call(params)
        params.dig_fetch(:some, :key) { IdentityObject.new }
      end
    end
Re:Diggin’ and Fetchin’ with TruffleRuby by byroot on Wednesday August 25, 2021 @02:47PM (#28305367)
> A shame that refinements don't really get much love

Their performance is quite awful on MRI. Any method that was refined is tagged as such, which incur a 40% performance hit (for empty methods)[0]. That alone tend to disqualify them for many use cases.

Then, it might get fixed, but whenever you call `using` all the heap is scanned and all methods caches flushed [1], which is a massive perf hit if it happens often.

TruffleRuby however runs them fast, but since most code out there target MRI, they're not really a good idea except for fringe use cases.

[0] https://gist.github.com/casperisfine/1c46f05cccfa945cd156f44... [1] https://github.com/ruby/ruby/pull/4323

Re:Diggin’ and Fetchin’ with TruffleRuby by eregon on Wednesday August 25, 2021 @05:02PM (#28306962)
Interesting, I did not know that just calling the original method, even when the refinement method is not used occurs such a cost on CRuby. Seems worth reporting if not already done. No such thing on TruffleRuby though, the only peak performance cost would be for megamorphic calls (rare, even more so in combination with refinements).
Re:Diggin’ and Fetchin’ with TruffleRuby by byroot on Wednesday August 25, 2021 @05:41PM (#28307454)
> Seems worth reporting if not already done.

I assumed it is expected overhead.

Re:Diggin’ and Fetchin’ with TruffleRuby by aardvark179 on Wednesday August 25, 2021 @04:41PM (#28306676)
I think refinements are better than monkey patching, I mean they won’t cause unexpected behaviour elsewhere, but on the other hand they add a whole new layer of complexity to understanding a code base.