Skip to content

Commit

Permalink
Minor performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sdsykes committed Apr 3, 2009
1 parent e1e9821 commit b377e1d
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 24 deletions.
25 changes: 3 additions & 22 deletions lib/optimizations/columns/attributes_proxy.rb
Expand Up @@ -34,33 +34,18 @@ class ScroogedAttributes < Hash
attr_accessor :fully_fetched, :klass, :updateable_result_set attr_accessor :fully_fetched, :klass, :updateable_result_set


def self.setup(record, klass, updateable_result_set) def self.setup(record, klass, updateable_result_set)
hash = new.replace(record) hash = new.replace(klass.columns_hash.merge(record))
hash.fully_fetched = false hash.fully_fetched = false
hash.klass = klass hash.klass = klass
hash.updateable_result_set = updateable_result_set hash.updateable_result_set = updateable_result_set
hash hash
end end


# Delegate Hash keys to all defined columns
#
def keys
@klass.column_names
end

# Let #has_key? consider defined columns
#
def has_key?(attr_name)
@klass.columns_hash.has_key?(attr_name)
end

alias_method :include?, :has_key?
alias_method :key?, :has_key?
alias_method :member?, :has_key?

# Lazily augment and load missing attributes # Lazily augment and load missing attributes
# #
def [](attr_name) def [](attr_name)
if interesting_for_scrooge?( attr_name ) return nil unless has_key?(attr_name)
if !scrooge_columns.include?(attr_name)
augment_callsite!( attr_name ) augment_callsite!( attr_name )
fetch_remaining fetch_remaining
add_to_scrooge_columns(attr_name) add_to_scrooge_columns(attr_name)
Expand Down Expand Up @@ -131,10 +116,6 @@ def fetch_remaining!( columns_to_fetch )
@updateable_result_set.reload_columns!(columns_to_fetch) @updateable_result_set.reload_columns!(columns_to_fetch)
end end


def interesting_for_scrooge?( attr_name )
@klass.columns_hash.has_key?(attr_name) && !scrooge_columns.include?(attr_name)
end

def augment_callsite!( attr_name ) def augment_callsite!( attr_name )
@klass.scrooge_seen_column!(callsite_signature, attr_name) @klass.scrooge_seen_column!(callsite_signature, attr_name)
end end
Expand Down
2 changes: 1 addition & 1 deletion lib/optimizations/result_sets/updateable_result_set.rb
Expand Up @@ -86,7 +86,7 @@ def update_with(remaining_attributes)
remaining_attributes.each do |r_id, r_att| remaining_attributes.each do |r_id, r_att|
old_attributes = current_attributes[r_id] old_attributes = current_attributes[r_id]
if old_attributes if old_attributes
old_attributes.update(r_att.merge(old_attributes)) # must call update, do not use reverse_update old_attributes.update(r_att)
end end
end end
end end
Expand Down
2 changes: 1 addition & 1 deletion lib/scrooge.rb
Expand Up @@ -27,7 +27,7 @@ class << self
# #
def find_by_sql(sql) def find_by_sql(sql)
if scope_with_scrooge?(sql) if scope_with_scrooge?(sql)
find_by_sql_without_scrooge(sql) find_by_sql_with_scrooge(sql)
else else
find_by_sql_without_scrooge(sql) find_by_sql_without_scrooge(sql)
end end
Expand Down

0 comments on commit b377e1d

Please sign in to comment.