Skip to content

Commit cd56e56

Browse files
committed
Bring in SimpleSet code for small speed improvement
1 parent b377e1d commit cd56e56

File tree

6 files changed

+98
-6
lines changed

6 files changed

+98
-6
lines changed

lib/callsite.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ def inheritable?
100100
#
101101
def setup_columns
102102
if inheritable?
103-
Set.new([primary_key, inheritance_column])
103+
SimpleSet.new([primary_key, inheritance_column])
104104
else
105-
primary_key.blank? ? Set.new : Set.new([primary_key])
105+
primary_key.blank? ? SimpleSet.new : SimpleSet.new([primary_key])
106106
end
107107
end
108108

lib/optimizations/associations/association_set.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AssociationSet
1818
Mtx = Mutex.new
1919

2020
def initialize
21-
@associations = Set.new
21+
@associations = SimpleSet.new
2222
@as_data_id = :"association_data_#{object_id}"
2323
end
2424

lib/scrooge.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require 'set'
44
require 'callsite'
5+
require 'simple_set'
56
require 'optimizations/columns/attributes_proxy'
67
require 'optimizations/columns/macro'
78
require 'optimizations/associations/macro'

lib/simple_set.rb

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
module Scrooge
2+
3+
class SimpleSet < Hash
4+
5+
class << self
6+
##
7+
# Creates a new set containing the given objects
8+
#
9+
# @return [SimpleSet] The new set
10+
#
11+
# @api public
12+
def [](*ary)
13+
new(ary)
14+
end
15+
end
16+
17+
##
18+
# Create a new SimpleSet containing the unique members of _arr_
19+
#
20+
# @param [Array] arr Initial set values.
21+
#
22+
# @return [Array] The array the Set was initialized with
23+
#
24+
# @api public
25+
def initialize(arr = [])
26+
Array(arr).each {|x| self[x] = true}
27+
end
28+
29+
##
30+
# Add a value to the set, and return it
31+
#
32+
# @param [Object] value Value to add to set.
33+
#
34+
# @return [SimpleSet] Receiver
35+
#
36+
# @api public
37+
def <<(value)
38+
self[value] = true
39+
self
40+
end
41+
42+
##
43+
# Merge _arr_ with receiver, producing the union of receiver & _arr_
44+
#
45+
# s = Extlib::SimpleSet.new([:a, :b, :c])
46+
# s.merge([:c, :d, :e, f]) #=> #<SimpleSet: {:e, :c, :f, :a, :d, :b}>
47+
#
48+
# @param [Array] arr Values to merge with set.
49+
#
50+
# @return [SimpleSet] The set after the Array was merged in.
51+
#
52+
# @api public
53+
def merge(arr)
54+
super(arr.inject({}) {|s,x| s[x] = true; s })
55+
end
56+
alias_method :|, :merge
57+
58+
##
59+
# Invokes block once for each item in the set. Creates an array
60+
# containing the values returned by the block.
61+
#
62+
# s = Extlib::SimpleSet.new([1, 2, 3])
63+
# s.collect {|s| s + 1} #=> [2, 3, 4]
64+
#
65+
# @return [Array] The values returned by the block
66+
#
67+
# @api public
68+
def collect(&block)
69+
keys.collect(&block)
70+
end
71+
alias_method :map, :collect
72+
73+
##
74+
# Get a human readable version of the set.
75+
#
76+
# s = SimpleSet.new([:a, :b, :c])
77+
# s.inspect #=> "#<SimpleSet: {:c, :a, :b}>"
78+
#
79+
# @return [String] A human readable version of the set.
80+
#
81+
# @api public
82+
def inspect
83+
"#<SimpleSet: {#{keys.map {|x| x.inspect}.join(", ")}}>"
84+
end
85+
86+
# def to_a
87+
alias_method :to_a, :keys
88+
89+
end # SimpleSet
90+
91+
end

test/callsite_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ def setup
1010

1111
test "should initialize with a default set of columns" do
1212
assert @callsite.columns.empty?
13-
assert_equal Scrooge::Callsite.new( MysqlUser, 123456 ).columns, Set["User"]
13+
assert_equal Scrooge::Callsite.new( MysqlUser, 123456 ).columns, SimpleSet["User"]
1414
Scrooge::Callsite.any_instance.stubs(:inheritable?).returns(true)
1515
Scrooge::Callsite.any_instance.stubs(:inheritance_column).returns("inheritance")
16-
assert_equal Scrooge::Callsite.new( MysqlUser, 123456 ).columns, Set["User","inheritance"]
16+
assert_equal Scrooge::Callsite.new( MysqlUser, 123456 ).columns, SimpleSet["User","inheritance"]
1717
end
1818

1919
test "should be inspectable" do

test/scrooge_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class ScroogeTest < ActiveRecord::TestCase
5151
end
5252

5353
test "should be able to generate a SQL select snippet from a given set" do
54-
assert_equal MysqlUser.scrooge_select_sql( Set['Password','User','Host'] ), "`user`.User,`user`.Password,`user`.Host"
54+
assert_equal MysqlUser.scrooge_select_sql( SimpleSet['Password','User','Host'] ), "`user`.User,`user`.Password,`user`.Host"
5555
end
5656

5757
test "should be able to augment an existing callsite when attributes is referenced that we haven't seen yet" do

0 commit comments

Comments
 (0)