native_ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 60f2b8067ce19f7f2bdfb9bad44dc3577284274b4216badb79a619242c256813
4
+ data.tar.gz: 784237bf405374032c60ab792a28a6d6ac9f4d09834b06894cb38c0919943730
5
+ SHA512:
6
+ metadata.gz: 7d01a030d69b3df024e1742b9fbd4e9f33c1e29369c55a5bd02161043322dde7e676c29161e2a5627bb1ebf8ae9573043920d9b5eb1901433499c1ffdc8c8060
7
+ data.tar.gz: 23c1bdcce43e7ae85bbadc646ba2d75bd89d9f5ba6ccc02d842a6c12804dc13a310165b42606522bd8ea7f2f251d7ca8c60c1820539ccb6ef3d4c1a1116dac3d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format progress
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ inherit_gem:
2
+ rubocop-rails_config:
3
+ - config/rails.yml
4
+
5
+ Style/ClassAndModuleChildren:
6
+ EnforcedStyle: nested
7
+
8
+ Lint/Debugger:
9
+ Enabled: true
10
+
11
+ Style/StringLiterals:
12
+ Enabled: true
13
+ EnforcedStyle: single_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-08-10
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 sebi
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.
data/README.md ADDED
@@ -0,0 +1,108 @@
1
+ # NativeRuby 🚀
2
+
3
+ NativeRuby is a standard library for Ruby implemented in Ruby, designed to take advantage of the YJIT compiler for improved performance. It provides optimized, native Ruby implementations of various core methods and iterators while maintaining the familiar Ruby API. 🔥
4
+
5
+ ## Important Note on Performance ⚠️
6
+
7
+ **The performance improvements provided by NativeRuby are primarily seen when YJIT is enabled.** Without YJIT, you may not notice significant performance gains. For the best results, always use NativeRuby with YJIT enabled.
8
+
9
+ ## Requirements 📋
10
+
11
+ - Ruby version: >= 3.0.0
12
+ - YJIT enabled for optimal performance
13
+
14
+ ## Installation 💎
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ ```ruby
19
+ gem 'native_ruby'
20
+ ```
21
+
22
+ And then execute:
23
+
24
+ $ bundle install
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install native_ruby
29
+
30
+ ## Usage 🛠️
31
+
32
+ To use NativeRuby in your project, you first need to require it:
33
+
34
+ ```ruby
35
+ require 'native_ruby'
36
+ ```
37
+
38
+ Then, you can configure which optimized methods you want to use:
39
+
40
+ ```ruby
41
+ NativeRuby.config do |c|
42
+ # Load all immutable iterators
43
+ c.load_all_immutable_iterators!
44
+
45
+ # Load all mutable iterators
46
+ c.load_all_mutable_iterators!
47
+
48
+ # Load all optimized methods for a specific class
49
+ c.require_all_for_class!(Array)
50
+
51
+ # Load a specific method for a class
52
+ c.load(:class, { class: Array, method: :bsearch })
53
+
54
+ # Load a specific iterator (mutable or immutable)
55
+ c.load(:iterators, { class: Array, method: :each, mutable: false })
56
+ end
57
+ ```
58
+
59
+ ### Configuration Options ⚙️
60
+
61
+ - `load_all_immutable_iterators!`: Loads all immutable iterator optimizations.
62
+ - `load_all_mutable_iterators!`: Loads all mutable iterator optimizations.
63
+ - `require_all_for_class!(klass)`: Loads all optimized methods for a given class.
64
+ - `load(type, options)`: Loads a specific optimized method or iterator.
65
+ - For class methods: `type: :class, class: ClassName, method: :method_name`
66
+ - For iterators: `type: :iterators, class: ClassName, method: :method_name, mutable: boolean`
67
+
68
+ ## YJIT and Performance 🚀
69
+
70
+ NativeRuby is specifically designed to work with YJIT (Yet Another Just-In-Time Compiler for Ruby). The performance improvements are primarily realized when YJIT is enabled.
71
+
72
+ To use NativeRuby with YJIT and see the performance benefits:
73
+
74
+ 1. Ensure you're using Ruby 3.1 or later.
75
+ 2. Enable YJIT when running your Ruby script:
76
+
77
+ ```
78
+ ruby --yjit your_script.rb
79
+ ```
80
+
81
+ Without YJIT enabled, you may not see significant performance improvements. Always use NativeRuby with YJIT for optimal results.
82
+
83
+ ## Development 🔧
84
+
85
+ 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.
86
+
87
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
88
+
89
+ ## Contributing 🤝
90
+
91
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sebyx07/native_ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/sebyx07/native_ruby/blob/master/CODE_OF_CONDUCT.md).
92
+
93
+ ## License 📄
94
+
95
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
96
+
97
+ ## Code of Conduct 🤓
98
+
99
+ Everyone interacting in the NativeRuby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/sebyx07/native_ruby/blob/master/CODE_OF_CONDUCT.md).
100
+
101
+ ## Author 👨‍💻
102
+
103
+ - **sebi** - [GitHub](https://github.com/sebyx07)
104
+
105
+ ## Links 🔗
106
+
107
+ - [Homepage](https://github.com/sebyx07/native_ruby)
108
+ - [Source Code](https://github.com/sebyx07/native_ruby)
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NativeRuby
4
+ class Configuration
5
+ def load_all_immutable_iterators!
6
+ Dir.glob(File.join(File.dirname(__FILE__), 'iterators', 'immutable', '**', '*.rb')).each do |file|
7
+ require file
8
+ end
9
+ end
10
+
11
+ def load_all_mutable_iterators!
12
+ Dir.glob(File.join(File.dirname(__FILE__), 'iterators', 'mutable', '**', '*.rb')).each do |file|
13
+ require file
14
+ end
15
+ end
16
+
17
+ def require_all_for_class!(klass)
18
+ Dir.glob(File.join(File.dirname(__FILE__), klass.name.downcase, '**', '*.rb')).each do |file|
19
+ require file
20
+ end
21
+ end
22
+
23
+ def load(type, options = {})
24
+ klass = options[:class]
25
+ method = options[:method]
26
+ mutable = options[:mutable]
27
+ mutable_path = mutable ? 'mutable' : 'immutable'
28
+
29
+ if type.to_s == 'class'
30
+ path = File.join(File.dirname(__FILE__), klass.name.downcase, "#{method}.rb")
31
+ raise "Cannot load #{klass}.#{method}" unless File.exist?(path)
32
+
33
+ return require path
34
+ end
35
+
36
+ path = File.join(File.dirname(__FILE__), type.to_s, mutable_path, "#{klass.name.downcase}", "#{method}.rb")
37
+ raise "Cannot load #{mutable_path} #{klass}.#{method}" unless File.exist?(path)
38
+
39
+ require path
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Array immutable each method
4
+ class Array
5
+ # Iterates the given block for each element with index.
6
+ #
7
+ # This implementation is considered immutable because it does not modify
8
+ # the array's size or structure during iteration. The array's length is
9
+ # calculated once at the beginning and remains constant throughout the iteration.
10
+ #
11
+ # If no block is given, returns an Enumerator object.
12
+ #
13
+ # @yield [Object] Passes each element of the array to the block.
14
+ # @return [Array] Returns self.
15
+ # @return [Enumerator] If no block is given.
16
+ def each
17
+ return to_enum(:each) { self.length } unless block_given?
18
+
19
+ i = 0
20
+ length = self.length
21
+ while i < length
22
+ yield self[i]
23
+ i = i.succ
24
+ end
25
+
26
+ self
27
+ end
28
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+ # Creates a new array containing the values returned by the block.
5
+ #
6
+ # This implementation is considered immutable for two reasons:
7
+ # 1. It does not modify the original array's size or structure.
8
+ # 2. It creates and returns a new array with the mapped values.
9
+ #
10
+ # The original array's length is calculated once at the beginning
11
+ # and remains constant throughout the iteration.
12
+ #
13
+ # If no block is given, returns an Enumerator object.
14
+ #
15
+ # @yield [Object] Passes each element of the array to the block.
16
+ # @return [Array] A new array with the results of running the block once for every element.
17
+ # @return [Enumerator] If no block is given.
18
+ def map
19
+ return to_enum(:map) { self.length } unless block_given?
20
+
21
+ i = 0
22
+ result = []
23
+ length = self.length
24
+ while i < length
25
+ result << yield(self[i])
26
+ i = i.succ
27
+ end
28
+ result
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Array
4
+ class Array
5
+ # Iterates the given block for each element with index.
6
+ #
7
+ # This implementation is considered mutable because:
8
+ # 1. It recalculates self.length on each iteration, allowing for potential
9
+ # changes to the array's size during iteration.
10
+ # 2. If the array is modified during iteration (e.g., by the yielded block),
11
+ # the method will operate on the modified array.
12
+ #
13
+ # If no block is given, returns an Enumerator object.
14
+ #
15
+ # @yield [Object] Passes each element of the array to the block.
16
+ # @return [Array] Returns self.
17
+ # @return [Enumerator] If no block is given.
18
+ def each
19
+ return to_enum(:each) { self.length } unless block_given?
20
+
21
+ i = 0
22
+ while i < self.length # Note: self.length is evaluated on each iteration
23
+ yield self[i]
24
+ i = i.succ
25
+ end
26
+ self
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Array
4
+ # Creates a new array containing the values returned by the block.
5
+ #
6
+ # This implementation is considered mutable because:
7
+ # 1. It recalculates self.length on each iteration, allowing for potential
8
+ # changes to the array's size during iteration.
9
+ # 2. If the original array is modified during iteration (e.g., by the yielded block),
10
+ # the method will operate on the modified array.
11
+ #
12
+ # Note that while the original array can be mutated during iteration,
13
+ # the method still returns a new array with the mapped values.
14
+ #
15
+ # If no block is given, returns an Enumerator object.
16
+ #
17
+ # @yield [Object] Passes each element of the array to the block.
18
+ # @return [Array] A new array with the results of running the block once for every element.
19
+ # @return [Enumerator] If no block is given.
20
+ def map
21
+ return to_enum(:map) { self.length } unless block_given?
22
+
23
+ i = 0
24
+ result = []
25
+ while i < self.length # Note: self.length is evaluated on each iteration
26
+ result << yield(self[i])
27
+ i = i.succ
28
+ end
29
+ result
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NativeRuby
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'native_ruby/version'
4
+ require_relative 'native_ruby/configuration'
5
+
6
+ module NativeRuby
7
+ class Error < StandardError; end
8
+ def self.config
9
+ @config ||= Configuration.new
10
+ yield @config if block_given?
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ module NativeRuby
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: native_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sebi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Standard library for Ruby in ruby, taking the advantage of YJIT compiler
14
+ email:
15
+ - gore.sebyx@yahoo.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/native_ruby.rb
28
+ - lib/native_ruby/configuration.rb
29
+ - lib/native_ruby/iterators/immutable/array/each.rb
30
+ - lib/native_ruby/iterators/immutable/array/map.rb
31
+ - lib/native_ruby/iterators/mutable/array/each.rb
32
+ - lib/native_ruby/iterators/mutable/array/map.rb
33
+ - lib/native_ruby/version.rb
34
+ - sig/native_ruby.rbs
35
+ homepage: https://github.com/sebyx07/native_ruby
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ allowed_push_host: https://rubygems.org
40
+ homepage_uri: https://github.com/sebyx07/native_ruby
41
+ source_code_uri: https://github.com/sebyx07/native_ruby
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.0.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.5.11
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Standard library for Ruby in ruby
61
+ test_files: []