Created
August 19, 2017 08:38
-
-
Save kamipo/684d03817a8115848cec8e8b079560b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
begin | |
require "bundler/inline" | |
rescue LoadError => e | |
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler" | |
raise e | |
end | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "rails", github: "rails/rails" | |
gem "arel", github: "rails/arel" | |
gem "sqlite3" | |
gem "benchmark-ips" | |
end | |
require "active_record" | |
require "benchmark/ips" | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
ActiveRecord::Schema.define do | |
create_table :foos do |t| | |
t.string :type | |
end | |
end | |
class Foo < ActiveRecord::Base | |
def self.fast_relation | |
relation = ActiveRecord::Relation.create(self, arel_table, predicate_builder) | |
if finder_needs_type_condition? && !ignore_default_scope? | |
relation.where!(type_condition) | |
relation.create_with!(inheritance_column.to_s => sti_name) | |
else | |
relation | |
end | |
end | |
end | |
class FastFoo < Foo | |
class << self | |
alias :relation :fast_relation | |
end | |
end | |
class SlowFoo < Foo | |
end | |
GC.start | |
GC.disable | |
Benchmark.ips do |x| | |
x.report("fast relation") { FastFoo.all } | |
x.report("slow relation") { SlowFoo.all } | |
x.compare! | |
end | |
GC.enable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment