rails-patch-json-encode 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8f6e4506779f59734a8f7e0de0e32a59972e963e
4
+ data.tar.gz: 8b0556765ad8e5ecd0ec80d379cf004c5e85a604
5
+ SHA512:
6
+ metadata.gz: d2248a53df54b7dec6839db35aa736adb78772cb220a29295ecf73edb3b75bb2aae10f89b6c5c0ebf7c8d47e8ab535f548e965a5ac84c783d1a9fdf2176645ae
7
+ data.tar.gz: debf84761c18bc0c9d78aabccae3993146bc3959fee74738120d32778cf65e9c2ec11163d9e2d66c5e8676d47146e84d35075dafb0ac0b1820a059b646dbbddc
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails-patch-json-encode.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 lulalala
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,54 @@
1
+ # Rails::Patch::Json::Encode
2
+
3
+ This is a monkey patch for Rails in order to speed up its JSON encoding, and to draw people's attention to this [Rails issue](https://github.com/rails/rails/issues/9212).
4
+
5
+ For full details please read Jason Hutchens' [blog post](http://devblog.agworld.com.au/post/42586025923/the-performance-of-to-json-in-rails-sucks-and-theres).
6
+
7
+ All credits goes to [Jason Hutchens](https://github.com/jasonhutchens) for discovering the issue and providing the code for this monkey patch.
8
+
9
+ ## Usage
10
+
11
+ First, go to your Rails console and type:
12
+
13
+ data = Hash.new
14
+ key = 'aaa'
15
+ 1000.times { data[key.succ!] = data.keys }
16
+ 1000 * Benchmark.realtime { data.to_json }
17
+
18
+ See how Rails performs before the patch.
19
+
20
+ Then bundle install this gem with a fast JSON encoding gem in your Rails' Gemfile.
21
+
22
+ gem 'rails-patch-json-encode'
23
+ gem 'oj'
24
+
25
+ In this case I choose the oj gem, but you can [choose a JSON gem that multi_json supports](https://github.com/intridea/multi_json#supported-json-engines).
26
+
27
+ Rails should now use the faster encoder. Now restart your console again and re-run the test to see how the performance changes.
28
+
29
+ The actual performance boost on real-world applications will probably be less than that. For one of my page I see the rendering time dropped by 25%.
30
+
31
+ ## Warning
32
+
33
+ This gem may break your app. **Test your app**. I am not sure if this is production ready.
34
+
35
+ ## What's with the name
36
+
37
+ This is just a temporal monkey patch, and a monkey patch isn't supposed to have a fancy name.
38
+
39
+ ## Related reading
40
+
41
+ * Jason Hutchen's [blog post](http://devblog.agworld.com.au/post/42586025923/the-performance-of-to-json-in-rails-sucks-and-theres)
42
+ * [Rails issue](https://github.com/rails/rails/issues/9212)
43
+ * [Current refactoring done by chancancode](https://github.com/rails/rails/pull/12183) trying to address this issue.
44
+ * [A pull-request related to this](https://github.com/intridea/multi_json/pull/138) about JSON and Rails trying to patch the same method*
45
+ * [Original issue and fix that resulted in this issue](https://rails.lighthouseapp.com/projects/8994/tickets/4890)
46
+
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ require "rails/patch/json/encode/version"
2
+
3
+ # Code from http://devblog.agworld.com.au/post/42586025923/the-performance-of-to-json-in-rails-sucks-and-theres
4
+ # essentially reversing Rails' hard-coded call to ActiveSupport::JSON.encode
5
+ [Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].each do |klass|
6
+ klass.class_eval do
7
+ def to_json(opts = {})
8
+ MultiJson::dump(self.as_json(opts), opts)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Rails
2
+ module Patch
3
+ module Json
4
+ module Encode
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails/patch/json/encode/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails-patch-json-encode"
8
+ spec.version = Rails::Patch::Json::Encode::VERSION
9
+ spec.authors = ["lulalala", "Jason Hutchens"]
10
+ spec.email = ["mark@goodlife.tw"]
11
+ spec.description = %q{A monkey patch to speed up Rails' JSON generation time.}
12
+ spec.summary = %q{A monkey patch to speed up Rails' JSON generation time.}
13
+ spec.homepage = "https://github.com/GoodLife/rails-patch-json-encode"
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_dependency 'multi_json'
25
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-patch-json-encode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - lulalala
8
+ - Jason Hutchens
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: '1.3'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: multi_json
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ description: A monkey patch to speed up Rails' JSON generation time.
57
+ email:
58
+ - mark@goodlife.tw
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/rails/patch/json/encode.rb
69
+ - lib/rails/patch/json/encode/version.rb
70
+ - rails-patch-json-encode.gemspec
71
+ homepage: https://github.com/GoodLife/rails-patch-json-encode
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.0.5
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: A monkey patch to speed up Rails' JSON generation time.
95
+ test_files: []
96
+ has_rdoc: