fib_rust 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: 4f8f68fb59b27416b76971650329df902aebb36079c720391b4293a0125bf813
4
+ data.tar.gz: 14ecab3caf75a52dd12a2eba734b729d27c169461e8227a951e731b351d4d63d
5
+ SHA512:
6
+ metadata.gz: 2bbbac9511674704769b77ab3f62d5612a955a3a3362265e0121b879c424b9fb4b9b1f4d2cb4d73042682dd5f703826ff39e7ff7a55fe7cdc239e9a904b60775
7
+ data.tar.gz: 5b30c639c9ba323e1a3e86690eddb4d3cc32802f8e55f2e8b2157c4a0aa179b03f875cc6736830b6ed0ed35757f0b493db051cabbcf00c83be3f2312d2c8b6f6
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+
14
+ #Added by cargo
15
+ /rust/target
16
+ /rust/Cargo.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fib_rust.gemspec
4
+ gemspec
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fib_rust (0.1.0)
5
+ ffi
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ benchmark-ips (2.8.2)
11
+ diff-lcs (1.3)
12
+ ffi (1.13.1)
13
+ rake (10.5.0)
14
+ rspec (3.9.0)
15
+ rspec-core (~> 3.9.0)
16
+ rspec-expectations (~> 3.9.0)
17
+ rspec-mocks (~> 3.9.0)
18
+ rspec-core (3.9.2)
19
+ rspec-support (~> 3.9.3)
20
+ rspec-expectations (3.9.2)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-mocks (3.9.1)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.9.0)
26
+ rspec-support (3.9.3)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ benchmark-ips
33
+ bundler (~> 2.0)
34
+ fib_rust!
35
+ rake (~> 10.0)
36
+ rspec (~> 3.0)
37
+
38
+ BUNDLED WITH
39
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Dmitry Gruzd
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,59 @@
1
+ # FibRust
2
+
3
+ This is rust-backed version of fibonacci function.
4
+
5
+ ## Performance comparison
6
+
7
+ ```
8
+ ❯ rake benchmark
9
+ ❯ rake benchmark
10
+ Calculating -------------------------------------
11
+ ruby 542.633k (± 0.4%) i/s - 2.721M in 5.014871s
12
+ ruby_recursive 4.761 (± 0.0%) i/s - 24.000 in 5.041325s
13
+ ruby_memoization 195.055k (± 1.2%) i/s - 989.456k in 5.073389s
14
+ fib_rust 8.051M (± 0.5%) i/s - 40.276M in 5.002752s
15
+
16
+ Comparison:
17
+ fib_rust: 8051043.9 i/s
18
+ ruby: 542633.4 i/s - 14.84x (± 0.00) slower
19
+ ruby_memoization: 195055.3 i/s - 41.28x (± 0.00) slower
20
+ ruby_recursive: 4.8 i/s - 1691152.45x (± 0.00) slower
21
+ ```
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'fib_rust'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install fib_rust
38
+
39
+ ## Usage
40
+
41
+ TODO: Write usage instructions here
42
+ ```ruby
43
+ 2.6.6 :001 > FibRust[40]
44
+ => 102334155
45
+ ```
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fib_rust.
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,62 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ task :rust_build do
9
+ require 'ffi'
10
+ `cd rust && cargo build --release`
11
+ `mv -f ./rust/target/release/libfib.#{FFI::Platform::LIBSUFFIX} ./lib/fib_rust/`
12
+ end
13
+
14
+ task :rust_test do
15
+ `cd rust && cargo test`
16
+ end
17
+
18
+ task :build => :rust_build
19
+ task :spec => :rust_build
20
+
21
+ task :test => %w[rust_test spec]
22
+
23
+ task :benchmark do
24
+ require 'fib_rust'
25
+ require 'benchmark/ips'
26
+
27
+ def ruby_fib(iterations)
28
+ first_num, second_num = [0, 1]
29
+
30
+ iterations.times do
31
+ first_num, second_num = second_num, first_num + second_num
32
+ end
33
+
34
+ first_num
35
+ end
36
+
37
+ def ruby_recursive(iterations)
38
+ return iterations if (0..1).include? iterations
39
+
40
+ (ruby_recursive(iterations - 1) + ruby_recursive(iterations - 2))
41
+ end
42
+
43
+ def ruby_memoization(iterations)
44
+ fib = Hash.new do |numbers, index|
45
+ numbers[index] = fib[index - 2] + fib[index - 1]
46
+ end.update(0 => 0, 1 => 1)
47
+
48
+ fib[iterations]
49
+ end
50
+
51
+ ITERATIONS = 30
52
+
53
+ Benchmark.ips do |x|
54
+ x.report('ruby') { ruby_fib(ITERATIONS) }
55
+ x.report('ruby_recursive') { ruby_recursive(ITERATIONS) }
56
+ x.report('ruby_memoization') { ruby_memoization(ITERATIONS) }
57
+ x.report('fib_rust') { FibRust[ITERATIONS] }
58
+
59
+ x.compare!
60
+ end
61
+ end
62
+ task :bench => :benchmark
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fib_rust"
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
@@ -0,0 +1,45 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fib_rust/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fib_rust"
8
+ spec.version = FibRust::VERSION
9
+ spec.authors = ["Dmitry Gruzd"]
10
+ spec.email = ["dgruzd@gitlab.com"]
11
+
12
+ spec.summary = %q{This is rust-backed fibonacci number generator}
13
+ spec.description = %q{Get the fibonacci number}
14
+ spec.homepage = "https://gitlab.com/dgruzd/fib_rust"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = "https://gitlab.com/dgruzd/fib_rust"
24
+ spec.metadata["changelog_uri"] = "https://gitlab.com/dgruzd/fib_rust"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "exe"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_dependency "ffi"
40
+
41
+ spec.add_development_dependency "bundler", "~> 2.0"
42
+ spec.add_development_dependency "rake", "~> 10.0"
43
+ spec.add_development_dependency "rspec", "~> 3.0"
44
+ spec.add_development_dependency "benchmark-ips"
45
+ end
@@ -0,0 +1,8 @@
1
+ require 'fib_rust/version'
2
+ require 'fib_rust/ffi'
3
+
4
+ module FibRust
5
+ def self.[](n)
6
+ FibRust.fib(n)
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'ffi'
2
+
3
+ module FibRust
4
+ extend FFI::Library
5
+ lib_name = "libfib.#{::FFI::Platform::LIBSUFFIX}"
6
+ ffi_lib File.expand_path(lib_name, __dir__)
7
+ attach_function :fib, [:uint], :uint
8
+ end
Binary file
Binary file
@@ -0,0 +1,3 @@
1
+ module FibRust
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ [package]
2
+ name = "fib"
3
+ version = "0.1.0"
4
+ authors = ["Dmitry Gruzd <dgruzd@gitlab.com>"]
5
+ edition = "2018"
6
+
7
+ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8
+
9
+ [lib]
10
+ crate-type = ["cdylib"]
11
+
12
+ [dependencies]
@@ -0,0 +1,18 @@
1
+ #[no_mangle]
2
+ pub extern "C" fn fib(n: u32) -> u32 {
3
+ let mut x = (0, 1);
4
+ for _ in 0..n {
5
+ x = (x.1, x.0 + x.1)
6
+ }
7
+ x.0
8
+ }
9
+
10
+ #[cfg(test)]
11
+ mod tests {
12
+ use super::*;
13
+
14
+ #[test]
15
+ fn it_works() {
16
+ assert_eq!(fib(5), 5);
17
+ }
18
+ }
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fib_rust
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Gruzd
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-06-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: benchmark-ips
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: Get the fibonacci number
84
+ email:
85
+ - dgruzd@gitlab.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - fib_rust.gemspec
100
+ - lib/fib_rust.rb
101
+ - lib/fib_rust/ffi.rb
102
+ - lib/fib_rust/libfib.dylib
103
+ - lib/fib_rust/libfib.so
104
+ - lib/fib_rust/version.rb
105
+ - rust/Cargo.toml
106
+ - rust/src/lib.rs
107
+ homepage: https://gitlab.com/dgruzd/fib_rust
108
+ licenses:
109
+ - MIT
110
+ metadata:
111
+ allowed_push_host: https://rubygems.org
112
+ homepage_uri: https://gitlab.com/dgruzd/fib_rust
113
+ source_code_uri: https://gitlab.com/dgruzd/fib_rust
114
+ changelog_uri: https://gitlab.com/dgruzd/fib_rust
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.0.8
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: This is rust-backed fibonacci number generator
134
+ test_files: []