Skip to content

Commit

Permalink
Patch Ancestry::InstanceMethods#ancestor_ids
Browse files Browse the repository at this point in the history
In many methods, this is generated twice because the result is never
saved.  This simply adds memoization to the result of this method.

Of Note:  The extra `#clear_memoized_instance_variables` additions were
necessary to fix failing tests due to ancestry records being updated and
saved, and the `@_ancestor_ids` value became out of date.

* * *

Before/After
------------

**Note:**  Only the totals here change... which is a bit weird and I
           didn't notice it before (probably just a reporting error on
           MemoryProfiler's end), but this patch was the only thing
           changed between the two runs.

Total allocated: 316196585 bytes (2917071 objects)   |   Total allocated: 307262021 bytes (2762757 objects)
                                  ^^^^^^^            |                                     ^^^^^^^
                                                     |
allocated objects by gem                             |   allocated objects by gem
-----------------------------------                  |   -----------------------------------
   1576834  activerecord-5.0.7                       |      1559102  activerecord-5.0.7
    477120  manageiq/app                             |       477120  manageiq/app
    274449  activemodel-5.0.7                        |       274449  activemodel-5.0.7
    208479  manageiq/lib                             |       106559  activesupport-5.0.7
    106557  activesupport-5.0.7                      |        82799  pending
     82799  pending                                  |        74117  ruby-2.3.3/lib
     74117  ruby-2.3.3/lib                           |        71895  manageiq/lib
     52875  manageiq-providers-vmware-0be2f13a0dc9   |        52875  manageiq-providers-vmware-0be2f13a0dc9
     35578  ancestry-2.2.2  <<<<<<<<<<               |        35578  ancestry-2.2.2  <<<<<<<<
     ...                                             |        ...
  • Loading branch information
NickLaMuro committed Apr 27, 2018
1 parent 6f8ad4c commit df6cc28
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/patches/ancestry_patch.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
module AncestryInstanceMethodsPatch
def update_descendants_with_new_ancestry
super
unless ancestry_callbacks_disabled?
if ancestry_changed? && !new_record? && sane_ancestry?
self.clear_memoized_instance_variables
unscoped_descendants.each(&:clear_memoized_instance_variables)
end
end
end
end

module Ancestry
module InstanceMethods
prepend AncestryInstanceMethodsPatch

def parse_ancestry_column obj
obj.to_s.split('/').map! { |id| cast_primary_key(id) }
end

def ancestor_ids
@_ancestor_ids ||= parse_ancestry_column(read_attribute(self.ancestry_base_class.ancestry_column))
end

STRING_BASED_KEYS = [:string, :uuid, :text].freeze
def cast_primary_key(key)
if STRING_BASED_KEYS.include? primary_key_type
Expand All @@ -14,5 +31,9 @@ def cast_primary_key(key)
end
end

def clear_memoized_instance_variables
@_ancestor_ids = nil
end

end
end

0 comments on commit df6cc28

Please sign in to comment.