sig 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +4 -0
- data/Gemfile +5 -3
- data/README.md +79 -15
- data/Rakefile +1 -111
- data/benchmark/compare.rb +145 -0
- data/lib/sig.rb +6 -3
- data/lib/sig/version.rb +3 -1
- data/sig.gemspec +1 -1
- metadata +8 -11
- data/.CHANGELOG.md.swp +0 -0
- data/.Rakefile.swp +0 -0
- data/.travis.yml +0 -19
- data/lib/sig/.kernel.rb.swp +0 -0
- data/spec/.sig_spec.rb.swp +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0275d2e23d0acbd4ee5ee320205b7f139d7827b5fac16d1897fd1cf3e52cadde
|
4
|
+
data.tar.gz: 33059bc6cb2c04122905353f7cef5582be23926c93d6b19df32aceb52aa7cabd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59337fd40a457f2d887424b7915b62f3743b3316b2b621ac5ea7a6c17179ab74243c80fc36d4105a983ad737d7471dbc0ae00a9427d64045bf97f23591324489
|
7
|
+
data.tar.gz: 0d77e6f24e7b2c2c8483c93d726b572aab8128fe5a65581f99d65684bd064a1904c66f10e27061bb975c313b086f6aff515cbe62daa991dd137b3500d44cae92
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -2,9 +2,11 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
+
gem 'rake'
|
6
|
+
gem 'irb' unless RUBY_ENGINE == "jruby"
|
5
7
|
gem 'minitest'
|
6
8
|
gem 'minitest-line'
|
7
9
|
gem 'minitest-reporters'
|
8
|
-
gem 'benchmark-ips'
|
9
|
-
gem 'rubype'
|
10
|
-
gem 'contracts'
|
10
|
+
# gem 'benchmark-ips'
|
11
|
+
# gem 'rubype'
|
12
|
+
# gem 'contracts'
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# `sig`: Optional Type Assertions for Ruby methods. [![[version]](https://badge.fury.io/rb/sig.svg)](http://badge.fury.io/rb/sig) [![[
|
1
|
+
# `sig`: Optional Type Assertions for Ruby methods. [![[version]](https://badge.fury.io/rb/sig.svg)](http://badge.fury.io/rb/sig) [![[ci]](https://github.com/janlelis/sig/workflows/Test/badge.svg)](https://github.com/janlelis/sig/actions?query=workflow%3ATest)
|
2
2
|
|
3
3
|
This gem adds the `sig` method that allows you to add signatures to Ruby methods. When you call the method, it will verify that the method's arguments/result fit to the previously defined behavior:
|
4
4
|
|
@@ -55,7 +55,7 @@ Nevertheless, nothing is wrong with ensuring specific behaviour of method argume
|
|
55
55
|
|
56
56
|
### Is this better than rubype?
|
57
57
|
|
58
|
-
The rubype gem achieves similar things like sig (and inspired the creation of sig). It offers a different syntax and differs in feature & implementation details, so in the end, it is a matter of taste, which gem you prefer.
|
58
|
+
The rubype gem achieves similar things like sig (and inspired the creation of sig). It offers a different syntax and differs in feature & implementation details, so in the end, it is a matter of taste, which gem you prefer.
|
59
59
|
|
60
60
|
## Setup
|
61
61
|
|
@@ -100,27 +100,91 @@ See source(https://github.com/janlelis/sig/blob/master/lib/sig.rb) or specs(http
|
|
100
100
|
|
101
101
|
## Benchmark (Take with a Grain of Salt)
|
102
102
|
|
103
|
+
You can run `rake benchmark` to run [it](https://github.com/janlelis/sig/blob/v1.0.1/Rakefile#L33-L148) on your machine.
|
104
|
+
|
105
|
+
There is still a lot room for performance improvements. Feel free to suggest some faster implementation to do the type checks (even if it is crazy and not clean, as long it does not add too much "magic", a.k.a does not make debugging harder).
|
106
|
+
|
107
|
+
Note: Starting with 0.3.0, rubype uses a C extension, which makes it much faster. The benchmark is still run with rubype 0.2.5, because 0.3.x currently does not work on jruby & rbx.
|
108
|
+
|
109
|
+
### MRI
|
110
|
+
|
103
111
|
```
|
104
112
|
ruby version: 2.2.2
|
113
|
+
ruby engine: ruby
|
114
|
+
ruby description: ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
|
115
|
+
sig version: 1.0.1
|
116
|
+
rubype version: 0.2.5
|
117
|
+
contracts version: 0.9
|
118
|
+
Calculating -------------------------------------
|
119
|
+
pure 107.628k i/100ms
|
120
|
+
sig 14.425k i/100ms
|
121
|
+
rubype 12.715k i/100ms
|
122
|
+
contracts 7.688k i/100ms
|
123
|
+
-------------------------------------------------
|
124
|
+
pure 6.856M (± 0.9%) i/s - 34.333M
|
125
|
+
sig 192.440k (± 1.5%) i/s - 966.475k
|
126
|
+
rubype 164.811k (± 0.8%) i/s - 826.475k
|
127
|
+
contracts 90.089k (± 0.7%) i/s - 453.592k
|
128
|
+
|
129
|
+
Comparison:
|
130
|
+
pure: 6855615.3 i/s
|
131
|
+
sig: 192439.7 i/s - 35.62x slower
|
132
|
+
rubype: 164810.5 i/s - 41.60x slower
|
133
|
+
contracts: 90088.6 i/s - 76.10x slower
|
134
|
+
```
|
135
|
+
|
136
|
+
### JRuby 9000
|
137
|
+
|
138
|
+
```
|
139
|
+
ruby version: 2.2.2
|
140
|
+
ruby engine: jruby
|
141
|
+
ruby description: jruby 9.0.0.0-SNAPSHOT (2.2.2) 2015-05-04 6055b79 Java HotSpot(TM) 64-Bit Server VM 24.80-b11 on 1.7.0_80-b15 +indy +jit [linux-amd64]
|
142
|
+
sig version: 1.0.1
|
143
|
+
rubype version: 0.2.5
|
144
|
+
contracts version: 0.9
|
145
|
+
Calculating -------------------------------------
|
146
|
+
pure 70.898k i/100ms
|
147
|
+
sig 5.308k i/100ms
|
148
|
+
rubype 3.152k i/100ms
|
149
|
+
contracts 279.000 i/100ms
|
150
|
+
-------------------------------------------------
|
151
|
+
pure 8.848M (±13.7%) i/s - 42.539M
|
152
|
+
sig 178.169k (±10.3%) i/s - 881.128k
|
153
|
+
rubype 119.689k (±26.5%) i/s - 444.432k
|
154
|
+
contracts 56.780k (±16.8%) i/s - 265.887k
|
155
|
+
|
156
|
+
Comparison:
|
157
|
+
pure: 8848039.4 i/s
|
158
|
+
sig: 178168.8 i/s - 49.66x slower
|
159
|
+
rubype: 119689.0 i/s - 73.93x slower
|
160
|
+
contracts: 56780.4 i/s - 155.83x slower
|
161
|
+
```
|
162
|
+
|
163
|
+
### RBX 2.5.3
|
164
|
+
|
165
|
+
```
|
166
|
+
ruby version: 2.1.0
|
167
|
+
ruby engine: rbx
|
168
|
+
ruby description: rubinius 2.5.3.c25 (2.1.0 fbb3f1e4 2015-05-02 3.4 JI) [x86_64-linux-gnu]
|
105
169
|
sig version: 1.0.1
|
106
170
|
rubype version: 0.2.5
|
107
|
-
contracts version: 0.
|
171
|
+
contracts version: 0.9
|
108
172
|
Calculating -------------------------------------
|
109
|
-
pure
|
110
|
-
sig 9.
|
111
|
-
rubype
|
112
|
-
contracts
|
173
|
+
pure 114.964k i/100ms
|
174
|
+
sig 9.654k i/100ms
|
175
|
+
rubype 3.775k i/100ms
|
176
|
+
contracts 3.964k i/100ms
|
113
177
|
-------------------------------------------------
|
114
|
-
pure
|
115
|
-
sig
|
116
|
-
rubype
|
117
|
-
contracts
|
178
|
+
pure 23.585M (± 3.3%) i/s - 117.263M
|
179
|
+
sig 134.304k (± 3.1%) i/s - 675.780k
|
180
|
+
rubype 56.042k (± 1.7%) i/s - 283.125k
|
181
|
+
contracts 69.820k (± 1.8%) i/s - 348.832k
|
118
182
|
|
119
183
|
Comparison:
|
120
|
-
pure:
|
121
|
-
sig:
|
122
|
-
|
123
|
-
|
184
|
+
pure: 23585373.7 i/s
|
185
|
+
sig: 134303.7 i/s - 175.61x slower
|
186
|
+
contracts: 69819.9 i/s - 337.80x slower
|
187
|
+
rubype: 56042.1 i/s - 420.85x slower
|
124
188
|
```
|
125
189
|
|
126
190
|
## Deactivate All Signature Checking
|
data/Rakefile
CHANGED
@@ -35,119 +35,9 @@ end
|
|
35
35
|
|
36
36
|
desc "Compare with contracts and rubype"
|
37
37
|
task :benchmark do
|
38
|
-
|
39
|
-
require "rubype"
|
40
|
-
require "rubype/version"
|
41
|
-
require "contracts"
|
42
|
-
require "contracts/version"
|
43
|
-
require_relative "lib/sig"
|
44
|
-
|
45
|
-
# - - -
|
46
|
-
|
47
|
-
class PureSum
|
48
|
-
def sum(x, y)
|
49
|
-
x + y
|
50
|
-
end
|
51
|
-
|
52
|
-
def mul(x, y)
|
53
|
-
x * y
|
54
|
-
end
|
55
|
-
end
|
56
|
-
pure_instance = PureSum.new
|
57
|
-
puts "ruby version: #{RUBY_VERSION}"
|
58
|
-
|
59
|
-
# - - -
|
60
|
-
|
61
|
-
class SigSum
|
62
|
-
sig [Numeric, Numeric], Numeric,
|
63
|
-
def sum(x, y)
|
64
|
-
x + y
|
65
|
-
end
|
66
|
-
|
67
|
-
sig [:to_i, :to_i], Numeric,
|
68
|
-
def mul(x, y)
|
69
|
-
x * y
|
70
|
-
end
|
71
|
-
end
|
72
|
-
sig_instance = SigSum.new
|
73
|
-
puts "sig version: #{Sig::VERSION}"
|
74
|
-
|
75
|
-
# - - -
|
76
|
-
|
77
|
-
class RubypeSum
|
78
|
-
def sum(x, y)
|
79
|
-
x + y
|
80
|
-
end
|
81
|
-
typesig :sum, [Numeric, Numeric] => Numeric
|
82
|
-
|
83
|
-
def mul(x, y)
|
84
|
-
x * y
|
85
|
-
end
|
86
|
-
typesig :mul, [:to_i, :to_i] => Numeric
|
87
|
-
end
|
88
|
-
rubype_instance = RubypeSum.new
|
89
|
-
puts "rubype version: #{Rubype::VERSION}"
|
90
|
-
|
91
|
-
# - - -
|
92
|
-
|
93
|
-
class ContractsSum
|
94
|
-
include Contracts
|
95
|
-
|
96
|
-
Contract Num, Num => Num
|
97
|
-
def sum(x, y)
|
98
|
-
x + y
|
99
|
-
end
|
100
|
-
|
101
|
-
Contract RespondTo[:to_i], RespondTo[:to_i] => Num
|
102
|
-
def mul(x, y)
|
103
|
-
x * y
|
104
|
-
end
|
105
|
-
end
|
106
|
-
contracts_instance = ContractsSum.new
|
107
|
-
puts "contracts version: #{Contracts::VERSION}"
|
108
|
-
|
109
|
-
Benchmark.ips do |x|
|
110
|
-
x.report("pure"){ |times|
|
111
|
-
i = 0
|
112
|
-
while i < times
|
113
|
-
pure_instance.sum(1, 2)
|
114
|
-
pure_instance.mul(1, 2)
|
115
|
-
i += 1
|
116
|
-
end
|
117
|
-
}
|
118
|
-
|
119
|
-
x.report("sig"){ |times|
|
120
|
-
i = 0
|
121
|
-
while i < times
|
122
|
-
sig_instance.sum(1, 2)
|
123
|
-
sig_instance.mul(1, 2)
|
124
|
-
i += 1
|
125
|
-
end
|
126
|
-
}
|
127
|
-
|
128
|
-
x.report("rubype"){ |times|
|
129
|
-
i = 0
|
130
|
-
while i < times
|
131
|
-
rubype_instance.sum(1, 2)
|
132
|
-
rubype_instance.mul(1, 2)
|
133
|
-
i += 1
|
134
|
-
end
|
135
|
-
}
|
136
|
-
|
137
|
-
x.report("contracts"){ |times|
|
138
|
-
i = 0
|
139
|
-
while i < times
|
140
|
-
contracts_instance.sum(1, 2)
|
141
|
-
contracts_instance.mul(1, 2)
|
142
|
-
i += 1
|
143
|
-
end
|
144
|
-
}
|
145
|
-
|
146
|
-
x.compare!
|
147
|
-
end
|
38
|
+
ruby "benchmark/compare.rb"
|
148
39
|
end
|
149
40
|
|
150
|
-
|
151
41
|
# # #
|
152
42
|
# Specs
|
153
43
|
|
@@ -0,0 +1,145 @@
|
|
1
|
+
require "benchmark/ips"
|
2
|
+
|
3
|
+
require "rubype"
|
4
|
+
require "rubype/version"
|
5
|
+
require "typecheck"
|
6
|
+
require "contracts"
|
7
|
+
require "contracts/version"
|
8
|
+
require_relative "../lib/sig"
|
9
|
+
|
10
|
+
# - - -
|
11
|
+
|
12
|
+
puts "ruby version: #{RUBY_VERSION}"
|
13
|
+
puts "ruby engine: #{RUBY_ENGINE}"
|
14
|
+
puts "ruby description: #{RUBY_DESCRIPTION}"
|
15
|
+
puts "sig version: #{Sig::VERSION}"
|
16
|
+
puts "rubype version: #{Rubype::VERSION}"
|
17
|
+
puts "typecheck version: #{Typecheck::VERSION}"
|
18
|
+
puts "contracts version: #{Contracts::VERSION}"
|
19
|
+
|
20
|
+
# - - -
|
21
|
+
|
22
|
+
class PureSum
|
23
|
+
def sum(x, y)
|
24
|
+
x + y
|
25
|
+
end
|
26
|
+
|
27
|
+
def mul(x, y)
|
28
|
+
x * y
|
29
|
+
end
|
30
|
+
end
|
31
|
+
pure_instance = PureSum.new
|
32
|
+
|
33
|
+
# - - -
|
34
|
+
|
35
|
+
class SigSum
|
36
|
+
sig [Numeric, Numeric], Numeric,
|
37
|
+
def sum(x, y)
|
38
|
+
x + y
|
39
|
+
end
|
40
|
+
|
41
|
+
sig [:to_i, :to_i], Numeric,
|
42
|
+
def mul(x, y)
|
43
|
+
x * y
|
44
|
+
end
|
45
|
+
end
|
46
|
+
sig_instance = SigSum.new
|
47
|
+
|
48
|
+
# - - -
|
49
|
+
|
50
|
+
class RubypeSum
|
51
|
+
def sum(x, y)
|
52
|
+
x + y
|
53
|
+
end
|
54
|
+
typesig :sum, [Numeric, Numeric] => Numeric
|
55
|
+
|
56
|
+
def mul(x, y)
|
57
|
+
x * y
|
58
|
+
end
|
59
|
+
typesig :mul, [:to_i, :to_i] => Numeric
|
60
|
+
end
|
61
|
+
rubype_instance = RubypeSum.new
|
62
|
+
|
63
|
+
# - - -
|
64
|
+
|
65
|
+
class TypecheckSum
|
66
|
+
extend Typecheck
|
67
|
+
|
68
|
+
typecheck 'Numeric, Numeric -> Numeric',
|
69
|
+
def sum(x, y)
|
70
|
+
x + y
|
71
|
+
end
|
72
|
+
|
73
|
+
typecheck '#to_i, #to_i -> Numeric',
|
74
|
+
def mul(x, y)
|
75
|
+
x * y
|
76
|
+
end
|
77
|
+
end
|
78
|
+
typecheck_instance = TypecheckSum.new
|
79
|
+
|
80
|
+
# - - -
|
81
|
+
|
82
|
+
class ContractsSum
|
83
|
+
include Contracts
|
84
|
+
|
85
|
+
Contract Num, Num => Num
|
86
|
+
def sum(x, y)
|
87
|
+
x + y
|
88
|
+
end
|
89
|
+
|
90
|
+
Contract RespondTo[:to_i], RespondTo[:to_i] => Num
|
91
|
+
def mul(x, y)
|
92
|
+
x * y
|
93
|
+
end
|
94
|
+
end
|
95
|
+
contracts_instance = ContractsSum.new
|
96
|
+
|
97
|
+
Benchmark.ips do |x|
|
98
|
+
x.report("pure"){ |times|
|
99
|
+
i = 0
|
100
|
+
while i < times
|
101
|
+
pure_instance.sum(1, 2)
|
102
|
+
pure_instance.mul(1, 2)
|
103
|
+
i += 1
|
104
|
+
end
|
105
|
+
}
|
106
|
+
|
107
|
+
x.report("sig"){ |times|
|
108
|
+
i = 0
|
109
|
+
while i < times
|
110
|
+
sig_instance.sum(1, 2)
|
111
|
+
sig_instance.mul(1, 2)
|
112
|
+
i += 1
|
113
|
+
end
|
114
|
+
}
|
115
|
+
|
116
|
+
x.report("rubype"){ |times|
|
117
|
+
i = 0
|
118
|
+
while i < times
|
119
|
+
rubype_instance.sum(1, 2)
|
120
|
+
rubype_instance.mul(1, 2)
|
121
|
+
i += 1
|
122
|
+
end
|
123
|
+
}
|
124
|
+
|
125
|
+
x.report("typecheck"){ |times|
|
126
|
+
i = 0
|
127
|
+
while i < times
|
128
|
+
typecheck_instance.sum(1, 2)
|
129
|
+
typecheck_instance.mul(1, 2)
|
130
|
+
i += 1
|
131
|
+
end
|
132
|
+
}
|
133
|
+
|
134
|
+
x.report("contracts"){ |times|
|
135
|
+
i = 0
|
136
|
+
while i < times
|
137
|
+
contracts_instance.sum(1, 2)
|
138
|
+
contracts_instance.mul(1, 2)
|
139
|
+
i += 1
|
140
|
+
end
|
141
|
+
}
|
142
|
+
|
143
|
+
x.compare!
|
144
|
+
end
|
145
|
+
|
data/lib/sig.rb
CHANGED
@@ -9,6 +9,8 @@ module Sig
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def self.define(object, expected_arguments, expected_result = nil, method_name)
|
12
|
+
no_argument_checks = expected_arguments.nil?
|
13
|
+
|
12
14
|
expected_arguments = Array(expected_arguments)
|
13
15
|
if expected_arguments.last.is_a?(Hash)
|
14
16
|
expected_keyword_arguments = expected_arguments.delete_at(-1)
|
@@ -20,14 +22,15 @@ module Sig
|
|
20
22
|
signature_checker = get_or_create_signature_checker(object)
|
21
23
|
signature_checker.send :define_method, method_name do |*arguments, **keyword_arguments|
|
22
24
|
if keyword_arguments.empty?
|
23
|
-
::Sig.check_arguments(expected_arguments, arguments)
|
25
|
+
::Sig.check_arguments(expected_arguments, arguments) unless no_argument_checks
|
24
26
|
result = super(*arguments)
|
25
27
|
else
|
26
28
|
::Sig.check_arguments_with_keywords(expected_arguments, arguments,
|
27
|
-
expected_keyword_arguments,
|
29
|
+
expected_keyword_arguments,
|
30
|
+
keyword_arguments) unless no_argument_checks
|
28
31
|
result = super(*arguments, **keyword_arguments)
|
29
32
|
end
|
30
|
-
::Sig.check_result(expected_result, result)
|
33
|
+
::Sig.check_result(expected_result, result) unless expected_result.nil?
|
31
34
|
|
32
35
|
result
|
33
36
|
end
|
data/lib/sig/version.rb
CHANGED
data/sig.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jan Lelis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Optional Type Assertions for Ruby methods.
|
14
14
|
email: mail@janlelis.de
|
@@ -16,23 +16,19 @@ executables: []
|
|
16
16
|
extensions: []
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
|
-
- ".CHANGELOG.md.swp"
|
20
|
-
- ".Rakefile.swp"
|
21
19
|
- ".gitignore"
|
22
|
-
- ".travis.yml"
|
23
20
|
- CHANGELOG.md
|
24
21
|
- Gemfile
|
25
22
|
- Gemfile.lock
|
26
23
|
- MIT-LICENSE.txt
|
27
24
|
- README.md
|
28
25
|
- Rakefile
|
26
|
+
- benchmark/compare.rb
|
29
27
|
- lib/sig.rb
|
30
|
-
- lib/sig/.kernel.rb.swp
|
31
28
|
- lib/sig/kernel.rb
|
32
29
|
- lib/sig/none.rb
|
33
30
|
- lib/sig/version.rb
|
34
31
|
- sig.gemspec
|
35
|
-
- spec/.sig_spec.rb.swp
|
36
32
|
- spec/sig_spec.rb
|
37
33
|
homepage: https://github.com/janlelis/sig
|
38
34
|
licenses:
|
@@ -44,20 +40,21 @@ require_paths:
|
|
44
40
|
- lib
|
45
41
|
required_ruby_version: !ruby/object:Gem::Requirement
|
46
42
|
requirements:
|
47
|
-
- - "
|
43
|
+
- - ">="
|
48
44
|
- !ruby/object:Gem::Version
|
49
45
|
version: '2.1'
|
46
|
+
- - "<"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '4.0'
|
50
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
50
|
requirements:
|
52
51
|
- - ">="
|
53
52
|
- !ruby/object:Gem::Version
|
54
53
|
version: '0'
|
55
54
|
requirements: []
|
56
|
-
|
57
|
-
rubygems_version: 2.4.6
|
55
|
+
rubygems_version: 3.1.2
|
58
56
|
signing_key:
|
59
57
|
specification_version: 4
|
60
58
|
summary: Optional Type Assertions for Ruby.
|
61
59
|
test_files:
|
62
|
-
- spec/.sig_spec.rb.swp
|
63
60
|
- spec/sig_spec.rb
|
data/.CHANGELOG.md.swp
DELETED
Binary file
|
data/.Rakefile.swp
DELETED
Binary file
|
data/.travis.yml
DELETED
data/lib/sig/.kernel.rb.swp
DELETED
Binary file
|
data/spec/.sig_spec.rb.swp
DELETED
Binary file
|