Skip to content

Commit

Permalink
Merge 0e10e79 into 343f6f7
Browse files Browse the repository at this point in the history
  • Loading branch information
jturkel committed Dec 10, 2018
2 parents 343f6f7 + 0e10e79 commit c05268e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 3.0.3
- Optimize association eager loadable checks by caching information on the association's reflection.
- Optimize association eager loading if we're only eager loading associations for a single model.

### 3.0.2
- Fix destroyed model eager loading which accidentally broke in [#74](https://github.com/salsify/goldiloader/pull/74).
Expand Down
7 changes: 7 additions & 0 deletions benchmark.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

cd "$( dirname "${BASH_SOURCE[0]}" )"

echo `pwd`

find benchmark/ -maxdepth 1 -type f -name "*_benchmark.rb" -exec bundle exec ruby {} \;
17 changes: 2 additions & 15 deletions benchmark/main.rb → benchmark/main_benchmark.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
# frozen_string_literal: true

$LOAD_PATH.push(File.expand_path('../lib', __dir__))

require_relative 'lib/forking_benchmark'
require 'active_record'

ActiveRecord::Migration.verbose = false
require_relative 'performance_helper'

ForkingBenchmark.ips do |x|
x.time = 5
x.warmup = 2

x.setup do
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)

require_relative './db/schema.rb'
setup_database

# Setup data
blog_1 = Blog.create!
Expand Down
17 changes: 17 additions & 0 deletions benchmark/performance_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

$LOAD_PATH.push(File.expand_path('../lib', __dir__))

require_relative 'lib/forking_benchmark'
require 'active_record'

ActiveRecord::Migration.verbose = false

def setup_database
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)

require_relative './db/schema.rb'
end
26 changes: 26 additions & 0 deletions benchmark/single_model_benchmark.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require_relative 'performance_helper'

ForkingBenchmark.ips do |x|
x.setup do
setup_database

# Setup data
blog = Blog.create!
user = User.create!
Post.create!(author: user, blog: blog)
end

x.report("ActiveRecord: Single model's association") do
Post.first!.author
end

x.report("Goldiloader: Single model's association", setup: -> { require('goldiloader') }) do |iterations|
iterations.times do
Post.first!.author
end
end

x.compare!
end
14 changes: 11 additions & 3 deletions lib/goldiloader/active_record_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,19 @@ def eager_loadable?
def load_with_auto_include
if loaded? && !stale_target?
target
elsif auto_include?
elsif !auto_include?
yield
elsif owner.auto_include_context.size == 1
# Bypassing the preloader for a single model reduces object allocations by ~5% in benchmarks
result = yield
# As of https://github.com/rails/rails/commit/bd3b28f7f181dce53e872daa23dda101498b8fb4
# ActiveRecord does not use ActiveRecord::Relation#exec_queries to resolve association
# queries
Goldiloader::AutoIncludeContext.register_models(result)
result
else
Goldiloader::AssociationLoader.load(owner, reflection.name)
target
else
yield
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/goldiloader/auto_include_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module Goldiloader
class AutoIncludeContext
attr_reader :models

delegate :size, to: :models

def initialize
@models = []
end
Expand Down

0 comments on commit c05268e

Please sign in to comment.