thread_hazardous 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 32481830314be733321546098794f0d6a486faf3
4
+ data.tar.gz: 21cad240c23a412d93c88e4c608556bb994ea671
5
+ SHA512:
6
+ metadata.gz: ae5005d936c819ec9ed80fe81f995842ff6d7eb4917929c3d68bc9af9770425b7d1e2b98c8cf7d7fdbd180fbf01013abcb10afb17b8b6c9542f611d38ea0814d
7
+ data.tar.gz: b398cc4c8d6105afa08b66374bb5381f2f0139c724be335056e3ce6debd1a1e70ce9fffe95ca13718a6a5ef97654bf92bb8b743c966e007821beed60455bc0b7
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in thread_hazardous.gemspec
4
+ gemspec
@@ -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.
@@ -0,0 +1,61 @@
1
+ # ThreadHazardous
2
+
3
+ Monkey patch thread_safe to make it faster in non thread safe applications
4
+
5
+ _CAUTION: Use this gem at your own risk._**_
6
+
7
+ If you use gems that depends on the `thread_safe` gem,
8
+ e.g. Rails, but your application is not doing any threading, then `thread_hazardous` can bring you a few performance
9
+ improvements at the cost of making these dependencies non thread safe.
10
+
11
+ ## Explanation
12
+
13
+ `ThreadSafe::Cache` on MRI to provide thread safety wrap all it's method in a mutex.
14
+ Also to ensure the cache won't be modified during an iteration, it clones the instance. So each iteration allocate an extra object.
15
+
16
+
17
+ 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.
18
+ Here's a profiling real life application under real life load:
19
+
20
+ ```
21
+ ==================================
22
+ Mode: wall(1000)
23
+ Samples: 15836 (0.35% miss rate)
24
+ GC: 696 (4.40%)
25
+ ==================================
26
+ TOTAL (pct) SAMPLES (pct) FRAME
27
+ 419 (2.6%) 419 (2.6%) ThreadSafe::NonConcurrentCacheBackend#[]
28
+ 208 (1.3%) 208 (1.3%) ThreadSafe::NonConcurrentCacheBackend#dupped_backend
29
+ 75 (0.5%) 75 (0.5%) block in ThreadSafe::Cache#values
30
+ ```
31
+
32
+
33
+ `thread_hazardous` simply monkey patch `ThreadSafe::Cache` to remove the mutex and not clone the instance on iteration.
34
+
35
+ ## Installation
36
+
37
+ Add this line to your application's Gemfile:
38
+
39
+ ```ruby
40
+ gem 'thread_hazardous'
41
+ ```
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ Or install it yourself as:
48
+
49
+ $ gem install thread_hazardous
50
+
51
+ ## Usage
52
+
53
+ Nothing more to do, it's all automatic.
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it ( https://github.com/byroot/thread_hazardous/fork )
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new :test do |t|
5
+ t.libs << 'lib'
6
+ t.test_files = FileList['test/**/*.rb']
7
+ end
8
+
9
+ task default: :test
10
+
@@ -0,0 +1,7 @@
1
+ require 'thread_hazardous/version'
2
+
3
+ if !defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby'
4
+ require 'thread_hazardous/mri_cache_backend'
5
+ else
6
+ STDERR.puts('thread_hazardous is a noop when not running on MRI')
7
+ end
@@ -0,0 +1,17 @@
1
+ require 'thread_safe/cache'
2
+
3
+ # All MriCacheBackend methods are just synchronizing a mutex before calling the parent implementation
4
+ # In a non thread safe environment it is simply a waste of time.
5
+ ThreadSafe::MriCacheBackend.public_instance_methods(false).each do |method|
6
+ ThreadSafe::MriCacheBackend.send(:define_method, method, ThreadSafe::NonConcurrentCacheBackend.instance_method(method))
7
+ end
8
+
9
+ class ThreadSafe::MriCacheBackend
10
+ # The original implementation duplicate the `@backend` Hash so it do not have to lock during the full iteration
11
+ def each_pair
12
+ @backend.each_pair do |k, v|
13
+ yield k, v
14
+ end
15
+ self
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module ThreadHazardous
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,6 @@
1
+ require File.join(File.dirname(__FILE__), "test_helper")
2
+
3
+ run_thread_safe_gem_tests('test_cache', except: %i(
4
+ test_compute_if_absent_atomicity
5
+ test_each_pair_allows_modification
6
+ ))
@@ -0,0 +1,18 @@
1
+ require 'thread_safe'
2
+
3
+ cache_path = ThreadSafe::Cache.instance_method(:initialize).source_location.first
4
+ THREAD_SAFE_PATH = File.expand_path('../..', File.dirname(cache_path))
5
+
6
+ require 'thread_hazardous'
7
+
8
+ def run_thread_safe_gem_tests(suite, except: [])
9
+ require "#{THREAD_SAFE_PATH}/test/#{suite}.rb"
10
+
11
+ unless except.empty?
12
+ class_name = suite.sub(/^[a-z\d]*/) { $&.capitalize }.gsub(/(?:_|(\/))([a-z\d]*)/i) { $2.capitalize }
13
+ test_class = Object.const_get(class_name)
14
+ except.each do |method|
15
+ test_class.send(:define_method, method) { skip("#{method} is not relevant in a non-threaded environment") }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'thread_hazardous/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'thread_hazardous'
8
+ spec.version = ThreadHazardous::VERSION
9
+ spec.authors = ['Jean Boussier']
10
+ spec.email = ['jean.boussier@gmail.com']
11
+ spec.summary = %q{Monkey patch thread_safe to make it faster in non thread safe applications}
12
+ spec.description = %Q{CAUTION: Use this gem at your own risk. If you use gems that depends on the thread_safe gem,
13
+ e.g. Rails, but your application is not doing any threading, then thread_hazardous can bring you a few performance
14
+ improvements at the cost of making these dependencies non thread safe.}
15
+ spec.homepage = 'https://github.com/byroot/thread_hazardous'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_runtime_dependency 'thread_safe', '~> 0.3.4'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.7'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'minitest', '>= 4'
28
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thread_hazardous
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-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thread_safe
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '4'
69
+ description: |-
70
+ CAUTION: Use this gem at your own risk. If you use gems that depends on the thread_safe gem,
71
+ e.g. Rails, but your application is not doing any threading, then thread_hazardous can bring you a few performance
72
+ improvements at the cost of making these dependencies non thread safe.
73
+ email:
74
+ - jean.boussier@gmail.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/thread_hazardous.rb
85
+ - lib/thread_hazardous/mri_cache_backend.rb
86
+ - lib/thread_hazardous/version.rb
87
+ - test/test_cache.rb
88
+ - test/test_helper.rb
89
+ - thread_hazardous.gemspec
90
+ homepage: https://github.com/byroot/thread_hazardous
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.2
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Monkey patch thread_safe to make it faster in non thread safe applications
114
+ test_files:
115
+ - test/test_cache.rb
116
+ - test/test_helper.rb