[#104481] [Ruby master Feature#18020] Introduce `IO::Buffer` for fiber scheduler. — samuel@...

Issue #18020 has been reported by ioquatix (Samuel Williams).

31 messages 2021/07/03

[#104492] [Ruby master Bug#18022] Spec errors for rbconfig/unicode_[emoji_]version_spec: Using Ruby 2.7 even when on Ruby 3.1 — duerst@...

Issue #18022 has been reported by duerst (Martin Dst).

8 messages 2021/07/04

[#104552] [Ruby master Feature#18033] Time.new to parse a string — nobu@...

Issue #18033 has been reported by nobu (Nobuyoshi Nakada).

26 messages 2021/07/09

[#104560] [Ruby master Bug#18035] Introduce general module for immutable by default. — samuel@...

Issue #18035 has been reported by ioquatix (Samuel Williams).

41 messages 2021/07/09

[#104629] [Ruby master Misc#18039] DevelopersMeeting20210819Japan — mame@...

Issue #18039 has been reported by mame (Yusuke Endoh).

11 messages 2021/07/16

[#104643] [Ruby master Bug#18040] Why should `foo(1 if true)` be an error? — bughit.github@...

Issue #18040 has been reported by bughit (bug hit).

10 messages 2021/07/19

[#104665] [Ruby master Feature#18042] YARV code optimization — motoroller95@...

Issue #18042 has been reported by motoroller (Iskandar Gohar).

11 messages 2021/07/23

[#104692] [Ruby master Bug#18048] Thread#join can break with fiber scheduler unblock fails or blocks. — samuel@...

Issue #18048 has been reported by ioquatix (Samuel Williams).

10 messages 2021/07/27

[#104723] [Ruby master Bug#18054] No rule to make target 'thread_fd_close.c', needed by 'thread_fd_close.o' — duerst@...

Issue #18054 has been reported by duerst (Martin Dst).

8 messages 2021/07/29

[ruby-core:104468] [Ruby master Bug#16243] case/when is slower than if on MRI

From: merch-redmine@...
Date: 2021-07-02 00:09:26 UTC
List: ruby-core #104468
Issue #16243 has been updated by jeremyevans0 (Jeremy Evans).

Status changed from Open to Closed

commit:5ada23ac1265a1da5d7ef82e1c71f14c40dddc26 changed case statements to use a `===` with a call cache, instead of using the `checkmatch` VM instruction.  In my benchmarking on master, I'm only seeing about 1-3% difference with the revised benchmark, as opposed to the much larger difference on ruby 3.0.  So I think this performance difference has been addressed. 

----------------------------------------
Bug #16243: case/when is slower than if on MRI
https://bugs.ruby-lang.org/issues/16243#change-92725

* Author: Eregon (Benoit Daloze)
* Status: Closed
* Priority: Normal
* ruby -v: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
This comes from a discussion on a PR on Sidekiq: https://github.com/mperham/sidekiq/pull/4303

This benchmark:
```ruby
# frozen_string_literal: true
require "benchmark/ips"

def deep_dup_case(obj)
  case obj
  when Integer, Float, TrueClass, FalseClass, NilClass
    obj
  when String
    obj.dup
  when Array
    obj.map { |e| deep_dup_case(e) }
  when Hash
    duped = obj.dup
    duped.each_pair do |key, value|
      duped[key] = deep_dup_case(value)
    end
  else
    obj.dup
  end
end

def deep_dup_if(obj)
  if Integer === obj || Float === obj || TrueClass === obj || FalseClass === obj || NilClass === obj
    obj
  elsif String === obj
    obj.dup
  elsif Array === obj
    obj.map { |e| deep_dup_if(e) }
  elsif Hash === obj
    duped = obj.dup
    duped.each_pair do |key, value|
      duped[key] = deep_dup_if(value)
    end
    duped
  else
    obj.dup
  end
end


obj = { "class" => "FooWorker", "args" => [1, 2, 3, "foobar"], "jid" => "123987123" }

Benchmark.ips do |x|
  x.report("deep_dup_case") do
    deep_dup_case(obj)
  end

  x.report("deep_dup_if") do
    deep_dup_if(obj)
  end

  x.compare!
end
```

gives in MRI 2.6.5:
```
Warming up --------------------------------------
       deep_dup_case    37.767k i/100ms
         deep_dup_if    41.802k i/100ms
Calculating -------------------------------------
       deep_dup_case    408.046k (0.9%) i/s -      2.077M in   5.090997s
         deep_dup_if    456.657k (ア 0.9%) i/s -      2.299M in   5.035040s

Comparison:
         deep_dup_if:   456657.4 i/s
       deep_dup_case:   408046.1 i/s - 1.12x  slower
```

It seems @tenderlovemaking already noticed this a while ago due to missing inline caches for `case`, but the performance bug seems to still remain:
https://youtu.be/b77V0rkr5rk?t=1442

Do you think this could be fixed in MRI?
AFAIK both JRuby and TruffleRuby support inline cached `===` calls in `case/when`.

I think the code with `case` is more idiomatic and readable and should be preferred to `if`, and this performance issue makes that less clear.

---Files--------------------------------
case_vs_if.rb (1003 Bytes)
case_vs_if_sum.rb (1.02 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next