monorails 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e0f2be15af49f2a1f5be7b385cb645ee8cc790a8
4
+ data.tar.gz: a01ad8d962f852efd2ca21797863d10d2005de9b
5
+ SHA512:
6
+ metadata.gz: 72621755345fd437539e34a3aac1fdefa2416da96ae3cefca9cfd8b07719395632b5283151faf82bc339919ea2d68540a6e9440f185d735c2572445dc22e4af4
7
+ data.tar.gz: 0043e3e810cde45d5fc2e183d64a34dffe2c4289474087c872c2f99e25dff4ed803a62216597bc94028a1dfe1ad8b6c1be9c4d8b3c41fd6c22f35470bf968ece
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ sudo: false
3
+ rvm:
4
+ - 2.0
5
+ - 2.1
6
+ - 2.2
7
+ gemfile:
8
+ - gemfiles/Gemfile.ar-4.0
9
+ - gemfiles/Gemfile.ar-4.1
10
+ - gemfiles/Gemfile.ar-4.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in monorails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jean Boussier
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # Monorails
2
+
3
+ [![Build Status](https://travis-ci.org/byroot/monorails.svg)](http://travis-ci.org/byroot/monorails)
4
+ [![Gem Version](https://badge.fury.io/rb/monorails.png)](http://badge.fury.io/rb/byroot/monorails)
5
+
6
+ Monkey patch Rails to make it faster in non thread safe applications
7
+
8
+ **CAUTION: Use this gem at your own risk.**
9
+
10
+ If your application is not doing any threading, then `monorails` can bring you a few performance
11
+ improvements at the cost of losing thread safety.
12
+
13
+ ## Explanation
14
+
15
+ First `monorails` includes `thread_hazardous` that bring a substential performance improvement.
16
+ [See it's readme for explanation](https://github.com/byroot/thread_hazardous#explanation).
17
+
18
+ `monorails` also patches `ActiveSupport::PerThreadRegistry` that is used in a couple Rails hotspots.
19
+ `PerThreadRegistry` stores an instance in `Thread.current` which is way slower than a regular instance variable:
20
+
21
+ ```
22
+ Calculating -------------------------------------
23
+ thread-safe 25.176k i/100ms
24
+ thread-unsafe 30.069k i/100ms
25
+ -------------------------------------------------
26
+ thread-safe 1.480M (± 6.5%) i/s - 7.377M
27
+ thread-unsafe 2.480M (± 7.2%) i/s - 12.328M
28
+ ```
29
+ For full details see [the benchmark implementation](https://gist.github.com/byroot/919e7756be7f89ec19af)
30
+
31
+ `monorails` also backport [a performance patch from Rails 4.2](https://github.com/rails/rails/commit/28d52c59f2cb32180ca24770bf95597ea3ad8198) for older versions.
32
+
33
+ These operations themselves are not that costly, but they are often used in heavy hotspots, especially in Rails, so in the end it adds up.
34
+ Here's a profiling real life application under real life load:
35
+
36
+ ```
37
+ ==================================
38
+ Mode: wall(1000)
39
+ Samples: 15836 (0.35% miss rate)
40
+ GC: 696 (4.40%)
41
+ ==================================
42
+ TOTAL (pct) SAMPLES (pct) FRAME
43
+ 429 (2.5%) 429 (2.5%) ActiveSupport::PerThreadRegistry#per_thread_registry_instance
44
+ 419 (2.6%) 419 (2.6%) ThreadSafe::NonConcurrentCacheBackend#[]
45
+ 208 (1.3%) 208 (1.3%) ThreadSafe::NonConcurrentCacheBackend#dupped_backend
46
+ 75 (0.5%) 75 (0.5%) block in ThreadSafe::Cache#values
47
+ 151 (0.9%) 147 (0.9%) block in ActiveRecord::AttributeMethods::ClassMethods#define_attribute_methods
48
+ ```
49
+
50
+ ## Installation
51
+
52
+ Add this line to your application's Gemfile:
53
+
54
+ ```ruby
55
+ gem 'monorails'
56
+ ```
57
+
58
+ And then execute:
59
+
60
+ $ bundle
61
+
62
+ Or install it yourself as:
63
+
64
+ $ gem install monorails
65
+
66
+ ## Usage
67
+
68
+ Nothing more to do, it's all automatic.
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it ( https://github.com/byroot/monorails/fork )
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.0.0'
4
+ gem 'bundler', '~> 1.7'
5
+ gem 'thread_hazardous', '~> 0.0.1'
6
+ gem 'rake'
7
+ gem 'rspec'
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.1.0'
4
+ gem 'thread_hazardous', '~> 0.0.1'
5
+ gem 'bundler', '~> 1.7'
6
+ gem 'rake'
7
+ gem 'rspec'
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord', '~> 4.2.0'
4
+ gem 'thread_hazardous', '~> 0.0.1'
5
+ gem 'bundler', '~> 1.7'
6
+ gem 'rake'
7
+ gem 'rspec'
@@ -0,0 +1,9 @@
1
+ module DoubleCheckedLockPatch
2
+ # Backport of https://github.com/rails/rails/commit/28d52c59f2cb32180ca24770bf95597ea3ad8198
3
+ def define_attribute_methods
4
+ return false if @attribute_methods_generated
5
+ super
6
+ end
7
+ end
8
+
9
+ ActiveRecord::Base.extend(DoubleCheckedLockPatch)
@@ -0,0 +1,7 @@
1
+ module ActiveSupport::PerThreadRegistry
2
+ private
3
+
4
+ def instance
5
+ @__instance ||= new
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveSupport::PerThreadRegistry
2
+ private
3
+
4
+ def per_thread_registry_instance
5
+ @__instance ||= new
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Monorails
2
+ VERSION = '0.0.1'
3
+ end
data/lib/monorails.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'monorails/version'
2
+ require 'thread_hazardous'
3
+
4
+ require 'active_support/version'
5
+ require 'active_support/per_thread_registry'
6
+
7
+ if ActiveSupport.version < Gem::Version.new('4.1')
8
+ require 'monorails/per_thread_registry_40'
9
+ else
10
+ require 'monorails/per_thread_registry'
11
+ end
12
+
13
+ if ActiveSupport.version < Gem::Version.new('4.2')
14
+ require 'active_record'
15
+ require 'monorails/attribute_methods'
16
+ end
data/monorails.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'monorails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'monorails'
8
+ spec.version = Monorails::VERSION
9
+ spec.authors = ['Jean Boussier']
10
+ spec.email = ['jean.boussier@gmail.com']
11
+ spec.summary = %q{A set of Rails patches to speedup applications that do not need thread safety.}
12
+ spec.description = %q{CAUTION: Use at your own risks.}
13
+ spec.homepage = 'https://github.com/byroot/monorails'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'activesupport', '~> 4.0'
22
+ spec.add_runtime_dependency 'thread_hazardous', '~> 0.0.1'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.7'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ class Registry
4
+ extend ActiveSupport::PerThreadRegistry
5
+
6
+ def itself
7
+ self
8
+ end
9
+
10
+ def thruth
11
+ 42
12
+ end
13
+ end
14
+
15
+ describe ActiveSupport::PerThreadRegistry do
16
+
17
+ it 'is memoized' do
18
+ expect { nil }.to_not change { Registry.itself.object_id }
19
+ end
20
+
21
+ it 'still forward methods just fine' do
22
+ expect(Registry.thruth).to be == 42
23
+ end
24
+
25
+ it 'share the memoized instance between all threads (that is why you should not use that patch in threading environments)' do
26
+ child_thread_object_id = nil
27
+ Thread.new { child_thread_object_id = Registry.itself.object_id }.join
28
+ expect(child_thread_object_id).to be == Registry.itself.object_id
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ require 'monorails'
2
+
3
+ RSpec.configure do |config|
4
+ config.expect_with :rspec do |expectations|
5
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
6
+ end
7
+
8
+ config.mock_with :rspec do |mocks|
9
+ mocks.verify_partial_doubles = true
10
+ end
11
+
12
+ config.order = :random
13
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monorails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jean Boussier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thread_hazardous
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: 'CAUTION: Use at your own risks.'
84
+ email:
85
+ - jean.boussier@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - gemfiles/Gemfile.ar-4.0
98
+ - gemfiles/Gemfile.ar-4.1
99
+ - gemfiles/Gemfile.ar-4.2
100
+ - lib/monorails.rb
101
+ - lib/monorails/attribute_methods.rb
102
+ - lib/monorails/per_thread_registry.rb
103
+ - lib/monorails/per_thread_registry_40.rb
104
+ - lib/monorails/version.rb
105
+ - monorails.gemspec
106
+ - spec/per_thread_registry_spec.rb
107
+ - spec/spec_helper.rb
108
+ homepage: https://github.com/byroot/monorails
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.2.2
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: A set of Rails patches to speedup applications that do not need thread safety.
132
+ test_files:
133
+ - spec/per_thread_registry_spec.rb
134
+ - spec/spec_helper.rb