Skip to content

Commit 6cb7b83

Browse files
committed
Merge pull request rspec#1870 from rspec/remove_set
Remove Set
2 parents a1c5f0b + 3a3c931 commit 6cb7b83

File tree

10 files changed

+114
-10
lines changed

10 files changed

+114
-10
lines changed

benchmarks/keys_each_vs_each_key.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'benchmark/ips'
2+
3+
small_hash = { :key => true, :more_key => true, :other_key => true }
4+
large_hash = (1...100).inject({}) { |hash, key| hash["key_#{key}"] = true; hash }
5+
6+
Benchmark.ips do |x|
7+
x.report('keys.each with small hash') do
8+
small_hash.keys.each { |value| value == true }
9+
end
10+
11+
x.report('each_key with small hash') do
12+
small_hash.each_key { |value| value == true }
13+
end
14+
15+
x.report('keys.each with large hash') do
16+
large_hash.keys.each { |value| value == true }
17+
end
18+
19+
x.report('each_key with large hash') do
20+
large_hash.each_key { |value| value == true }
21+
end
22+
end
23+
24+
__END__
25+
26+
Calculating -------------------------------------
27+
keys.each with small hash
28+
105.581k i/100ms
29+
each_key with small hash
30+
112.045k i/100ms
31+
keys.each with large hash
32+
7.625k i/100ms
33+
each_key with large hash
34+
6.959k i/100ms
35+
-------------------------------------------------
36+
keys.each with small hash
37+
2.953M (± 3.8%) i/s - 14.781M
38+
each_key with small hash
39+
2.917M (± 4.0%) i/s - 14.678M
40+
keys.each with large hash
41+
79.349k (± 2.5%) i/s - 396.500k
42+
each_key with large hash
43+
72.080k (± 2.1%) i/s - 361.868k

lib/rspec/core.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
version
1212
warnings
1313

14+
set
1415
flat_map
1516
filter_manager
1617
dsl

lib/rspec/core/configuration_options.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'erb'
22
require 'shellwords'
3-
require 'set'
43

54
module RSpec
65
module Core
@@ -53,12 +52,12 @@ def organize_options
5352
end
5453
end
5554

56-
UNFORCED_OPTIONS = [
55+
UNFORCED_OPTIONS = Set.new([
5756
:requires, :profile, :drb, :libs, :files_or_directories_to_run,
5857
:full_description, :full_backtrace, :tty
59-
].to_set
58+
])
6059

61-
UNPROCESSABLE_OPTIONS = [:formatters].to_set
60+
UNPROCESSABLE_OPTIONS = Set.new([:formatters])
6261

6362
def force?(key)
6463
!UNFORCED_OPTIONS.include?(key)

lib/rspec/core/formatters.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ def built_in_formatter(key)
196196
end
197197

198198
def notifications_for(formatter_class)
199-
formatter_class.ancestors.inject(Set.new) do |notifications, klass|
200-
notifications + Loader.formatters.fetch(klass) { Set.new }
199+
formatter_class.ancestors.inject(::RSpec::Core::Set.new) do |notifications, klass|
200+
notifications.merge Loader.formatters.fetch(klass) { ::RSpec::Core::Set.new }
201201
end
202202
end
203203

lib/rspec/core/formatters/deprecation_formatter.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
RSpec::Support.require_rspec_core "formatters/helpers"
2-
require 'set'
32

43
module RSpec
54
module Core

lib/rspec/core/reporter.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'set'
21
module RSpec::Core
32
# A reporter will send notifications to listeners, usually formatters for the
43
# spec suite run.

lib/rspec/core/set.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module RSpec
2+
module Core
3+
# @private
4+
#
5+
# We use this to replace `::Set` so we can have the advantage of
6+
# constant time key lookups for unique arrays but without the
7+
# potential to pollute a developers environment with an extra
8+
# piece of the stdlib. This helps to prevent false positive
9+
# builds.
10+
#
11+
class Set
12+
include Enumerable
13+
14+
def initialize(array=[])
15+
@values = {}
16+
merge(array)
17+
end
18+
19+
def <<(key)
20+
@values[key] = true
21+
self
22+
end
23+
24+
def each(&block)
25+
@values.keys.each(&block)
26+
self
27+
end
28+
29+
def include?(key)
30+
@values.key?(key)
31+
end
32+
33+
def merge(values)
34+
values.each do |key|
35+
@values[key] = true
36+
end
37+
self
38+
end
39+
end
40+
end
41+
end

spec/rspec/core/formatters/base_text_formatter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
end
5353

5454
def output_from_running(example_group)
55-
allow(RSpec.configuration).to receive(:loaded_spec_files) { [File.expand_path(__FILE__)].to_set }
55+
allow(RSpec.configuration).to receive(:loaded_spec_files) { RSpec::Core::Set.new([File.expand_path(__FILE__)]) }
5656
example_group.run(reporter)
5757
examples = example_group.examples
5858
send_notification :dump_summary, summary_notification(1, examples, examples, [], 0)

spec/rspec/core/set_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
RSpec.describe 'RSpec::Core::Set' do
2+
3+
let(:set) { RSpec::Core::Set.new([1, 2, 3]) }
4+
5+
it 'takes an array of values' do
6+
expect(set).to include(1, 2, 3)
7+
end
8+
9+
it 'can be appended to' do
10+
set << 4
11+
expect(set).to include 4
12+
end
13+
14+
it 'can have more values merged in' do
15+
set.merge([4, 5]).merge([6])
16+
expect(set).to include(4, 5, 6)
17+
end
18+
19+
it 'is enumerable' do
20+
expect(set).to be_an Enumerable
21+
expect { |p| set.each(&p) }.to yield_successive_args(1, 2, 3)
22+
end
23+
end

spec/rspec/core_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
fake_libs = File.expand_path('../../support/fake_libs', __FILE__)
55
allowed_loaded_features = [
66
/optparse\.rb/, # Used by OptionParser.
7-
/set\.rb/, # used in a few places but being removed in #1870.
87
/rbconfig\.rb/, # loaded by rspec-support for OS detection.
98
/shellwords\.rb/, # used by ConfigurationOptions and RakeTask.
109
/stringio/, # Used by BaseFormatter.

0 commit comments

Comments
 (0)