Skip to content

Instantly share code, notes, and snippets.

@ghiculescu
Created June 17, 2021 19:20
Show Gist options
  • Save ghiculescu/0eff2eb56254aa62bd10bf997cfc1d0f to your computer and use it in GitHub Desktop.
Save ghiculescu/0eff2eb56254aa62bd10bf997cfc1d0f to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails", branch: "main"
# gem "rails", github: "ghiculescu/rails", branch: "has-many-build-perf-regression"
gem "sqlite3"
gem "benchmark-ips"
end
require "active_record"
require "active_support"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :authors, force: true do |t|
end
create_table :posts, force: true do |t|
t.references :author
end
end
class Author < ActiveRecord::Base
has_many :posts
end
class Post < ActiveRecord::Base
belongs_to :author
end
author = Author.create!
posts = 100_000.times.map { |n| { author_id: author.id } }
Post.insert_all(posts)
author = Author.find(author.id)
author.posts.load_target
Benchmark.ips do |x|
x.report("main branch") { author.posts.build }
# x.report("my branch") { author.posts.build }
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment