This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 753
Remove Set #1870
Merged
Merged
Remove Set #1870
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'benchmark/ips' | ||
|
||
small_hash = { :key => true, :more_key => true, :other_key => true } | ||
large_hash = (1...100).inject({}) { |hash, key| hash["key_#{key}"] = true; hash } | ||
|
||
Benchmark.ips do |x| | ||
x.report('keys.each with small hash') do | ||
small_hash.keys.each { |value| value == true } | ||
end | ||
|
||
x.report('each_key with small hash') do | ||
small_hash.each_key { |value| value == true } | ||
end | ||
|
||
x.report('keys.each with large hash') do | ||
large_hash.keys.each { |value| value == true } | ||
end | ||
|
||
x.report('each_key with large hash') do | ||
large_hash.each_key { |value| value == true } | ||
end | ||
end | ||
|
||
__END__ | ||
|
||
Calculating ------------------------------------- | ||
keys.each with small hash | ||
105.581k i/100ms | ||
each_key with small hash | ||
112.045k i/100ms | ||
keys.each with large hash | ||
7.625k i/100ms | ||
each_key with large hash | ||
6.959k i/100ms | ||
------------------------------------------------- | ||
keys.each with small hash | ||
2.953M (± 3.8%) i/s - 14.781M | ||
each_key with small hash | ||
2.917M (± 4.0%) i/s - 14.678M | ||
keys.each with large hash | ||
79.349k (± 2.5%) i/s - 396.500k | ||
each_key with large hash | ||
72.080k (± 2.1%) i/s - 361.868k |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
version | ||
warnings | ||
|
||
set | ||
flat_map | ||
filter_manager | ||
dsl | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
RSpec::Support.require_rspec_core "formatters/helpers" | ||
require 'set' | ||
|
||
module RSpec | ||
module Core | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module RSpec | ||
module Core | ||
# @private | ||
# | ||
# We use this to replace `::Set` so we can have the advantage of | ||
# constant time key lookups for unique arrays but without the | ||
# potential to pollute a developers environment with an extra | ||
# piece of the stdlib. This helps to prevent false positive | ||
# builds. | ||
# | ||
class Set | ||
include Enumerable | ||
|
||
def initialize(array=[]) | ||
@values = {} | ||
merge(array) | ||
end | ||
|
||
def <<(key) | ||
@values[key] = true | ||
self | ||
end | ||
|
||
def each(&block) | ||
@values.keys.each(&block) | ||
self | ||
end | ||
|
||
def include?(key) | ||
@values.key?(key) | ||
end | ||
|
||
def merge(values) | ||
values.each do |key| | ||
@values[key] = true | ||
end | ||
self | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
RSpec.describe 'RSpec::Core::Set' do | ||
|
||
let(:set) { RSpec::Core::Set.new([1, 2, 3]) } | ||
|
||
it 'takes an array of values' do | ||
expect(set).to include(1, 2, 3) | ||
end | ||
|
||
it 'can be appended to' do | ||
set << 4 | ||
expect(set).to include 4 | ||
end | ||
|
||
it 'can have more values merged in' do | ||
set.merge([4, 5]).merge([6]) | ||
expect(set).to include(4, 5, 6) | ||
end | ||
|
||
it 'is enumerable' do | ||
expect(set).to be_an Enumerable | ||
expect { |p| set.each(&p) }.to yield_successive_args(1, 2, 3) | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you get around to benchmarking
Hash#keys.each
vsHash#each_key
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have just now,
#keys.each
is faster but not by a lot.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Weird. I wonder why
each_key
even exists then...given that all it does is yield each key I'd expect it to be optimized for that case and be faster than the extra hop ofkeys.each
. Can you commit your benchmark for future reference?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well it's law of demeter approved ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was already in the process of comitting