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 +4 -4
- data/.github/workflows/ci.yml +23 -0
- data/README.md +10 -11
- data/benchmark/hash-with-indifferent-access +1 -1
- data/benchmark/symbol-to-s +2 -6
- data/ext/fstring/fstring.c +2 -50
- data/fstring.gemspec +6 -1
- data/lib/fstring.rb +14 -2
- data/lib/fstring/all.rb +6 -2
- data/lib/fstring/version.rb +1 -1
- metadata +22 -9
- data/.travis.yml +0 -7
- data/ext/fstring/fstring.h +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfc550b2d94bcbd9fb144b2541a0bdac0591aabe7d34875f9eb7f026ee383d78
|
4
|
+
data.tar.gz: 04c7d8abd9e9d6eea5ccddf84cae1ab219b6bf6926380766145329648336193e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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,
|
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
|
-
|
61
|
-
|
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.
|
data/benchmark/symbol-to-s
CHANGED
@@ -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.
|
12
|
-
bench.report("
|
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
|
data/ext/fstring/fstring.c
CHANGED
@@ -1,54 +1,6 @@
|
|
1
|
-
#include "
|
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
|
-
|
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
|
}
|
data/fstring.gemspec
CHANGED
@@ -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
|
-
|
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
|
data/lib/fstring.rb
CHANGED
@@ -3,6 +3,18 @@
|
|
3
3
|
require "fstring/version"
|
4
4
|
|
5
5
|
module FString
|
6
|
-
|
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
|
-
|
16
|
+
def unpatch_symbol!
|
17
|
+
Symbol.alias_method(:to_s, :_original_to_s)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/fstring/all.rb
CHANGED
data/lib/fstring/version.rb
CHANGED
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:
|
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:
|
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
|
-
|
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.
|
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: []
|
data/.travis.yml
DELETED