symbol-fstring 0.1.0 → 1.0.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7123e21ed5c05290855b7c2e5602352722b41165d7b310c7d0bfa7d84ad48421
4
- data.tar.gz: 1e7086d700e9d677f83762d4afa603a466abf294a7844398c532fd7b8dd1538c
3
+ metadata.gz: cfc550b2d94bcbd9fb144b2541a0bdac0591aabe7d34875f9eb7f026ee383d78
4
+ data.tar.gz: 04c7d8abd9e9d6eea5ccddf84cae1ab219b6bf6926380766145329648336193e
5
5
  SHA512:
6
- metadata.gz: 9b097ad93b7ed268ae854038730e66606bb6da56798e04928fddb39d7393c4180efed6398b8b2f4afb8561c945d8163d84ea879d6971e36205d9205c7c40b21b
7
- data.tar.gz: 9013fbf56f8f3c3c1aa6a7a45ceab36cdf83e2ba0539baefde05d27df5eecaf490ea50a2d125eb964c098908d589d797d76b17b373c0ba655d88d9cda78dd48f
6
+ metadata.gz: 7e9b887c028e4abdaf1a6fc08b308ae9eb05ca52b23941b6c7495484e7e9f865133629232db9d3fe48fabbd9479bce9636832c5860f963c26c9d2d75e3537a0c
7
+ data.tar.gz: 178aa618f95c6284115c860c5300e27a0686da90c3708a36bdfcb246ed0891ef4a5a7588b3d011d9053a60d00b5e2abfd173afd46ec6f1474105d349a4c46e5c
@@ -0,0 +1,23 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby: [ ruby-head, 2.7, 2.6, 2.5 ]
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - name: Set up Ruby
15
+ uses: ruby/setup-ruby@v1
16
+ with:
17
+ ruby-version: ${{ matrix.ruby }}
18
+ - name: Install dependencies
19
+ run: bundle install
20
+ - name: Run test
21
+ run: rake
22
+ - name: Install gem
23
+ run: rake install
data/README.md CHANGED
@@ -11,7 +11,9 @@ The problem with this is that `Symbol#to_s` creates a new string every time it i
11
11
  in hotspots, it causes a lot of work for the garbage collector, and cause many identical strings to be kept in memory.
12
12
 
13
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.
14
+ but unfortunately it got reverted, instead [Ruby 3.0 should have `Symbol#name`](https://github.com/ruby/ruby/commit/eb67c603ca7e435181684857e650b4633fda5bb6).
15
+
16
+ This gem backports the `Symbol#name` method for older rubies, and optionally allow to replace `Symbol#to_s` by `Symbol#name`.
15
17
 
16
18
  ## Installation
17
19
 
@@ -33,6 +35,10 @@ Or install it yourself as:
33
35
 
34
36
  `FString` can be used in two ways.
35
37
 
38
+ ### `Symbol.name`
39
+
40
+ By default this gem backport the Ruby 3.0 `Symbol#name` method.
41
+
36
42
  ### `FString.patch_symbol!`
37
43
 
38
44
  If your application and your dependencies are compatible with it, you can change `Symbol#to_s` behavior globally with:
@@ -44,23 +50,16 @@ FString.patch_symbol!
44
50
  Or you can also add it this way in your Gemfile:
45
51
 
46
52
  ```ruby
47
- gem 'fstring', require: 'fstring/all'
53
+ gem 'symbol-fstring', require: 'fstring/all'
48
54
  ```
49
55
 
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
56
  ### Benchmark
56
57
 
57
58
  From `benchmark/symbol-to_s`
58
59
 
59
60
  ```
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
61
+ Symbol#to_s (orig) 11.748M (± 1.7%) i/s - 58.786M in 5.005571s
62
+ Symbol#name (patch) 18.067M (± 1.9%) i/s - 90.510M in 5.011549s
64
63
  ```
65
64
 
66
65
  But there's also a reduced pressure on the garbage collector.
@@ -10,7 +10,7 @@ class PatchedHash < ActiveSupport::HashWithIndifferentAccess
10
10
  private
11
11
 
12
12
  def convert_key(key)
13
- key.is_a?(Symbol) ? FString.symbol_to_s(key) : key
13
+ key.is_a?(Symbol) ? key.name : key
14
14
  end
15
15
  end
16
16
 
@@ -5,11 +5,7 @@ require 'bundler/setup'
5
5
  require "fstring"
6
6
  require 'benchmark/ips'
7
7
 
8
- FString.patch_symbol!
9
-
10
8
  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 }
9
+ bench.report("Symbol#to_s (orig)") { :foo.to_s }
10
+ bench.report("Symbol#name (patch)") { :foo.name }
15
11
  end
@@ -1,54 +1,6 @@
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
- }
1
+ #include "ruby.h"
44
2
 
45
3
  void Init_fstring()
46
4
  {
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);
5
+ rb_define_method(rb_cSymbol, "name", rb_sym2str, 0);
54
6
  }
@@ -30,7 +30,11 @@ Gem::Specification.new do |spec|
30
30
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
31
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
32
  end
33
- spec.extensions = ['ext/fstring/extconf.rb']
33
+
34
+ unless Symbol.method_defined?(:name)
35
+ spec.extensions = ['ext/fstring/extconf.rb']
36
+ end
37
+
34
38
  spec.bindir = "exe"
35
39
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
40
  spec.require_paths = ["lib"]
@@ -40,4 +44,5 @@ Gem::Specification.new do |spec|
40
44
  spec.add_development_dependency "minitest", "~> 5.0"
41
45
  spec.add_development_dependency "benchmark-ips", "~> 2.7"
42
46
  spec.add_development_dependency "activesupport"
47
+ spec.add_development_dependency "rake-compiler"
43
48
  end
@@ -3,6 +3,18 @@
3
3
  require "fstring/version"
4
4
 
5
5
  module FString
6
- end
6
+ require "fstring/fstring" unless Symbol.method_defined?(:name)
7
+
8
+ class << self
9
+ def patch_symbol!
10
+ unless Symbol.method_defined?(:_original_to_s)
11
+ Symbol.alias_method(:_original_to_s, :to_s)
12
+ end
13
+ Symbol.alias_method(:to_s, :name)
14
+ end
7
15
 
8
- require "fstring/fstring"
16
+ def unpatch_symbol!
17
+ Symbol.alias_method(:to_s, :_original_to_s)
18
+ end
19
+ end
20
+ end
@@ -1,4 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "fstring"
4
- FString.patch_symbol!
3
+ if Symbol.method_defined?(:name)
4
+ Symbol.alias_method(:to_s, :name)
5
+ else
6
+ require "fstring"
7
+ FString.patch_symbol!
8
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FString
4
- VERSION = "0.1.0"
4
+ VERSION = "1.0.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: symbol-fstring
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean Boussier
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-05 00:00:00.000000000 Z
11
+ date: 2020-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,7 +80,21 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- description:
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake-compiler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
84
98
  email:
85
99
  - jean.boussier@gmail.com
86
100
  executables: []
@@ -88,8 +102,8 @@ extensions:
88
102
  - ext/fstring/extconf.rb
89
103
  extra_rdoc_files: []
90
104
  files:
105
+ - ".github/workflows/ci.yml"
91
106
  - ".gitignore"
92
- - ".travis.yml"
93
107
  - Gemfile
94
108
  - Gemfile.lock
95
109
  - LICENSE.txt
@@ -102,7 +116,6 @@ files:
102
116
  - dev.yml
103
117
  - ext/fstring/extconf.rb
104
118
  - ext/fstring/fstring.c
105
- - ext/fstring/fstring.h
106
119
  - fstring.gemspec
107
120
  - lib/fstring.rb
108
121
  - lib/fstring/all.rb
@@ -115,7 +128,7 @@ metadata:
115
128
  allowed_push_host: https://rubygems.org
116
129
  homepage_uri: https://github.com/Shopify/symbol-fstring
117
130
  source_code_uri: https://github.com/Shopify/symbol-fstring
118
- post_install_message:
131
+ post_install_message:
119
132
  rdoc_options: []
120
133
  require_paths:
121
134
  - lib
@@ -130,8 +143,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
143
  - !ruby/object:Gem::Version
131
144
  version: '0'
132
145
  requirements: []
133
- rubygems_version: 3.0.3
134
- signing_key:
146
+ rubygems_version: 3.1.2
147
+ signing_key:
135
148
  specification_version: 4
136
149
  summary: Allow fast access to internal fstring of symbols and modules
137
150
  test_files: []
@@ -1,7 +0,0 @@
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
@@ -1,9 +0,0 @@
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