Permalink
Browse files

performance improvements around accessing attributes

  • Loading branch information...
1 parent b8f15da commit cf79a08a1e448528c47ffc7f2615dcdab34e73a5 @sdsykes sdsykes committed Mar 30, 2009
Showing with 16 additions and 7 deletions.
  1. +1 −1 lib/callsite.rb
  2. +3 −3 lib/optimizations/columns/attributes_proxy.rb
  3. +12 −3 lib/optimizations/columns/macro.rb
View
@@ -6,7 +6,7 @@ class Callsite
#
Mtx = Mutex.new # mutex should perhaps be per-instance at the expense of a little memory
-
+
attr_accessor :klass,
:signature,
:columns,
@@ -52,7 +52,7 @@ def keys
# Let #has_key? consider defined columns
#
def has_key?(attr_name)
- keys.include?(attr_name.to_s)
+ @klass.column_names_hashed.has_key?(attr_name.to_s)
end
alias_method :include?, :has_key?
@@ -111,7 +111,7 @@ def update(hash)
def fetch_remaining
unless @fully_fetched
- columns_to_fetch = @klass.column_names - @scrooge_columns.to_a
+ columns_to_fetch = keys - @scrooge_columns.to_a
unless columns_to_fetch.empty?
fetch_remaining!( columns_to_fetch )
end
@@ -127,7 +127,7 @@ def fetch_remaining!( columns_to_fetch )
end
def interesting_for_scrooge?( attr_s )
- has_key?(attr_s) && !@scrooge_columns.include?(attr_s)
+ @klass.column_names_hashed.has_key?(attr_s) && !@scrooge_columns.include?(attr_s)
end
def augment_callsite!( attr_s )
@@ -66,6 +66,12 @@ def scope_with_scrooge?( sql )
column_names.include?(self.primary_key.to_s) &&
sql !~ ScroogeRegexJoin
end
+
+ # far more efficient to search for a hash key than through an array
+ #
+ def column_names_hashed
+ @column_names_hashed ||= column_names.inject({}) {|memo, cn| memo[cn] = true; memo}
+ end
private
@@ -77,12 +83,15 @@ def find_by_sql_with_scrooge(sql)
callsite.reset
sql = sql.gsub(scrooge_select_regex, "SELECT #{scrooge_select_sql(callsite.columns)} FROM")
+
results = connection.select_all(sanitize_sql(sql), "#{name} Load Scrooged")
result_set = ResultSets::ResultArray.new
updateable = ResultSets::UpdateableResultSet.new(result_set, self)
- results.inject(result_set) do |memo, record|
- memo << instantiate(ScroogedAttributes.setup(record, callsite.columns, self, callsite_sig, updateable))
+ site_columns = callsite.columns
+
+ results.each do |record|
+ result_set << instantiate(ScroogedAttributes.setup(record, site_columns, self, callsite_sig, updateable))
end
callsite.register_result_set(result_set)
@@ -110,7 +119,7 @@ def scrooge_select_regex
def callsite_sql( sql )
sql.gsub(ScroogeRegexSanitize, ScroogeBlankString)
end
-
+
end
module InstanceMethods

0 comments on commit cf79a08

Please sign in to comment.