#sort_by and #sort_obj

Eric Hodel | Sun, 07 Oct 2007 08:42:32 GMT

Posted in ,

I was speeding up RubyGems and found a bunch of places that still used #sort instead of #sort_by. #sort_by is faster than #sort because it performs fewer comparisons resulting in fewer method calls.

All these places were external to the thing I was sorting, so they’d need to know how to perform the sorting, which is just plain wrong. I did a bit of thinking, and created a #sort_obj method to return an object that can be used for sorting instead.

So Gem::Specification#<=> went from:

def <=>(other)
  platform_num = platform == Gem::Platform::RUBY ? -1 : 1
  other_platform_num = other.platform == Gem::Platform::RUBY ? -1 : 1

  [@name, @version, platform_num] <=>
    [other.name, other.version, other_platform_num]
end

To:

def sort_obj
  [@name, @version.to_ints, @platform == Gem::Platform::RUBY ? -1 : 1]
end

def <=>(other)
  sort_obj <=> other.sort_obj
end

So now instead of:

specs.sort_by do |spec|
  [@name, @version.to_ints, @platform == Gem::Platform::RUBY ? -1 : 1]
end

I can write:

specs.sort_by { |spec| spec.sort_obj }
no comments

Comments RSS FEED

Comments are disabled