Advanced Rails - Building Industrial-Strength Web Apps in Record Time

(Tuis.) #1

160 | Chapter 6: Performance


end

# Invalidate cache when new location is assigned
def location=(val)
@location_geometry = @location_string = nil
super
end

# Invalidate cache when record is reloaded
def reload
@location_geometry = @location_string = nil
super
end
end

Now that we have made a substantial change, it is time to reprofile and see how the
numbers compare. After running the profiler again, we see that we have managed to
push that method a few notches down the list:


Thread ID: 2057980
Total: 11.21

%self total self wait child calls name
8.56 1.38 0.96 0.00 0.42 4430 Array#each_index
7.76 2.07 0.87 0.00 1.20 4230 Array#each-1
4.19 0.74 0.47 0.00 0.27 1040 GeoRuby::SimpleFeatures::
HexEWKBParser#decode_hex

We have cut the number of calls todecode_hexfrom 2,730 to 1,040. (This number is
still higher than 100 because there is another spatial class that we have not opti-
mized yet.) In addition, we have cut the total time spent indecode_hexfrom 1.26 sec-
onds to 0.47 seconds without optimizing the actual function at all. When we
benchmark these different versions of the application, we will see what kind of an
impact the optimizations have on the action as a whole.


Next, we notice from the preceding flat profile that we are spending the most time in
Array#each_indexandArray#each-1(the ruby-prof syntax indicating the first-level
recursive call toArray#each). These are more complicated to track down, primarily
because they have many callers (Array#each is used in many places).


The optimization process for this problem was fairly difficult, and we do not show it
here; it was a fairly boring and highly application-specific optimization. (It involved
rewriting a complicated clustering algorithm to build up the same data set using
fewer intermediate data structures.) But the change did result in a small improve-
ment in the profile, as well as the overall running time:


Thread ID: 2057980
Total: 10.7

%self total self wait child calls name
7.94 1.38 0.85 0.00 0.53 4430 Array#each_index
5.23 1.77 0.56 0.00 1.21 4230 Array#each-1
Free download pdf