Skip to content

Instantly share code, notes, and snippets.

View yuroyoro's full-sized avatar
🍣
🍣

しいたけ yuroyoro

🍣
🍣
View GitHub Profile
@yuroyoro
yuroyoro / bechmark.rb
Last active August 29, 2015 14:15
Performance regression of ActiveRecord preloading :has_many through associations | Major performance regression when preloading has_many_through association · Issue #12537 · rails/rails https://github.com/rails/rails/issues/12537#issuecomment-55462750
begin
class CreateModels < ActiveRecord::Migration
def change
create_table :items do |t|
end
create_table :events do |t|
t.belongs_to :item
end
@yuroyoro
yuroyoro / benchmark.rb
Created February 27, 2015 04:59
Performance regression in ActiveRecord model accessing generated property method
require 'benchmark/ips'
begin
class CreateModels < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :attribute1
t.string :attribute2
t.string :attribute3
t.string :attribute4
@yuroyoro
yuroyoro / dangerous_fasten_ar_attribute_method.patch
Created March 2, 2015 05:38
Rails4.2でのActiveRecordのcomplied attribute_methodを速くするpatch(だけどとても危険)
diff --git activerecord/lib/active_record/attribute_methods/read.rb activerecord/lib/active_record/attribute_methods/read.rb
index 20f0936..77bd11d 100644
--- activerecord/lib/active_record/attribute_methods/read.rb
+++ activerecord/lib/active_record/attribute_methods/read.rb
@@ -33,6 +33,17 @@ module ActiveRecord
end
}.new
+ TextReaderMethodCache = Class.new(AttributeMethodCache) {
+ def method_body(method_name, const_name)
@yuroyoro
yuroyoro / ar_collection_proxy_delegates_to_association.patch
Created March 4, 2015 03:26
ActiveRecord::Associations::CollectionProxy#new の性能劣化を回復させる危険な闇patch
diff --git activerecord/lib/active_record/associations/collection_proxy.rb activerecord/lib/active_record/associations/collection_proxy.rb
index a034753..c5b84e5 100644
--- activerecord/lib/active_record/associations/collection_proxy.rb
+++ activerecord/lib/active_record/associations/collection_proxy.rb
@@ -28,13 +28,23 @@ module ActiveRecord
# is computed directly through SQL and does not trigger by itself the
# instantiation of the actual post records.
class CollectionProxy < Relation
+
delegate(*(ActiveRecord::Calculations.public_instance_methods - [:count]), to: :scope)
block in ActiveRecord::Associations::Preloader::ThroughAssociation#associated_records_by_owner (/home/ozaki/dev/rails/activerecord/lib/active_record/associations/preloader/through_association.rb:45)
samples: 8 self (0.8%) / 332 total (33.3%)
callers:
332 ( 100.0%) ActiveRecord::Associations::Preloader::ThroughAssociation#associated_records_by_owner
callees (324 total):
307 ( 94.8%) block (2 levels) in ActiveRecord::Associations::Preloader::ThroughAssociation#associated_records_by_owner
9 ( 2.8%) ActiveRecord::Core#hash
8 ( 2.5%) block (2 levels) in ActiveRecord::Associations::Preloader::ThroughAssociation#associated_records_by_owner
code:
| 45 | through_records.each_with_object({}) { |(lhs,center),records_by_owner|
require 'benchmark/ips'
begin
class CreateModels < ActiveRecord::Migration
def change
create_table :items do |t|
end
create_table :events do |t|
t.belongs_to :item
================================================================================
3.2.21 - sqlite3
================================================================================
Calculating -------------------------------------
limit 100 has_many 13.000 i/100ms
limit 100 has_many_through
5.000 i/100ms
limit 100 double has_many_through
3.000 i/100ms
@yuroyoro
yuroyoro / hash_deep_slice.rb
Created March 26, 2015 04:14
Hash#deep_slice
# Hash#deep_slice
#
# h = {
# :a => 1,
# :b => 2,
# :c => {
# :ca => 31,
# :cb => 32,
# :cc => 33
# },
@yuroyoro
yuroyoro / update_all_jobs_slack_notification_setting.groovy
Last active August 29, 2015 14:18
Jenkinsのスクリプトコンソールで、ジョブに一括してSlack通知を設定するスクリプト
jobs = jenkins.model.Jenkins.instance.items
jobs.each {job ->
job.removeProperty(jenkins.plugins.slack.SlackNotifier.SlackJobProperty)
jp = new jenkins.plugins.slack.SlackNotifier.SlackJobProperty(
null, // String teamDomain,
null, // String token,
"#channel_to_notified", // String room,
false, // boolean startNotification,