symbol-fstring 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7123e21ed5c05290855b7c2e5602352722b41165d7b310c7d0bfa7d84ad48421
4
+ data.tar.gz: 1e7086d700e9d677f83762d4afa603a466abf294a7844398c532fd7b8dd1538c
5
+ SHA512:
6
+ metadata.gz: 9b097ad93b7ed268ae854038730e66606bb6da56798e04928fddb39d7393c4180efed6398b8b2f4afb8561c945d8163d84ea879d6971e36205d9205c7c40b21b
7
+ data.tar.gz: 9013fbf56f8f3c3c1aa6a7a45ceab36cdf83e2ba0539baefde05d27df5eecaf490ea50a2d125eb964c098908d589d797d76b17b373c0ba655d88d9cda78dd48f
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ *.bundle
11
+ *.so
12
+ Gemfile.lock
13
+
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.5
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fstring.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Jean Boussier
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,80 @@
1
+ # symbol-fstring
2
+
3
+ `symbol-fstring` is a Ruby extension that provide access to symbols internal string representations.
4
+
5
+ ## Why?
6
+
7
+ In Ruby many APIs tend to accept symbols, but regularly convert them to string internally. The typical example is
8
+ `ActiveSupport::HashWithIndifferentAccess`, but there are plenty more.
9
+
10
+ The problem with this is that `Symbol#to_s` creates a new string every time it is invoked, and since it often happens
11
+ in hotspots, it causes a lot of work for the garbage collector, and cause many identical strings to be kept in memory.
12
+
13
+ There was [an attempt to make `Symbol#to_s` return it's internal fstring for Ruby 2.7](https://bugs.ruby-lang.org/issues/16150),
14
+ but unfortunately it got reverted, and probably won't happen before a while.
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'symbol-fstring'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install symbol-fstring
31
+
32
+ ## Usage
33
+
34
+ `FString` can be used in two ways.
35
+
36
+ ### `FString.patch_symbol!`
37
+
38
+ If your application and your dependencies are compatible with it, you can change `Symbol#to_s` behavior globally with:
39
+
40
+ ```ruby
41
+ FString.patch_symbol!
42
+ ```
43
+
44
+ Or you can also add it this way in your Gemfile:
45
+
46
+ ```ruby
47
+ gem 'fstring', require: 'fstring/all'
48
+ ```
49
+
50
+ ### `FString.to_s`
51
+
52
+ If you don't want to patch the core `Symbol` class, you can alternatively substitute `some_symbol.to_s` for
53
+ `FString.to_s(some_symbol)` in your own code.
54
+
55
+ ### Benchmark
56
+
57
+ From `benchmark/symbol-to_s`
58
+
59
+ ```
60
+ Symbol#to_s (orig) 12.786M (± 1.5%) i/s - 64.032M in 5.009020s
61
+ FString#symbol_to_s 20.371M (± 1.7%) i/s - 101.981M in 5.007706s
62
+ FString#to_s 19.086M (± 1.5%) i/s - 95.410M in 5.000016s
63
+ Symbol#to_s (patch) 21.669M (± 1.9%) i/s - 108.591M in 5.013331s
64
+ ```
65
+
66
+ But there's also a reduced pressure on the garbage collector.
67
+
68
+ ## Development
69
+
70
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
71
+
72
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/symbol-fstring.
77
+
78
+ ## License
79
+
80
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,18 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => [:build, :test]
11
+
12
+ require 'rake/extensiontask'
13
+ GEMSPEC = eval(File.read('fstring.gemspec'))
14
+ Rake::ExtensionTask.new('fstring', GEMSPEC) do |ext|
15
+ ext.ext_dir = 'ext/fstring'
16
+ ext.lib_dir = 'lib/fstring'
17
+ end
18
+ task build: :compile
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require "fstring"
6
+ require "active_support/hash_with_indifferent_access"
7
+ require 'benchmark/ips'
8
+
9
+ class PatchedHash < ActiveSupport::HashWithIndifferentAccess
10
+ private
11
+
12
+ def convert_key(key)
13
+ key.is_a?(Symbol) ? FString.symbol_to_s(key) : key
14
+ end
15
+ end
16
+
17
+ BASE = ActiveSupport::HashWithIndifferentAccess.new(foo: 42)
18
+ PATCHED = PatchedHash.new(foo: 42)
19
+
20
+ Benchmark.ips do |bench|
21
+ bench.report("HWIA#[:foo]") { BASE[:foo] }
22
+ bench.report("PatchedHash#[:foo]") { PATCHED[:foo] }
23
+ end
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require "fstring"
6
+ require 'benchmark/ips'
7
+
8
+ FString.patch_symbol!
9
+
10
+ Benchmark.ips do |bench|
11
+ bench.report("Symbol#to_s (orig)") { :foo.id2name }
12
+ bench.report("FString#symbol_to_s") { FString.symbol_to_s(:foo) }
13
+ bench.report("FString#to_s") { FString.to_s(:foo) }
14
+ bench.report("Symbol#to_s (patch)") { :foo.to_s }
15
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fstring"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/dev.yml ADDED
@@ -0,0 +1,22 @@
1
+ name: fstring
2
+
3
+ type: ruby
4
+
5
+ up:
6
+ - homebrew:
7
+ - openssl
8
+ - ruby: 2.6.3
9
+ - bundler
10
+
11
+ commands:
12
+ console:
13
+ desc: 'start a console'
14
+ run: bin/console
15
+ run:
16
+ desc: 'start the application'
17
+ run: bin/run
18
+ test:
19
+ syntax:
20
+ argument: file
21
+ optional: args...
22
+ run: bin/testunit
@@ -0,0 +1,10 @@
1
+ require 'mkmf'
2
+
3
+ $CFLAGS = "-D_GNU_SOURCE -Werror -Wall "
4
+ if ENV.key?('DEBUG')
5
+ $CFLAGS << "-O0 -g -DDEBUG"
6
+ else
7
+ $CFLAGS << "-O3"
8
+ end
9
+
10
+ create_makefile('fstring/fstring')
@@ -0,0 +1,54 @@
1
+ #include "fstring.h"
2
+
3
+ VALUE
4
+ rb_symbol_to_fstring(VALUE symbol)
5
+ {
6
+ return rb_sym2str(symbol);
7
+ }
8
+
9
+ VALUE
10
+ rb_fstring_to_s(VALUE rb_mFString, VALUE object)
11
+ {
12
+ if (RB_TYPE_P(object, T_SYMBOL)) {
13
+ return rb_sym2str(object);
14
+ } else {
15
+ return rb_funcall(object, rb_intern("to_s"), 0);
16
+ }
17
+ }
18
+
19
+ VALUE
20
+ rb_fstring_symbol_to_s(VALUE rb_mFString, VALUE symbol)
21
+ {
22
+ return rb_sym2str(symbol);
23
+ }
24
+
25
+ VALUE
26
+ rb_fstring_patch_symbol(VALUE rb_mFString)
27
+ {
28
+ VALUE rb_cSymbol;
29
+ rb_cSymbol = rb_const_get(rb_cObject, rb_intern("Symbol"));
30
+ rb_undef(rb_cSymbol, rb_intern("to_s"));
31
+ rb_define_method(rb_cSymbol, "to_s", rb_symbol_to_fstring, 0);
32
+ return Qnil;
33
+ }
34
+
35
+ VALUE
36
+ rb_fstring_unpatch_symbol(VALUE rb_mFString)
37
+ {
38
+ VALUE rb_cSymbol;
39
+ rb_cSymbol = rb_const_get(rb_cObject, rb_intern("Symbol"));
40
+ rb_undef(rb_cSymbol, rb_intern("to_s"));
41
+ rb_define_method(rb_cSymbol, "to_s", rb_sym_to_s, 0);
42
+ return Qnil;
43
+ }
44
+
45
+ void Init_fstring()
46
+ {
47
+ VALUE rb_mFString;
48
+
49
+ rb_mFString = rb_const_get(rb_cObject, rb_intern("FString"));
50
+ rb_define_singleton_method(rb_mFString, "symbol_to_s", rb_fstring_symbol_to_s, 1);
51
+ rb_define_singleton_method(rb_mFString, "to_s", rb_fstring_to_s, 1);
52
+ rb_define_singleton_method(rb_mFString, "patch_symbol!", rb_fstring_patch_symbol, 0);
53
+ rb_define_singleton_method(rb_mFString, "unpatch_symbol!", rb_fstring_unpatch_symbol, 0);
54
+ }
@@ -0,0 +1,9 @@
1
+ #ifndef FSTRING_H
2
+ #define FSTRING_H
3
+
4
+ #include "ruby.h"
5
+ #include "ruby/intern.h"
6
+
7
+ void Init_fstring();
8
+
9
+ #endif //FSTRING_H
@@ -0,0 +1,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fstring/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "symbol-fstring"
8
+ spec.version = FString::VERSION
9
+ spec.authors = ["Jean Boussier"]
10
+ spec.email = ["jean.boussier@gmail.com"]
11
+
12
+ spec.summary = %q{Allow fast access to internal fstring of symbols and modules}
13
+ spec.homepage = "https://github.com/Shopify/symbol-fstring"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = spec.homepage
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.extensions = ['ext/fstring/extconf.rb']
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_development_dependency "bundler", "~> 2.0"
39
+ spec.add_development_dependency "rake", "~> 10.0"
40
+ spec.add_development_dependency "minitest", "~> 5.0"
41
+ spec.add_development_dependency "benchmark-ips", "~> 2.7"
42
+ spec.add_development_dependency "activesupport"
43
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fstring/version"
4
+
5
+ module FString
6
+ end
7
+
8
+ require "fstring/fstring"
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fstring"
4
+ FString.patch_symbol!
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FString
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fstring"
metadata ADDED
@@ -0,0 +1,137 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: symbol-fstring
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jean Boussier
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: benchmark-ips
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - jean.boussier@gmail.com
86
+ executables: []
87
+ extensions:
88
+ - ext/fstring/extconf.rb
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - benchmark/hash-with-indifferent-access
99
+ - benchmark/symbol-to-s
100
+ - bin/console
101
+ - bin/setup
102
+ - dev.yml
103
+ - ext/fstring/extconf.rb
104
+ - ext/fstring/fstring.c
105
+ - ext/fstring/fstring.h
106
+ - fstring.gemspec
107
+ - lib/fstring.rb
108
+ - lib/fstring/all.rb
109
+ - lib/fstring/version.rb
110
+ - lib/symbol-fstring.rb
111
+ homepage: https://github.com/Shopify/symbol-fstring
112
+ licenses:
113
+ - MIT
114
+ metadata:
115
+ allowed_push_host: https://rubygems.org
116
+ homepage_uri: https://github.com/Shopify/symbol-fstring
117
+ source_code_uri: https://github.com/Shopify/symbol-fstring
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubygems_version: 3.0.3
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: Allow fast access to internal fstring of symbols and modules
137
+ test_files: []