John Lam 1997 — 2026

The Joy of Deleting Code

You know how it is. At first you have to rip things apart before you can begin building things back up again. There’s been lots of ripping apart happening in RubyCLR over the past few weeks. A brand-new and very high performance reflection layer now lies underneath everything. And tonight I got the first vertical slice to work.

Here’s the unit test that now passes:

def test_default_ctor
  a = ArrayList.new
  assert_equal 'System.Collections.ArrayList', a.clr_type.full_name
end

Here’s what the code used to look like for defining a method shim:

def self.method_shim(klass, method_info)
  method_labels = (1..method_info.signatures.length).collect { |i| ("l" + i.to_s).to_sym }

  create_safe_ruby_instance_method(klass, method_info.ruby_member_name) do
    match_sig   method_info.member_id
    switch      method_labels
    throw_clr   'Cannot find method that matches Ruby parameters'

    method_info.signatures.each_with_index do |sig, i|
      label      method_labels[i]

      ld_this    klass
      ld_params  sig
      inst_call  method_info, i
      ret_2rb    method_info, i
    end
  end
end

Here’s what it looks like now:

def self.method_shim(klass, method_name, members)
  method_labels = (1..members.length).collect { |i| ("l" + i.to_s).to_sym }

  create_safe_ruby_instance_method(klass, method_name) do
    match_sig   add_members_to_signature_cache(members)
    switch      method_labels
    throw_clr   'Cannot find method that matches Ruby parameters'

    members.each_with_index do |member, i|
      label      method_labels[i]

      ld_this    klass
      ld_params  member.get_signature
      inst_call  member
      ret_2rb    member, i
    end
  end
end

The new code is much better than the old code. In the old code, all type information was expressed using strings. This made things quite flexible, but at a severe performance penalty. There were many type lookup operations (to be fair, some of which were cached), but these are now eliminated in the new code.

I was also rather ashamed of the method_info data structure. It’s a rather central data structure in RubyCLR and it became the kitchen sink of metadata. It evolved over time and was never redesigned. In the new code it doesn’t exist anymore.

Tomorrow should be a fun day – I get to delete a ton of code that isn’t used anymore. I have far more fun deleting code than writing it.